5

I can upload single file to Azure blob storage with Python. But for a folder with multiple folders containing data, is there a way I can try to upload the whole folder with same directory to Azure?

Say I have

FOLDERA
------SUBFOLDERa
----------filea.txt
----------fileb.txt
------SUBFOLDERb
------SUBFOLDERc

I want to put this FOLDERA as above structure to Azure. Any hints?

2 Answers 2

2

@Krumelur is almost right, but here I want to give a working code example, as well as explain some folders are not be able to upload to azure blob storage.

1.Code example:

from azure.storage.blob import BlockBlobService,PublicAccess
import os

def run_sample():
    account_name = "your_account_name"
    account_key ="your_account_key"
    block_blob_service = BlockBlobService(account_name, account_key)
    container_name ='test1'

    path_remove = "F:\\"
    local_path = "F:\\folderA"

    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)
                block_blob_service.create_blob_from_path(container_name,file_path_on_azure,file_path_on_local)            

# Main method.
if __name__ == '__main__':
    run_sample()

2.You should remember that any empty folder can not be created / uploaded to azure blob storage, since there is no real "folder" in azure blob storage. The folder or directory is just a part of the blob name. So without a real blob file like test.txt inside a folder, there is no way to create/upload an empty folder. So in your folder structure, the empty folder SUBFOLDERb and SUBFOLDERc are not be able to upload to azure blob storage.

The test result is as below, all the non-empty folders are uploaded to blob storage in azure:

enter image description here

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

6 Comments

Thanks a lot! This is where I were stuck: replace(path_remove,"")
Glad it helps:)
Is there a similar way of uploading folder to Blob using BlobServiceClient? @IvanYang
@liamsuma, I think it's possible. Could you please raise an issue and then let me know the url?
Thanks for your time and response. I will post a question with its url linked here
|
0

There is nothing built in, but you can easily write that functionality in your code (see os.walk).

Another option is to use the subprocess module to call into the azcopy command line tool.

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.