I have the following script to copy blobs from one container to another.
#================================ SOURCE ===============================
# Source Client
connection_string = '' # The connection string for the source container
account_key = '' # The account key for the source container
# source_container_name = 'newblob' # Name of container which has blob to be copied
table_service_out = TableService(account_name='', account_key='')
table_service_in = TableService(account_name='', account_key='')
# Create client
client = BlobServiceClient.from_connection_string(connection_string)
client = BlobServiceClient.from_connection_string(connection_string)
all_containers = client.list_containers(include_metadata=True)
for container in all_containers:
# 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
)
print("==========================")
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:
# Create blob client for source blob
source_blob = BlobClient(
client.url,
container_name = container['name'],
blob_name = blob.name,
credential = sas_token
)
target_connection_string = ''
target_account_key = ''
source_container_name = container['name']
target_blob_name = blob.name
target_destination_blob = container['name'] today
print(target_blob_name)
# print(blob.name)
target_client = BlobServiceClient.from_connection_string(target_connection_string)
try:
container_client = target_client.create_container(target_destination_blob)
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
print("COPY TO")
print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")
except:
# 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)
print("COPY TO")
print(f"EXCEPT: saving blob {target_blob_name} into {target_destination_blob} ")
This exact same code working just fine yesterday, but today when I run it again, I got the following error.
Traceback (most recent call last):
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 292, in create_container
return self._client.container.create( # type: ignore
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_generated/operations/_container_operations.py", line 132, in create
map_error(status_code=response.status_code, response=response, error_map=error_map)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 105, in map_error
raise error
azure.core.exceptions.ResourceExistsError: Operation returned an invalid status 'The specified container already exists.'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/user/Desktop/AzCopy/blob.py", line 68, in <module>
container_client = target_client.create_container(target_destination_blob)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 83, in wrapper_use_tracer
return func(*args, **kwargs)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_blob_service_client.py", line 518, in create_container
container.create_container(
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/tracing/decorator.py", line 83, in wrapper_use_tracer
return func(*args, **kwargs)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 300, in create_container
process_storage_error(error)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_shared/response_handlers.py", line 150, in process_storage_error
error.raise_with_traceback()
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 247, in raise_with_traceback
raise super(AzureError, self).with_traceback(self.exc_traceback)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_container_client.py", line 292, in create_container
return self._client.container.create( # type: ignore
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/storage/blob/_generated/operations/_container_operations.py", line 132, in create
map_error(status_code=response.status_code, response=response, error_map=error_map)
File "/Users/user/miniforge3/lib/python3.9/site-packages/azure/core/exceptions.py", line 105, in map_error
raise error
azure.core.exceptions.ResourceExistsError: The specified container already exists.
RequestId:
Time:2021-09-29T10:42:10.8775237Z
ErrorCode:ContainerAlreadyExists
Error:None
With the try and except I didn't had any problem until today. Is the any better option for checking if the container exists to do not try and create it again?
Thank you so much for any help you can provide.
UPDATE:
I updated my code with the if condition as follow:
target_client = BlobServiceClient.from_connection_string(target_connection_string)
container_client = target_client.get_container_client(target_destination_blob)
if (!container_client.exists())
container_client.create()
new_blob = target_client.get_blob_client(target_destination_blob, target_blob_name)
new_blob.start_copy_from_url(source_blob.url)
print("COPY TO")
print(f"TRY: saving blob {target_blob_name} into {target_destination_blob} ")
But the if (!container_client.exists()):
is highlighted as an error stating Expression Expected
CodePudding user response:
You can make use of exists
method on ContainerClient
to see if the container exists or not. If the container does not exist, exists
method would return false and you can then create the container. If it exists, you can simply continue with your code.
For example your code could be:
...
...
...
container_client = target_client.get_container_client(target_destination_blob)
if not container_client.exists():
print("container does not exist. create it")
container_client.create_container()
...
...
...