Loading...

Performing simple FILE Storage REST API operations using CURL

Image

The blog aims to perform simple File storage REST API operations such as Create, Copy, Rename and Delete operations using CURL utility.

Let’s look at some of the CURL command syntax to perform REST API operations and will be making use of SAS as the auth scheme. 

We need to take care of the pointers below while performing the operations via CURL:

  • Ensure the URL is formed correctly as per the operation you are trying to perform.
  • The mandatory header needs to be passed and can be done using “-H” parameter.
  • Ensure you are appending/removing extra ‘?’ to the SAS token in the URLs accordingly.
  • Http verb can be GET, PUT or DELETE as provided by the CURL specifications.

Note:

If you employ an authentication mechanism different from SAS, please ensure that you are including the required headers.

 

Create FileShare:
The create share operation creates a new Azure Files share under the specified account.

Create Share (FileREST API) - Azure Files | Microsoft Learn

Syntax:

 

curl -X <HTTP Verb> -H "x-ms-version: 2019-07-07" -H "Content-Length: Content Length in Bytes" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare?restype=share&SAStoken"

 

In the below example screenshot, a PUT operation was performed on the storage account to create a File share name as testfileshare. File Share was created successfully.

ManjunathS_1-1704192335167.png

ManjunathS_2-1704192365784.png

 

Create directory:

The Create Directory operation creates a new directory under the specified share or parent directory.

Create Directory (FileREST API) - Azure Files | Microsoft Learn 

Syntax :

 

curl -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: Content Length in Bytes" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/DirectoryToBeCreated?restype=directory&SAStoken"

 

In the below example screenshot, a PUT operation was performed on the storage account to create a directory name as testdir1. Directory was created successfully.

ManjunathS_13-1704192716278.png

ManjunathS_1-1704192559894.png

 

Delete Directory:
The Delete Directory operation removes the specified empty directory. Note that the directory must be empty before it can be deleted.
Delete Directory (FileREST API) - Azure Files | Microsoft Learn

Syntax :

 

curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/DirectoryToBeDeleted?restype=directory&SAStoken"

 

In the below example screenshot, we have deleted the directory named myfileshare inside a file share using HTTP verb as DELETE

ManjunathS_2-1704192559920.png

ManjunathS_3-1704192559925.png

 

Create file:
The Create File operation creates a new file or replaces a file. When you call Create File, you only initialize the file. To add content to a file, you call the Put Range operation.
 Create File (FileREST API) - Azure Files | Microsoft Learn

Syntax :

 

curl -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "x-ms-type: file" -H "x-ms-content-length:50000" -H "Content-Length: Content Length in Bytes" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/FileToBeCreatedWithFileExtension?SAStoken"

 

In the below example screenshot, we have created a file named filecreate1 inside a directory and File share.

Mandatory headers:

 

x-ms-content-length: size for the file, up to 4 tebibytes (TiB) x-ms-type: file x-ms-date: <Date and Time in GMT format> x-ms-version: 2021-06-08

 

ManjunathS_4-1704192559940.png

ManjunathS_5-1704192559943.png

 

Put Range:
The Put Range operation writes a range of bytes to a file.

Put Range (FileREST API) - Azure Files | Microsoft Learn

Syntax :

 

curl -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: Content Length in Bytes" -H "x-ms-range: bytes=startByte-endByte" -H "x-ms-write: <update or Clear>" -d "Content of the blob" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileToBeUpdatedWithFileExtension?comp=range?SAStoken"

 

Mandatory headers:

ManjunathS_6-1704192559945.png

In the below example screenshot, We are writing 9 bytes of data i.e. "abcdefghi" to an existing file.

ManjunathS_7-1704192559948.png

ManjunathS_8-1704192559952.png

 

Copy file:

The Copy File operation copies a blob or file to a destination file within the storage account.
Copy File (FileREST API) - Azure Files | Microsoft Learn

Syntax :

 

curl -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: Content Length in Bytes" -H "x-ms-copy-source: https://SourceStorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileToBeCopiedWithFileExtension?SAStoken" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileNameToBeCreatedWithFileExtension?SAStoken"

 

In the below example screenshot, we have copied the file named copyfile.txt from Source fileshare testfileshare to destination fileshare myfileshare1. You need to authorize the source file to successfully copy to destination.

Mandatory headers:

 

x-ms-date: <Date and Time in GMT format> x-ms-version: 2021-06-08 x-ms-copy-source: https://SourceStorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory /FileToBeCopied.txt?SAStoken

 

ManjunathS_9-1704192559972.png

ManjunathS_10-1704192559976.png

 

Rename file:
The Rename File operation renames a file in a storage account.
 Rename File (REST API) - Azure Storage | Microsoft Learn

Syntax URL:

 

curl -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: Content Length in Bytes" -H "x-ms-type: file" -H "x-ms-file-rename-source: https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/ExistingFileWithFileExtension?SAS token" "https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/NewFileToBeRenamedWithFileExtension?comp=rename&SAStoken"

 

In the below example screenshot, we have renamed the file from filecreate2 to filecreate3.

Mandatory headers:

 

x-ms-date: <Date and Time in GMT format> x-ms-version: 2021-06-08 x-ms-file-rename-source : https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/ExistingFile?SAS token

 

ManjunathS_11-1704192560007.png

ManjunathS_12-1704192560011.png

 

Note:

To bypass the SSL certification warnings, you have the option to use the "-k" flag, which permits insecure server connections when utilizing SSL. However, it is not recommended.

 

Conclusion:
If you find yourself needing to test a different API that hasn't been covered in this discussion, you can effortlessly apply a comparable approach. Simply modify the essential header parameters according to the specifications of the particular API you are examining.

 

This adaptability empowers you to apply these testing methods across a diverse range of APIs, ensuring that you can effectively manage different endpoints by adjusting headers and configurations as necessary.

 

I trust that this information is beneficial for navigating through various APIs and tailoring your approach to meet their distinct requirements. Best of luck with your testing, exploration, and leveraging of APIs to advance your development initiatives!

 

If your curl command does not execute or return what you expected it to, We can run the command with the -v or --verbose option to get more information.

When verbose mode is enabled, curl gets more talkative and explains and shows a lot more of its doings.
Example screenshot,

ManjunathS_0-1704822606999.png

ManjunathS_1-1704822607011.png

 

I hope this explanation is helpful!

 

Reference Links:

Operations on the FileService resource - Azure Files | Microsoft Learn

Operations on the FileShare resource - Azure Files | Microsoft Learn

Operations on directories (FileREST API) - Azure Files | Microsoft Learn

Operations on files (FileREST API) - Azure Files | Microsoft Learn

Learn more
Author image

Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related

Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!

* Yes, I agree to the privacy policy