将文件从URL上传到Microsoft Azure Blob存储

Lew*_*ski 5 python url upload azure

从本地路径(从我的计算机)上传文件不是问题。但是,我没有找到如何从特定的URL上传。

如果可能的话-需要使用Python解决方案。只有本地文件的文档https://docs.microsoft.com/zh-cn/azure/storage/blobs/storage-quickstart-blobs-python

如何针对远程URL执行此操作?

Gau*_*tri 6

您可以利用async copy blob功能从可公开访问的URL中创建Blob。请参见下面的示例代码:

from azure.storage.blob import BlockBlobService, PublicAccess
from azure.storage.blob.models import Blob

def run_sample():
    block_blob_service = BlockBlobService(account_name='your_name', account_key='your_key')
    container_name ='t1s'

    block_blob_service.copy_blob(container_name,'remoteURL.pdf','https://media.readthedocs.org/pdf/azure-storage/v0.20.3/azure-storage.pdf')


# Main method.
if __name__ == '__main__':
    run_sample()
Run Code Online (Sandbox Code Playgroud)


Iva*_*ang 2

就可以了first download the file as stream,然后调用该方法create_blob_from_stream

以下是演示代码:

from azure.storage.blob import BlockBlobService, PublicAccess
from azure.storage.blob.models import Blob
import requests

def run_sample():
    block_blob_service = BlockBlobService(account_name='your_name', account_key='your_key')
    container_name ='t1s'

    response = requests.get('https://media.readthedocs.org/pdf/azure-storage/v0.20.3/azure-storage.pdf',stream=True)       
    block_blob_service.create_blob_from_stream(container_name,'remoteURL.pdf',response.raw)


# Main method.
if __name__ == '__main__':
    run_sample()
Run Code Online (Sandbox Code Playgroud)

测试结果如下: 在此输入图像描述