Performing simple ADLS Gen2 Storage REST API operations using CURL
The blog points to performing simple ADLS Gen2 REST API operations such as List, Create, Update and Delete 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 for most of the operations. For the operation of setting up ACL, we will be making use of Bearer token 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, PATCH or DELETE as provided by the CURL specifications.
Filesystem - List
Filesystem - List - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" -H "x-ms-date: <Date and Time in GMT format>" "https://<StorageAccountName>.dfs.core.windows.net?resource=account&SASToken"
In the below snippet, the operation is being performed on the account sampleadlgen2account and it lists the filesystem present inside it as below:
Path - List
Path - List - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" -H "x-ms-date: <Date and Time in GMT format>" "https://<StorageAccountName>.dfs.core.windows.net?recursive=true&resource=filesystem&SASToken"
In the below snippet, the operation is being performed on the filesystem named mycontainer of the account sampleadlgen2account and it lists the filesystem in the recursive format:
In order to create a File in the filesystem, 3 set of API’s need to be called in sequence i.e. CreateFile(Put) -> Append File(Patch) -> Flush File (Patch). Let us now try calling each of them one by one as per their required set of headers
Create File
Path - Create - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: 0" -H "Content-Type: text/plain" “https://<StorageAccountName>.dfs.core.windows.net/<FileSystemName/FileName?resource=file&SASToken”
In the below snippet example, a Put operation was performed over the filesystem named mycontainer and the file name is sampletestfile. Note that we need to pass content length initially as 0. The file was created successfully.
You could further validate using Azure Portal or Azure Storage explorer as per your feasibility.
Append/Flush File
Path - Update - REST API (Azure Storage Services) | Microsoft Docs
The second API that needs to be called is to Append the data to the file that was created by the above call. For that we made a call to the Update API in which we passed the content to be written to the file along with its length.
curl -i -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 "Content-Type: text/plain" -d "Content of the file" “https://<StorageAccountName>.dfs.core.windows.net/<FileSystemName/FileName?action=append&position=0&SASToken”
At this point there is still no update to the content size.
The last is to call a Flush file operation and that is done using the same Update API with Flush parameter.
curl -i -X <HTTP Verb> -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: 0" -H "Content-Type: text/plain" “https://<StorageAccountName>.dfs.core.windows.net/<FileSystemName/FileName?action=flush&position=<flush position>&timeout=<timeout value>&close=true&SASToken"
Read File
Path - Read - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X GET -H "x-ms-version: 2021-06-08" -H "x-ms-date: <Date and Time in GMT format>" "https://<StorageAccountName>.dfs.core.windows.net/<FileSystem>/<Filename>?SASToken"
In the below example, we tried performing the Read File operation on the same file name sampletestfile present in the container named mycontainer that we created as part of above operations
Delete FileSystem
Filesystem - Delete - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2020-08-04" -H "x-ms-date: <Date and Time in GMT format>" https://<StorageAccountName>.dfs.core.windows.net/<FileSystemName>?resource=filesystem&SASToken
In the below example snippet, we deleted a filesystem named tes from the account storage account
Similar operation can be performed on the file level basis as well provided the path is provided appropriately
Set Access Control
Path - Update - REST API (Azure Storage Services) | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2020-08-04" -H "authorization: Bearer <Bearer Token>" -H "x-ms-date: <Date and Time in GMT format>" -H "x-ms-acl: <ACL Permission in appropriate format>"https://<StorageAccountName>.dfs.core.windows.net/<DirectoryName>/?action=setAccessControl"
Hope this helps!
Published on:
Learn moreRelated posts
Azure Storage - TLS 1.0 and 1.1 retirement
Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...
Efficient Management of Append and Page Blobs Using Azure Storage Actions
Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...
[Azure AI Search] Internal Server Error when creating CMK encrypted objects
Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...
Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate
Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...
Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat
Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...
Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#
Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...
Performing simple Azure Table Storage REST API operations using curl command.
The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...
Bulk delete all the old jobs from the batch account
Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...
Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases
In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...
[Azure Service Bus] JMS messages getting dead-lettered
The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...