0

I need to create, upload and copy 100 blobs from one account storage to second with python script.

`

  import os, uuid
  from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__


#connect_str1 = os.getenv('AZURE_STORAGE_CONNECTION_STRING1')
connect_str2 ="DefaultEndpointsProtocol=https;AccountName=arielstorageaccount2;AccountKey=n+Bx[...]Q==;EndpointSuffix=core.windows.net"
#blob_service_client = BlobServiceClient.from_connection_string(connect_str)
blob_service_client2 = BlobServiceClient.from_connection_string(connect_str2)

# Create a unique name for the container
#container_name = str(uuid.uuid4())
container_name2 = str(uuid.uuid4())

# Create the container
#container_client = blob_service_client.create_container(container_name)
container_client2 = blob_service_client2.create_container(container_name2)


local_path = r"C:\Users\pavel\PycharmProjects\pythonProject1\data"
os.mkdir(local_path)

    # Create a file in the local data directory to upload and download
for i in range(100):
    local_file_name = str(uuid.uuid4()) + ".txt"
    upload_file_path = os.path.join(local_path, local_file_name)
    cwd = os.getcwd()
    print(cwd)
    # Write text to the file
    file = open(upload_file_path, 'w')
    file.write("Hello, World!")
    file.close()

    # Create a blob client using the local file name as the name for the blob
    blob_client = blob_service_client2.get_blob_client(container=container_name2, blob=local_file_name)

    print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

    # Upload the created file
    with open(upload_file_path, "rb") as data:
        blob_client.upload_blob(data)

`

I have a few problems : 1. It does create new containers but not upload BLOBS 2. The code doesn't do the loop 100 times 3. don't understand how to copy the BLOB from one account storage to another

2
  • The code doesn't do the loop 100 times - How many times does it loop? Do you see files created locally? Commented Oct 13, 2021 at 14:13
  • 1 time , yes locally I see them. Commented Oct 13, 2021 at 15:11

1 Answer 1

1

try with this code ,I tried in my system ,I tried with downloading all the blobs from container to local machine then uploading them to another storage

from azure.storage.blob import BlobServiceClient
import os
source_key = 'source_key'
source_account_name = 'sourceaccounttest1'
block_blob_service = BlobServiceClient(
    account_url=f'https://{source_account_name}.blob.core.windows.net/', credential=source_key)

des_key = 'des_accKey'
des_account_name = 'desaccount'
des_blob_service_client = BlobServiceClient(
    account_url=f'https://{des_account_name}.blob.core.windows.net/', credential=des_key)
#generator = block_blob_service.list_containers("testcopy")
source_container_client = block_blob_service.get_container_client(
    'testcopy')
des_container_client = des_blob_service_client.get_container_client(
    'testcopy')
generator =source_container_client.list_blobs("")

fp = open('local file path', 'ab')
i=1
for blob in generator:

    print(blob.name)
    print("HI")
    path_to_file = "localfilepath"+str(i)+".txt" 
    
    blob_client = source_container_client.get_blob_client(blob.name)
    with open(path_to_file, "wb") as my_blob:
        blob_data = blob_client.download_blob()
        blob_data.readinto(my_blob)
    i=i+1
    
path_remove = "C:\\"
local_path = "C:\\blobs" #the local folder
i=1
for r,d,f in os.walk(local_path):
        if f:
            for file in f:
                file_path_on_azure = os.path.join(r,file).replace(path_remove,"")
                file_path_on_local = os.path.join(r,file)
                file_names='doc'+str(i)
                blob_client = des_container_client.get_blob_client(file_names)
                i=i+1
                
                print(blob_client.blob_name)
                with open(file_path_on_local,'rb') as data:
                    blob_client.upload_blob(data)

OUTPUT

enter image description here

Locally saved inside blob folder

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.