![](/img/trans.png)
[英]Problem to upload a file from local to Azure Blob Storage in python
[英]Upload file from URL to Microsoft Azure Blob Storage
从本地路径(从我的电脑)上传文件不是问题。 但是,我没有找到如何从特定 URL 上传。
如果可能的话 - 需要 Python 解决方案。 只有本地文件的文档https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python
如何为远程 URL 执行此操作?
您可以利用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()
您可以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()
好的,补充一个信息,因为当我看到这个脚本时我很困惑。
您可以在 portal.azure.com 上找到 account_name 和 account_key 的信息:
资源组 --> 选择 Blob 存储所在的资源组 --> 选择存储帐户 --> 单击左侧面板中的访问密钥。
account_name=存储帐户名称(例如:mybackuptorage)
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.