I was taking a look at this documentation from Microsoft.
On which is said that to create a container, we should use
# Create a unique name for the container
container_name = str(uuid.uuid4())
# Create the container
container_client = blob_service_client.create_container(container_name)
I think its easy, so I try to use it in my code. as follow
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
print(container['name'], container['metadata'])
# print("==========================")
container_client = client.get_container_client(container.name)
# print(container_client)
blobs_list = container_client.list_blobs()
for blob in blobs_list:
print(blob.name)
print("==========================")
# # ============================= TARGET =======================================
# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = source_container_name
target_blob_name = blob.name
target_destination_blob = str(container['name'])
print(target_destination_blob)
container_client = BlobServiceClient.create_container(target_destination_blob)
# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container = ContainerClient.from_connection_string(target_connection_string, target_destination_blob)
# Create new blob and start copy operation.
# new_blob = client.get_blob_client(destination_container_name, blob_name)
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
print(source_blob.url)
The purpose is loop through all containers in storage A, and copy all containers and blobs and send them to storage B, recreating the same container name and blobs (basically a backup system)
At this point, as the containers does not exists in storage B, I need to make sure that I am checking if those storage does not exists
, and if this is true to create them, otherwise it can process the copy.
But for now I was just trying to create a container with the above code, but I get this error.
TypeError: create_container() missing 1 required positional argument: 'name'
As the name should be a string. I tried to cast to str but I get the same exact error.
According to the documentation, the parameter should be a string, but that does not seems to work.
UPDATE:
This is my updated version of the code:
from typing import Container
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient
from azure.storage.blob import ResourceTypes, AccountSasPermissions
from azure.storage.blob import generate_account_sas
from datetime import *
#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
source_container_name = '' # Name of container which has blob to be copied
blob_name = '' # Name of the blob you want to copy
destination_container_name = '' # Name of container where blob will be copied
# Create client
client = BlobServiceClient.from_connection_string(connection_string)
# Create sas token for blob
sas_token = generate_account_sas(
account_name = client.account_name,
account_key = account_key,
resource_types = ResourceTypes(object=True, container=True),
permission= AccountSasPermissions(read=True,list=True),
# start = datetime.now(),
expiry = datetime.utcnow() + timedelta(hours=4) # Token valid for 4 hours
)
# Create blob client for source blob
source_blob = BlobClient(
client.url,
container_name = source_container_name,
blob_name = blob_name,
credential = sas_token
)
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
print(container['name'], container['metadata'])
# print("==========================")
container_client = client.get_container_client(container.name)
# print(container_client)
blobs_list = container_client.list_blobs()
for blob in blobs_list:
print(blob.name)
print("==========================")
# # ============================= TARGET =======================================
# Target Client
target_connection_string = ''
target_account_key = ''
source_container_name = source_container_name
target_blob_name = blob.name
target_destination_blob = str(container['name'])
print(target_destination_blob)
# Create target client
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container = ContainerClient.from_connection_string(target_connection_string, target_destination_blob)
container_client = target_client.create_container(target_destination_blob)
# Create new blob and start copy operation.
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
My expectation for this behaviour, is too loop over all containers and blobs in SOURCE
and create a exact replica of containers and blobs in TARGET
Once of the issue, is if I run the code the first time, it create only the first storage, but no blob, if I run it the second time, I get the error
ErrorCode:ContainerAlreadyExists