Ultimate Guide to Azure Storage APIs for Modern Applications

Azure Storage: An In-Depth Guide

Azure Storage provides scalable, durable cloud storage, backup, and recovery solutions for any data — big and small. The following guide delves into the most pertinent Azure Storage APIs and how to use them effectively with code examples.

Creating a Storage Account

  
  az storage account create \
    --name mystorageaccount \
    --resource-group myResourceGroup \
    --location westus \
    --sku Standard_LRS
  

Blob Storage

Upload a Blob

  
  from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
  
  connect_str = ""
  blob_service_client = BlobServiceClient.from_connection_string(connect_str)
  container_name = "mycontainer"

  container_client = blob_service_client.get_container_client(container_name)
  blob_client = container_client.get_blob_client("myfile.txt")

  with open("myfile.txt", "rb") as data:
      blob_client.upload_blob(data)
  

Download a Blob

  
  with open("downloadedfile.txt", "wb") as download_file:
      download_file.write(blob_client.download_blob().readall())
  

List Blobs

  
  blobs = container_client.list_blobs()
  for blob in blobs:
      print(blob.name)
  

Table Storage

Create a Table

  
  from azure.data.tables import TableServiceClient

  table_service_client = TableServiceClient.from_connection_string(conn_str=connect_str)
  table_client = table_service_client.create_table_if_not_exists("mytable")
  

Insert Entity

  
  entity = {
      u'PartitionKey': u'partitionkey',
      u'RowKey': u'rowkey',
      u'property1': u'value',
      u'property2': 123,
  }
  table_client.create_entity(entity=entity)
  

Query Entities

  
  from azure.data.tables import TableQuery
  
  query_filter = "PartitionKey eq 'partitionkey'"
  entities = table_client.query_entities(query_filter=query_filter)
  for entity in entities:
      print(entity)
  

Queue Storage

Create a Queue

  
  from azure.storage.queue import QueueServiceClient

  queue_service_client = QueueServiceClient.from_connection_string(connect_str)
  queue_service_client.create_queue("myqueue")
  

Add Message to Queue

  
  queue_client = queue_service_client.get_queue_client("myqueue")
  queue_client.send_message("Hello, Azure!")
  

Receive Messages

  
  messages = queue_client.receive_messages()
  for msg in messages:
      print(msg.content)
      queue_client.delete_message(msg)
  

File Storage

Create a File Share

  
  from azure.storage.file import FileServiceClient

  file_service_client = FileServiceClient.from_connection_string(connect_str)
  share_client = file_service_client.get_share_client("myfileshare")
  share_client.create_share()
  

Upload a File

  
  from azure.storage.file import ShareFileClient

  file_client = share_client.get_file_client("samplefile.txt")
  with open("samplefile.txt", "rb") as source_file:
      file_client.upload_file(source_file)
  

Azure Storage Application Example

  
  import os
  from azure.storage.blob import BlobServiceClient
  from azure.data.tables import TableServiceClient
  from azure.storage.queue import QueueServiceClient
  from azure.storage.file import FileServiceClient

  connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")

  # Blob Storage: Upload
  blob_service_client = BlobServiceClient.from_connection_string(connect_str)
  container_client = blob_service_client.get_container_client("mycontainer")
  blob_client = container_client.get_blob_client("myfile.txt")
  with open("myfile.txt", "rb") as data:
      blob_client.upload_blob(data)

  # Table Storage: Insert
  table_service_client = TableServiceClient.from_connection_string(connect_str)
  table_client = table_service_client.create_table_if_not_exists("mytable")
  entity = {"PartitionKey": "partitionkey", "RowKey": "rowkey", "property1": "value", "property2": 123}
  table_client.create_entity(entity)

  # Queue Storage: Send Message
  queue_service_client = QueueServiceClient.from_connection_string(connect_str)
  queue_client = queue_service_client.get_queue_client("myqueue")
  queue_client.send_message("Hello, Azure!")

  # File Storage: Upload
  file_service_client = FileServiceClient.from_connection_string(connect_str)
  share_client = file_service_client.get_share_client("myfileshare")
  share_client.create_share()
  file_client = share_client.get_file_client("samplefile.txt")
  with open("samplefile.txt", "rb") as source_file:
      file_client.upload_file(source_file)
  

By leveraging Azure Storage services, you can ensure that your application data is resilient, readily available, and easily manageable.

Hash: c275e2c11635e73c98159167afe7d8a4fba65cbbd0261f154153901e73a52bde

Leave a Reply

Your email address will not be published. Required fields are marked *