gpt4 book ai didi

python - 从远程 URL 将 Azure Blob 复制为 BlockBlob

转载 作者:行者123 更新时间:2023-12-03 04:49:28 27 4
gpt4 key购买 nike

我正在使用azure-sdk-for-python BlobClient start_copy_from_url 将远程文件复制到本地存储。

但是,文件最终总是作为 AppendBlob 而不是 BlockBlob。我不明白如何强制目标 BlockType 为 BlockBlob。

connection_string = "connection string to my dest blob storage account"
container_name = "myContainerName"
dest_file_name = "myDestFile.csv"
remote_blob_url = "http://path/to/remote/blobfile.csv"

client = BlobServiceClient.from_connection_string(connection_string)
dest_blob = client.get_blob_client(container_name, dest_file_name)
dest_blob.start_copy_from_url(remote_blob_url)

最佳答案

一旦创建 blob 类型,就无法更改它。请参阅 Copy Blob From URL REST API ,没有 blob-types header 。

您可以引用我的代码从附加 blob 创建 block blob:

from azure.storage.blob import BlobPermissions
from datetime import datetime, timedelta
from azure.storage.blob import BlockBlobService
import requests
from io import BytesIO

account_name = "***"
account_key = "***"
container_name = "test"
blob_name = "test2.csv"

block_blob_service = BlockBlobService(account_name, account_key)

sas_token = block_blob_service.generate_blob_shared_access_signature(container_name, blob_name,
permission=BlobPermissions.READ,
expiry=datetime.utcnow() + timedelta(hours=1))
blob_url_with_sas = block_blob_service.make_blob_url(container_name, blob_name, sas_token=sas_token)

r = requests.get(blob_url_with_sas, stream=True)
block_blob_service.create_blob_from_stream("test", "jay.block", stream=BytesIO(r.content))

enter image description here

关于python - 从远程 URL 将 Azure Blob 复制为 BlockBlob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58925500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com