Errors Connecting to Azure Storage File Share using Rest API

Drew Wesley 0 Reputation points
2024-01-05T20:27:14.19+00:00

I'm trying to transfer a pdf file to a storage account (file share).

I'm successful doing this using the REST API with PHP/cURL to a container blob using an access key, but not to a file share. These are the error response messages I receive.

Is there any way to get more information on what header information being sent is incorrect?

Do I need to use an SAS (shared access signature)?

InvalidAuthenticationInfoAuthentication information is not given in the correct format.

...or...

AuthenticationFailedServer failed to authenticate the request.

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,388 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,419 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Silvia Wibowo 5,456 Reputation points Microsoft Employee
    2024-01-07T23:48:25.1266667+00:00

    Hi @Drew Wesley , I understand that you're trying to put a PDF file into Azure File Share, but you got InvalidAuthentication error.

    You can reach Azure File Share using REST API with these authentication method - see entry of Azure Files (REST): Storage Account Key, SAS, or Entra ID. Note that most of the REST APIs you'll use for Azure Files are different from those for Azure Storage Blob.

    Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.

    0 comments No comments

  2. Sumarigo-MSFT 47,516 Reputation points Microsoft Employee
    2024-01-08T15:33:31.0833333+00:00

    @Andrew Butt Welcome to Microsoft Q&A Forum, Thank you for posting your query here!
    Adding more information to the above response!

    Some time PC time could cause this issue, please check the date and time.

    Can you try to re-generate the SAS token through Azure Storage Explorer tool or generating a shared access signature (in azure portal) for key2 instead of key1. Changing the key fixed the error. (Settings > Shared access signature) Also the connection string should be updated as well - if used. (Settings > Access keys)and try again

    Also to upload or download files to an Azure Storage account, there are several option, Especially easy and the fastest ways is through Azcopy tool

    There is similar thread discussion in the SO forum, please refer to the suggestion mentioned here

    Azure Files REST API: https://learn.microsoft.com/en-us/rest/api/storageservices/file-service-rest-api

    Based on the error message : Server failed to authenticate the request. Please refer to the information in the www-authenticate header or The authentication information wasn't provided in the correct format. Verify the value of Authorization header.

    User's image

    User's image

    If the issue still persist, I would like to work closer on this issue!

    Please let us know if you have any further queries. I’m happy to assist you further.


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


  3. Drew Wesley 0 Reputation points
    2024-01-30T14:32:00.8533333+00:00

    I was able to successfully work with the RestAPI to transfer the file by understanding the necessary headers involved in the two step process of 1) creating the file, and 2) writing to it. I'm using PHP, so while the headers are similar to what I used below for testing in a cmd prompt, they're slightly different. Pointers: When creating the file, the x-ms-content-length is the size of the file, but the Content-Length is 0, 'but' when writing the file, the x-ms-range must be specified as '0 - <file length - 1>', and may need to be 'chunked' when uploading larger files. Depending on the current API's chunk size limit (which used to be 4mb), the chunking ranges would look like this for a 8192mb file:
    0 - 4095
    4096 - 8191
    ...requiring two separate curl calls to write each chunk (data packet). (Hope this helps someone out there..) === AZURE FILE STORAGE + REST API + SAS AUTH + CURL: UPLOAD FILE PT. 1/2: CREATE FILE === curl -X PUT ^ -H "x-ms-type: file" ^ -H "x-ms-content-length: 342547" ^ -H "Content-Length: 0" ^ --data-binary "@<local_file_path>" "https://<account_name>.file.core.windows.net/<file_share>/<directory><file>?<sas>" === AZURE FILE STORAGE + REST API + SAS AUTH + CURL: UPLOAD FILE PT. 2/2: WRITE TO FILE === curl -X PUT ^ -H "x-ms-write: update" ^ -H "x-ms-range: bytes=0-342546" ^ -H "x-ms-content-length: 342547" ^ --data-binary "@<local_file_path>" "https://<account_name>.file.core.windows.net/<file_share>/<directory><file>?<sas>?comp=range"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.