How to upload files to azure blob storage?

走远了吗. 提交于 2021-01-28 19:00:45

问题


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.


回答1:


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://docs.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.




回答2:


The issue you're seeing for SAS is actually working as it's supposed to. SAS has an expiration date, and can be revoked at anytime you want. So you'll be required to use the new one as soon as it is available.

I'd recommend using the python SDK since it uses the Storage Key and Account name, which doesn't expire, except when a key is rotated. I wrote a few tools/Samples in python SDK which performs all actions: List, Upload and delete: https://github.com/adamsmith0016/Azure-storage

Feel free to clone and re-use any code.



来源:https://stackoverflow.com/questions/55300190/how-to-upload-files-to-azure-blob-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!