Performing simple File Storage REST API operations using Postman
Published Dec 18 2023 03:12 AM 3,894 Views

The blog provides guidance to perform simple File storage REST API operations such as Create, Copy, Rename and Delete operations using Postman.

 

Let’s look at some of the 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 Postman: 

  • Ensure the URL is formed correctly as per the operation you are trying to perform.
  • The mandatory header needs to be passed.
  • 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 REST API specifications.

Create Share:

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

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

Syntax URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare?restype=share&SAStoken

In the below snippet example, a PUT operation was performed on the storage account named somatest to create a File share name as createshare1. File Share was created successfully.

Soma_Sekhara_Raju_0-1701757459645.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 URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/DirectoryToBeCreated?restype=directory&SAStoken

In the below example, we tried performing the Create directory operation named as createmydirectory1 on existing File share.

Soma_Sekhara_Raju_1-1701757604923.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 URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/DirectoryToBeDeleted?restype=directory&SAStoken

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

Soma_Sekhara_Raju_0-1701757961844.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 URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileToBeCreated?SAStoken

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

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

Soma_Sekhara_Raju_0-1701758367976.png

 

Put Range:

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

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

Syntax URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileToBeUpdated?comp=range?SAStoken

Mandatory headers:

x-ms-content-length:<Number of bytes being transmitted in the request body>
x-ms-date:<Date and Time in GMT format>
x-ms-version:2021-06-08
x-ms-range:bytes=<Range of bytes to be written. Both the start and end of the range must be specified>
x-ms-write:<update or Clear>

In the below example snippet, we have tried to update a file which is created as part of Create File REST API. We are writing 5bytes to the file.

Soma_Sekhara_Raju_0-1702454692322.pngSoma_Sekhara_Raju_1-1702454700708.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 URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/FileNameToBeCreated?SAStoken

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>

In the below example snippet, we have copied the file named copyfile2 from Source fileshare testshare to destination fileshare createshare. You need to authorize the source file to successfully copy to destination.

 Soma_Sekhara_Raju_1-1701758659140.png

 

Rename file:

The Rename File operation renames a file in a storage account.

Rename File (REST API) - Azure Storage | Microsoft Learn

Syntax URL:

https://StorageAccountName.file.core.windows.net/NameOfTheFileshare/NameOfTheDirectory/NewFileToBeReamed?comp=rename&SAStoken

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?SAStoken>

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

Soma_Sekhara_Raju_2-1701758907731.png

Conclusion:

Should you need to test a different API not discussed here, you can easily follow a similar pattern. Adjust the necessary header parameters as required by the specific API you're exploring.

This flexibility allows you to extend these testing techniques to a broad spectrum of APIs, ensuring you're equipped to handle various endpoints by customizing headers and configurations as needed.

I hope this information proves valuable in navigating different APIs and adapting your approach to their unique requirements. Happy testing, discovering, and harnessing the potential of APIs to drive your development endeavors!

 

Hope this helps!!

 

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

Co-Authors
Version history
Last update:
‎Dec 15 2023 06:07 AM
Updated by: