gpt4 book ai didi

python - 如何使用python将文件分块传输到azure blob存储而不写入文件

转载 作者:行者123 更新时间:2023-12-03 01:09:29 25 4
gpt4 key购买 nike

我需要将文件从 Google 云存储传输到 Azure Blob 存储。

Google 提供了一个代码片段,用于将文件下载到字节变量,如下所示:

# Get Payload Data
req = client.objects().get_media(
bucket=bucket_name,
object=object_name,
generation=generation) # optional
# The BytesIO object may be replaced with any io.Base instance.
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, req, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
if status:
print 'Download %d%%.' % int(status.progress() * 100)
print 'Download Complete!'
print fh.getvalue()

我可以通过更改 fh 对象类型来修改它以存储到文件,如下所示:

fh = open(object_name, 'wb')

然后我可以使用 blob_service.put_block_blob_from_path 上传到 Azure Blob 存储。

我想避免在进行传输时写入计算机上的本地文件。

我收集 Google 的代码片段将数据一次加载到 io.BytesIO() 对象中。我认为我应该使用它一次向 blob 存储写入一个 block 。

我尝试将整个内容读入内存,然后使用 put_block_blob_from_bytes 上传,但出现内存错误(文件可能太大(~600MB)。

有什么建议吗?

最佳答案

根据blobservice.py for Azure Storage的源码和 BlobReader for Google Cloud Storage ,您可以尝试使用 Azure 函数 blobservice.put_block_blob_from_file 从 GCS 类 blobreader 具有函数 read 作为流写入流,请见下文。

enter image description here

enter image description here

所以引用https://cloud.google.com/appengine/docs/python/blobstore/#Python_Using_BlobReader中的代码,您可以尝试如下操作。

from google.appengine.ext import blobstore
from azure.storage.blob import BlobService

blob_key = ...
blob_reader = blobstore.BlobReader(blob_key)

blob_service = BlobService(account_name, account_key)
container_name = ...
blob_name = ...
blobservice.put_block_blob_from_file(container_name, blob_name, blob_reader)

关于python - 如何使用python将文件分块传输到azure blob存储而不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264428/

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