-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path46. Blob Storage.py
26 lines (18 loc) · 1.43 KB
/
46. Blob Storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# To interact with Blob storage using Python, you can use the Azure Blob Storage SDK for Python. Here's some sample code that demonstrates how to create a new container in Blob storage and upload a file:
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import os
# Set the connection string for your Azure Storage account
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
# Create the Blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Create a new container
container_name = 'my-container'
container_client = blob_service_client.create_container(container_name)
# Upload a file to the container
local_file_path = '/path/to/local/file'
blob_name = 'my-blob'
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
with open(local_file_path, 'rb') as data:
blob_client.upload_blob(data)
# This code assumes that you have set the AZURE_STORAGE_CONNECTION_STRING environment variable to the connection string for your Azure Storage account. You will also need to replace /path/to/local/file with the path to the file you want to upload, and my-container and my-blob with the names you want to use for the container and blob, respectively.
#Once the file has been uploaded, you can access it using the URL generated by the blob_client.url property. You can also list the blobs in a container using the container_client.list_blobs() method.