【问题标题】:Use Python to create multiple containers with specific content使用 Python 创建具有特定内容的多个容器
【发布时间】:2019-08-08 05:04:51
【问题描述】:

我的 Blob 存储中有一个包含大约 200k 图像的容器。我想用 Python 编写一个脚本,将 20k 的这些图像批量复制到名为 imageset1、imageset2、...、imageset20 之类的新容器中(最后一个容器中的图像少于 20k,这很好)。

到目前为止,我有以下内容:

from azure.storage.blob import BlockBlobService 
from io import BytesIO from shutil
import copyfileobj 
with BytesIO() as input_blob: 
   with BytesIO() as output_blob:
block_blob_service = BlockBlobService(account_name='my_account_name', account_key='my_account_key')

# Download as a stream 
block_blob_service.get_blob_to_stream('mycontainer', 'myinputfilename', input_blob) 


# Here is where I want to chunk up the container contents into batches of 20k


# Then I want to write the above to a set of new containers using, I think, something like this... 
block_blob_service.create_blob_from_stream('mycontainer', 'myoutputfilename', output_blob)

这是将容器的内容分块并将结果写入新容器,我不知道该怎么做。有人可以帮忙吗?

【问题讨论】:

  • 所有发布的都是程序描述。请参阅 Jon Skeet 的 How to Ask 帮助页面和 The perfect question 博客文章。我们无法确定您想从我们这里得到什么。请edit您的帖子包含一个我们可以回答的有效问题。提醒:通过访问help center,确保您知道这里的主题是什么;要求我们为您编写程序、建议和外部链接都是题外话。
  • 是否有任何模式可以对这些图像进行分类?按名称还是按时间带等?
  • 彼得,不,没有。图片格式如下:RBG4906_1.jpg, RBG4906_2.jpg(所以有两张略有不同的图片,后缀为1或2)。图片名称中的数字不是连续的,所以据我所知没有模式。
  • @JassiL 所以你只想将它们移动到具有平均数字大小的不同容器中。对吗?

标签: python-3.x azure azure-blob-storage


【解决方案1】:

这是我实现您需求的示例代码,它适用于我的容器。

from azure.storage.blob.baseblobservice import BaseBlobService

account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<the source container name>'

blob_service = BaseBlobService(
    account_name=account_name,
    account_key=account_key
)

blobs = blob_service.list_blobs(container_name)

# The target container index starts with 1
container_index = 1
# The blob number in new container, such as 3 in my testing 
num_per_container = 3
count = 0
# The prefix of new container name
prefix_of_new_container = 'imageset'
flag_of_new_container = False

for blob in blobs:
    if flag_of_new_container == False:
        flag_of_new_container = blob_service.create_container("%s%d" % (prefix_of_new_container, container_index))
    print(blob.name, "%s%d" % (prefix_of_new_container,container_index))
    blob_service.copy_blob("%s%d" % (prefix_of_new_container, container_index), blob.name, "https://%s.blob.core.windows.net/%s/%s" % (account_name, container_name, blob.name))
    count += 1
    if count == num_per_container:
        container_index += 1
        count = 0
        flag_of_new_container = False

注意:我只使用BaseBlobService,因为它足以满足您的需求,即使对于 AppendBlob 或 PageBlob 也是如此。此外,您可以使用BlockBlobService 代替它。

【讨论】:

  • 哦,哇——这是一个非常棒且非常有帮助的回复!谢谢:-)
猜你喜欢
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2011-09-09
  • 2023-02-09
  • 2019-08-13
  • 2021-09-21
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多