Thursday, March 7, 2024

How to fetch the file from azure blob storage using API

To fetch a file from Azure Blob Storage using an API, you typically use the Azure Storage Blob client library, which provides APIs for interacting with Azure Blob Storage. Here's a general outline of how you can do it in Python using the `azure-storage-blob` library:

1. Install the Azure Blob Storage library:
   ```
   pip install azure-storage-blob
   ```

2. Import the necessary modules:
   ```python
   from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
   ```

3. Connect to your Azure Storage Account:
   ```python
   connection_string = "<your_connection_string>"
   blob_service_client = BlobServiceClient.from_connection_string(connection_string)
   ```

4. Get a reference to the container where your blob is stored:
   ```python
   container_name = "<your_container_name>"
   container_client = blob_service_client.get_container_client(container_name)
   ```

5. Get a reference to the blob you want to fetch:
   ```python
   blob_name = "<your_blob_name>"
   blob_client = container_client.get_blob_client(blob_name)
   ```

6. Download the blob content:
   ```python
   with open("<local_file_path>", "wb") as f:
       blob_data = blob_client.download_blob()
       blob_data.readinto(f)
   ```

Replace placeholders such as `<your_connection_string>`, `<your_container_name>`, `<your_blob_name>`, and `<local_file_path>` with your actual Azure Storage Account connection string, container name, blob name, and local file path, respectively.

No comments: