Performing simple BLOB Storage REST API operations using CURL
The blog points to perform simple storage REST API operations such as Get, Put, List 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. 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.
List Blobs
List Blobs (REST API) - Azure Storage | Microsoft Docs
Syntax:
curl -I -X <HTTP Verb> -H "x-ms-version: 2019-12-12" “https://storageAccountName.blob.core.windows.net/containerName?restype=container&comp=list&SASToken”
In the below snippet, the operation is being performed over a container named testcontainer11. There is just one single blob present in the container, and it is listed as below:
Put Blob
Put Blob (REST API) - Azure Storage | 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>" -H "x-ms-blob-type: Blob Type" -H "Content-Length: <Length of the blob>" -d "Blob data/content" “https://storageAccountName.blob.core.windows.net/containerName/blobname?SASToken”
In the below snippet example, a Put operation was performed over the container named testcontainer11 and the blob name is myblob3. The blob type was passed as block blob along with the content of the blob and its length. The blob was created successfully.
You could further validate using Azure Portal or Azure Storage explorer as per your feasibility.
The above example was inclined towards the Block Blob however we can also perform the same over the append blobs as well. Below is how we need to perform the same:
We first need to call the Put Blob operation only with blob type as Append Blob and with Content-Length header as 0
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" -H "x-ms-date: <Date and Time in GMT format>" -H "x-ms-blob-type: AppendBlob" -H "Content-Length: 0" “https://storageAccountName.blob.core.windows.net/containerName/blobname?SASToken”
We then call the Append Block API to write to the blob
Append Block (REST API) - Azure Storage | Microsoft Docs
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" -H "x-ms-date: <Date and Time in GMT format>" -H "Content-Length: Content Length in Bytes" -d "Content of the blob" "https://storageAccountName.blob.core.windows.net/containerName/blobname?comp=appendblock&SASToken"
At this point, below were the content of the file:
We again called the Append Block API to append to the above blob further as well.
You will notice a change in the file size
We could see updated content as well:
Get Blob
Get Blob (REST API) - Azure Storage | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" "https://storageAccountName.blob.core.windows.net/containername/blobname?SASToken"
In the below example, we tried performing the GetBlob operation on the same blob name myblob3 present in the container named testcontainer11 that we created as part of above Put Operation
Delete Blob
Delete Blob (REST API) - Azure Storage | Microsoft Docs
Syntax:
curl -i -X <HTTP Verb> -H "x-ms-version: 2019-12-12" “https://storageAccountName.blob.core.windows.net/containername/blobname?SASToken”
In the below example snippet, we deleted the blob named myblob3 from the container named testcontainer11
Copy Blob
Copy Blob (REST API) - Azure Storage | Microsoft Docs
Syntax:
curl --request <http Verb> "<Destination URL>" --header "x-ms-date: <Date and Time in GMT format>" " --header "x-ms-version: 2019-12-12" --header "x-ms-copy-source: <SOURCE URL>" --header "Content-length: 0"
In the below example snippet, we copied the blob named Appointment_confirmation.jpg from the container named testcontainer10 present in the source storage account to a blob named abc.jpg present in destination container named testcontainer present in the destination storage account
Hope this helps!
Published on:
Learn more