Python: How to move or copy Azure Blob from one container to another

眉间皱痕 提交于 2019-12-18 16:10:44

问题


I am using Microsoft Azure SDK for Python in project. I want to move or copy Blob from one container to another. for exmaple

https://demostorage.blob.core.windows.net/image-container/pretty.jpg

I want to move this blob to

https://demostorage.blob.core.windows.net/demo-container/

I have found following method in python SDK but unable to understand it.

def copy_blob(self, container_name, blob_name,...):

How can I do this? Thank you


回答1:


I have done in this way.

from azure.storage.blob import BlobService

def copy_azure_files(self):

        blob_service = BlobService(account_name='account_name', account_key='account_key')
        blob_name = 'pretty.jpg'
        copy_from_container = 'image-container'
        copy_to_container = 'demo-container'

        blob_url = blob_service.make_blob_url(copy_from_container, blob_name)
        # blob_url:https://demostorage.blob.core.windows.net/image-container/pretty.jpg

        blob_service.copy_blob(copy_to_container, blob_name, blob_url)

        #for move the file use this line
        blob_service.delete_blob(copy_from_container, blob_name)

I have not found any Blob Move method yet. So I have used the copy method and then execute Blob function.

This is my solution. If you have better way to handle all this please share with me.

Note: I have not used any custom method all these methods are included in SDK.




回答2:


Using the most current Azure Blob Storage SDK.

from azure.storage.blob import BlockBlobService

account_name = "demostorage"
account_key = "lkjASDRwelriJfou3lkjksdfjLj349u9LJfasdjfs/dlkjfjLKSjdfi8ulksjdfAlkjsdfkL762FDSDFSDAfju==" 

source_container_name = "image-container"
source_file_path = "pretty.jpg"

target_container_name = "demo-container"
target_file_path = "pretty_copy.jpg"

service = BlockBlobService(account_name, account_key)

service.copy_blob(
    target_container_name, 
    target_file_path, 
    f"https://{account_name}.blob.core.windows.net/{source_container_name}/{source_file_path}",
)


来源:https://stackoverflow.com/questions/32500935/python-how-to-move-or-copy-azure-blob-from-one-container-to-another

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