How to write to Azure Storage Gen2 via Rest API

Viktor Chuhra
2 min readAug 15, 2019

--

Requirements: Postman, generated SAS signature, storage account with Azure Data Lake Storage Gen2 file system.

Following approach requires SAS(shared access signature) token to be generated in the Azure portal and used as the parameter.

Navigate to Azure portal -> storage account -> Shared access signature -> Generate SAS and connection string

Import following collection into Postman:

postman_collection.json

{
"info": {
"name": "create_files_collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "create_file",
"request": {
"auth": {
"type": "noauth"
},
"method": "PUT",
"header": [],
"url": {
"raw": "https://{{accountName}}.{{dnsSuffix}}/{{filesystem}}/{{file_name}}?resource=file&{{sas_token}}",
"protocol": "https",
"host": [
"{{accountName}}",
"{{dnsSuffix}}"
],
"path": [
"{{filesystem}}",
"{{file_name}}"
],
"query": [
{
"key": "resource",
"value": "file"
},
{
"key": "{{sas_token}}",
"value": null
}
]
}
},
"response": []
},
{
"name": "write_content",
"request": {
"auth": {
"type": "noauth"
},
"method": "PATCH",
"header": [
{
"key": "Content-Length",
"value": "5",
"type": "text"
},
{
"key": "Content-Type",
"name": "Content-Type",
"value": "text/plain",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "hello"
},
"url": {
"raw": "https://{{accountName}}.{{dnsSuffix}}/{{filesystem}}/{{file_name}}?position=0&action=append&{{sas_token}}",
"protocol": "https",
"host": [
"{{accountName}}",
"{{dnsSuffix}}"
],
"path": [
"{{filesystem}}",
"{{file_name}}"
],
"query": [
{
"key": "position",
"value": "0"
},
{
"key": "action",
"value": "append"
},
{
"key": "{{sas_token}}",
"value": null
}
]
}
},
"response": []
},
{
"name": "flush",
"request": {
"auth": {
"type": "noauth"
},
"method": "PATCH",
"header": [
{
"key": "Content-Type",
"value": "text/plain",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "https://{{accountName}}.{{dnsSuffix}}/{{filesystem}}/{{file_name}}?action=flush&position=5&{{sas_token}}",
"protocol": "https",
"host": [
"{{accountName}}",
"{{dnsSuffix}}"
],
"path": [
"{{filesystem}}",
"{{file_name}}"
],
"query": [
{
"key": "action",
"value": "flush"
},
{
"key": "position",
"value": "5"
},
{
"key": "{{sas_token}}",
"value": null
}
]
}
},
"response": []
},
{
"name": "read",
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "Content-Type",
"value": "text/plain",
"type": "text"
}
],
"url": {
"raw": "https://{{accountName}}.{{dnsSuffix}}/{{filesystem}}/{{file_name}}?{{sas_token}}",
"protocol": "https",
"host": [
"{{accountName}}",
"{{dnsSuffix}}"
],
"path": [
"{{filesystem}}",
"{{file_name}}"
],
"query": [
{
"key": "resource",
"value": "file",
"disabled": true
},
{
"key": "{{sas_token}}",
"value": null
}
]
}
},
"response": []
}
]
}

Import following Postman environment file, define required variables (accountName, filesystem, sas_token) and set this environment as active

environment.json

{"id": "ecc28076-ae53-4f59-93b9-72f8dcf01492","name": "azure_env","values": [{"key": "accountName","value": "","enabled": true},{"key": "dnsSuffix","value": "dfs.core.windows.net","enabled": true},{"key": "filesystem","value": "","enabled": true},{"key": "sas_token","value": "","enabled": true},{"key": "file_name","value": "hello.txt","enabled": true}],"_postman_variable_scope": "environment","_postman_exported_using": "Postman/7.6.0-canary02"}

Postman collection defines 4 simple steps:

  • Create file (response code: 201 Created)
  • Write content into the file stream (response code: 202 Accepted)
  • Flush stream to the file (200 OK)
  • Read file contents via GET request (200 OK)

I hope it was helpful. I have spent almost half of the day discovering this.

--

--