简体   繁体   English

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

[英]Upload file from URL to Microsoft Azure Blob Storage

It is not a problem to upload file from local path (from my computer).从本地路径(从我的电脑)上传文件不是问题。 However, I didn't find how to upload from specific URL.但是,我没有找到如何从特定 URL 上传。

If it possible - solution in Python is needed.如果可能的话 - 需要 Python 解决方案。 There is documentation only for local files https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python只有本地文件的文档https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

How to do this for remote URL?如何为远程 URL 执行此操作?

You can make use of async copy blob functionality to create a blob from a publicly accessible URL.您可以利用async copy blob功能从可公开访问的 URL 创建 blob。 Please see sample code below:请参阅下面的示例代码:

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()

You can first download the file as stream , then call the method create_blob_from_stream .您可以first download the file as stream ,然后调用create_blob_from_stream方法。

The following is a demo code:下面是一个演示代码:

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()

Test result as below:测试结果如下: 在此处输入图片说明

Oke, one additional information because I was confused when I saw this script.好的,补充一个信息,因为当我看到这个脚本时我很困惑。

Information for account_name and account_key you can find on portal.azure.com:您可以在 portal.azure.com 上找到 account_name 和 account_key 的信息:

Resource Groups --> Select Resource Group where is Blob Storage --> Select Storage Account --> Click on Access Key from left panel.资源组 --> 选择 Blob 存储所在的资源组 --> 选择存储帐户 --> 单击左侧面板中的访问密钥。

account_name=Storage account name (like: mybackupstorage) account_name=存储帐户名称(例如:mybackuptorage)

account_key=Key (like: ihwIKU@Hsniq87dbki*&qlos8ejuwa3ox7w4rykwij7ryx83deozd) account_key=Key(例如:ihwIKU@Hsniq87dbki*&qlos8ejuwa3ox7w4rykwij7ryx83deozd)

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='', account_key='')
    container_name ='container-name'

    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM