Search code examples
pythonazureazure-blob-storageazcopy

How to upload files to azure blob storage?


I need to upload files to azure blob container every day from local system. I use azcopy with sas for doing it. But what i encountered is that SAS for container keep changing on every refresh. So is there any better way i can upload files using python or azcopy. Or is there any way to get the SAS toke from the azure without login and pass that SAS token to azcopy command? as of now i use this command from azcopy

.\azcopy "Sourcefilepath" "Destblobpath?SAS_Token" --recurcive=true

Each day i should login to azure get the SAS token and pass for the above command. i tried .\azcopy login and i get login successful, but i cant send files with

.\azcopy "Sourcepath" "Destpath"

Don't know where i'm doing wrong.


Solution

  • If you're using python, I would suggest using the azure python sdk to do your uploading. You can see more from this example here...

    https://github.com/Azure-Samples/storage-blobs-python-quickstart/blob/master/example.py

    It can be as quick as this (from the quick start docs : https://learn.microsoft.com/en-us/python/api/overview/azure/storage?view=azure-python) to interface with your azure blob storage account. Just put in some logic to loop through a directory recursively and upload each file.

    First make sure to pip install the required packages, and then grab your account name (blob storage name) and your access key from the portal...plug them in and you're good to go.

    pip install azure-storage-blob azure-mgmt-storage
    

    Then write some python code here...

    from azure.storage.blob import BlockBlobService, PublicAccess
    
    blob_service = BlockBlobService('[your account name]','[your access key]')
    
    blob_service.create_container(
        'mycontainername',
        public_access=PublicAccess.Blob
    )
    
    blob_service.create_blob_from_bytes(
        'mycontainername',
        'myblobname',
        b'hello from my python file'
    )
    
    print(blob_service.make_blob_url('mycontainername', 'myblobname'))
    

    This should get you going in the right direction pretty quickly.