How to mount azure blob storage in Linux to read and write files to it. Provide python…
- User
How to mount azure blob storage in Linux to read and write files to it. Provide python code.
- Assistant
How to mount azure blob storage in Linux to read and write files to it. Provide python code.
To mount Azure Blob Storage in Linux and read/write files using Python, you can use BlobFuse, an open-source project developed by Microsoft. BlobFuse allows you to mount an Azure Blob Storage container as a filesystem on Linux, enabling you to interact with it using standard file system APIs.
Below are the steps to mount Azure Blob Storage using BlobFuse and Python code examples to read and write files.
Azure Subscription: You need an active Azure subscription. If you don't have one, you can create a free account here.
Azure Storage Account: An Azure Storage account with a Blob container. You can create one via the Azure Portal or Azure CLI.
Linux Machine: A Linux machine (local or VM) where you have sudo privileges.
Azure Storage Account Credentials: You need the storage account name and either the account key or a shared access signature (SAS).
# Update package lists
sudo apt-get update
# Install prerequisite packages
sudo apt-get install -y pkg-config libfuse3-dev fuse3
Download and install the BlobFuse package appropriate for your distribution.
For Ubuntu 20.04:
# Download the package
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
# Install the package
sudo dpkg -i packages-microsoft-prod.deb
# Update package lists again
sudo apt-get update
# Install blobfuse2
sudo apt-get install -y blobfuse2
Create a configuration file for BlobFuse.
# Create a directory for BlobFuse configuration
mkdir ~/blobfuse2
# Create a configuration file
nano ~/blobfuse2/blobfuse2.yaml
Add the following content to blobfuse2.yaml:
version: 2
logging:
type: syslog
level: log_debug
components:
- libfuse
- attr_cache
- azstorage
libfuse:
path: /path/to/mountpoint # Replace with your mount point
azstorage:
type: block
account_name: your_storage_account_name # Replace with your storage account name
account_key: your_storage_account_key # Replace with your storage account key
container: your_container_name # Replace with your container name
Replace the placeholders (/path/to/mountpoint, your_storage_account_name, your_storage_account_key, your_container_name) with your actual storage account details and desired mount point.
sudo mkdir /mnt/azureblob # Or any other directory you specified
sudo chown $USER:$USER /mnt/azureblob
blobfuse2 mount ~/blobfuse2/blobfuse2.yaml
Verify that the storage is mounted:
df -h | grep blobfuse2
Now that the Azure Blob Storage is mounted to your Linux filesystem, you can use regular file operations in Python to read and write files.
# write_file.py
file_path = '/mnt/azureblob/hello.txt' # Path in the mounted container
with open(file_path, 'w') as file:
file.write('Hello, Azure Blob Storage!\n')
file.write('This is a test file written from Python.\n')
print(f'File written to {file_path}')
# read_file.py
file_path = '/mnt/azureblob/hello.txt' # Path in the mounted container
with open(file_path, 'r') as file:
content = file.read()
print(f'Content of {file_path}:')
print(content)
Note: Ensure that your Python script has the necessary permissions to read and write to the mount point.
If you prefer not to mount the storage and instead access it directly from your Python application, you can use the Azure Blob Storage SDK for Python.
pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
# Replace with your connection string
connection_string = 'DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net'
# Create a BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Specify the container and blob name
container_name = 'your-container-name'
blob_name = 'hello.txt'
# Create the BlobClient
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# Write content to the blob
data = 'Hello, Azure Blob Storage!\nThis is a test file written from Python.\n'
blob_client.upload_blob(data, overwrite=True)
print(f'Blob {blob_name} uploaded to container {container_name}.')
from azure.storage.blob import BlobServiceClient
# Replace with your connection string
connection_string = 'DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net'
# Create a BlobServiceClient object
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Specify the container and blob name
container_name = 'your-container-name'
blob_name = 'hello.txt'
# Create the BlobClient
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# Download the blob's content
stream = blob_client.download_blob()
content = stream.readall().decode('utf-8')
print(f'Content of blob {blob_name}:')
print(content)
Mounting with BlobFuse: Allows you to interact with Azure Blob Storage using standard file operations, which can be convenient if you have existing code that works with a file system.
Using Azure SDK for Python: Direct interaction with Azure Blob Storage via the SDK can provide better performance and more control over the operations.
Choose the method that best suits your application's requirements.
Note: Always ensure that you handle your Azure credentials securely. Avoid hardcoding sensitive information in your scripts. Consider using environment variables or Azure Managed Identities for authentication in production environments.
Not yet included in a published release.