how can I store files to azure blob in python?
- User
how can I store files to azure blob in python?
- Assistant
To store files in Azure Blob Storage using Python, you can use the
azure-storage-bloblibrary. Below are the steps to achieve this:-
Install the Azure Storage Blob library: You can install the library using pip:
shpip install azure-storage-blob -
Set up your Azure Blob Storage connection: You need your Azure Storage account name and account key. You can find these in the Azure portal.
-
Upload a file to Azure Blob Storage: Here is a sample code to upload a file:
pythonimport os from azure.storage.blob import BlobServiceClient # Replace with your own values account_name = 'your_account_name' account_key = 'your_account_key' container_name = 'your_container_name' blob_name = 'your_blob_name' file_path = 'path_to_your_file' try: # Create the BlobServiceClient object which will be used to create a container client connect_str = f"DefaultEndpointsProtocol=https;AccountName={account_name};AccountKey={account_key};EndpointSuffix=core.windows.net" blob_service_client = BlobServiceClient.from_connection_string(connect_str) # Create a container client container_client = blob_service_client.get_container_client(container_name) # Create a blob client blob_client = container_client.get_blob_client(blob_name) # Upload the file with open(file_path, "rb") as data: blob_client.upload_blob(data) print(f"File '{file_path}' uploaded to '{container_name}/{blob_name}' successfully.") except Exception as ex: print(f"An error occurred: {ex}")
Explanation:
-
Import the necessary modules:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.