In this blog, We’ll look at how to use Python to discard a file into an Azure Blob and copy it from one container to another.
What are Blobs?
Blobs are objects that can store large amounts of text or binary data such as images, documents, multimedia, and archive data.
Azure Blob Storage was created to meet specific requirements. If the business use case requires the storage of unstructured data such as audio, video, images, and so on, you should probably choose this option. The objects that are stored in Blob do not always have an extension.
The following points describe the use case scenarios:
- Serving images or documents directly to a browser
- Storing Files for distributed access
- Streaming video and audio
- Writing to log Files
- Storing data for backup, restore, disaster recovery and archiving
- Storing data for analysis by an on-premises or Azure-hosted service
A container acts as a directory in a file system to organize a collection of blobs.
What is Azure Storage Account?
A storage account is a container for a collection of Azure Storage services. A storage account can only include data services from Azure Storage. Merging data services into a storage account allows the user to manage them as a group. The settings specified when the account is created, or those changed after the account is created, are applicable everywhere. When a storage account is deleted, all the data contained within it is also deleted.
Prerequisites:
- An Azure account with an active subscription.
- An Azure Storage account.
- Python 2.7 or 3.6+.
Creating a Container for uploading a file into a Blob:
Step 1: Create a new directory for the project and navigate to it.
mkdir azure-file-uploader
cd azure-file-uploader
Step 2: Create a new directory called data inside the azure-file-uploader directory. The blob data files will be created and saved in this directory.
mkdir data
Step 3: Install the Azure Blob Storage client library for Python and any libraries that it depends on. In this particular instance, it is simply the Azure Python core library.
pip install azure-storage-blob
Step 4: Create a Python file named blog2.py and import the required packages.
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__ from dotenv import load_dotenv
Step 5: Select Access keys from the Storage Account menu pane’s Security + networking section. You can see the account access keys as well as the entire connection string for each key here.
Select Show keys from the Access keys pane. Find the Connection string value in the key1 section. Make an.env file, add a variable, and paste the connection string into it.
AZURE_STORAGE_CONNECTION_STRING = "<connection string>"
and add this code to the blog2.py.
load_dotenv()
connect_str = os.environ.get('AZURE_STORAGE_CONNECTION_STRING')
Step 6: Add this code to a try-except block to make a container. The BlobServiceClient object is used to create a container client, and it is given a unique name.
try:
print("Azure Blob Storage v" + __version__ + " - Python quickstart sample")
blob_service_client = BlobServiceClient.from_connection_string(connect_str) container_name = str(uuid.uuid4())
container_client = blob_service_client.create_container(container_name)
except Exception as ex:
print('Exception:')
print(ex)
Step 7: Add the following code to the try block: create a local directory to hold blob data and create and write a file in the local data directory to upload it to the blob.
local_path = "./data"
os.mkdir(local_path)
local_file_name = str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)
file = open(upload_file_path, 'w')
file.write("Hello, World!")
file.close()
Step 8: Add the following code to the try block to create a blob client with the local file name as the blob name and upload the created file.
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)
Step 9: Run the python file
python blog2.py
OUTPUT:
Azure Blob Storage v12 - Python quickstart sample
Uploading to Azure Storage as blob:
quickstartcf275796-2188-4057-b6fb-038352e35038.txt
Thus a text file is created and uploaded into the Blob with Python.
Copying a blob from one container to another
Copy the account name to a variable called account name in the.env file.
To move or copy a Blob from one container to another, add this function.
account_name = os.environ.get('account_name')
# Source
source_container_name = "<source container name>"source_file_path = "<generated text file name>"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
source_blob = (f"https://{account_name}.blob.core.windows.net/{source_container_name}/{source_file_path}")
# Target
target_container_name = "<destination container name>"target_file_path = "<destination text file name>"
copied_blob = blob_service_client.get_blob_client(target_container_name, target_file_path)
copied_blob.start_copy_from_url(source_blob)
Thus the file got uploaded and copied from container to another. Thanks for reading my blog!