【问题标题】:Upload file from URL to Microsoft Azure Blob Storage将文件从 URL 上传到 Microsoft Azure Blob 存储
【发布时间】:2019-04-10 05:06:39
【问题描述】:

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

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

如何为远程 URL 执行此操作?

【问题讨论】:

    标签: python azure url upload


    【解决方案1】:

    好的,补充一点,因为我看到这个脚本时很困惑。

    您可以在 portal.azure.com 上找到有关 account_name 和 account_key 的信息:

    资源组 --> 选择 Blob 存储所在的资源组 --> 选择存储帐户 --> 单击左侧面板中的访问密钥。

    account_name=存储帐户名称(例如:mybackupstorage)

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

    【讨论】:

      【解决方案2】:

      您可以利用 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()
      

      【讨论】:

      • 确实,感谢它的工作相对较小的文件。它适用于大文件吗?例如,100GB。我尝试这样做,文件已部分上传到 storege。
      【解决方案3】:

      你可以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()
      

      测试结果如下:

      【讨论】:

      • 其实这不正确。可以使用复制 blob 从任何可公开访问的 URL 创建块 blob。请在下面查看我的答案。
      最近更新 更多