Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Python创建具有特定内容的多个容器_Python 3.x_Azure_Azure Storage Blobs - Fatal编程技术网

Python 3.x 使用Python创建具有特定内容的多个容器

Python 3.x 使用Python创建具有特定内容的多个容器,python-3.x,azure,azure-storage-blobs,Python 3.x,Azure,Azure Storage Blobs,我的blob存储中有一个容器,其中包含大约200k个图像。我想用Python编写一个脚本,将这些20k的图像批量复制到称为imageset1、imageset2、…、imageset20之类的新容器中(最后一个容器中的图像将少于20k,这很好) 到目前为止,我有以下几点: from azure.storage.blob import BlockBlobService from io import BytesIO from shutil import copyfileobj with Byte

我的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)

它是把一个容器的内容分块,然后把结果写到新的容器中,我不知道怎么做。有人能帮忙吗?

这是我的示例代码,可以满足您的需要,并且可以在我的容器上使用

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
从azure.storage.blob.baseblobservice导入baseblobservice
帐户名称=“”
帐户密钥=“”
容器名称=“”
blob_服务=BaseBlobService(
账户名称=账户名称,
帐户密钥=帐户密钥
)
blob=blob\u服务。列出blob(容器名称)
#目标容器索引以1开头
容器索引=1
#新容器中的blob编号,如我的测试中的3
每个容器的数量=3
计数=0
#新容器名称的前缀
_new_容器的前缀_='imageset'
新容器的标记=False
对于blob中的blob:
如果新容器的标志=False:
标记\u新\u容器的\u=blob\u服务。创建\u容器(“%s%d”%(前缀\u新\u容器,容器索引))
打印(blob.name,“%s%d”%(新容器的前缀,容器索引))
blob_服务.copy_blob(“%s%d”%(新容器的前缀,容器索引),blob.name,“https://%s.blob.core.windows.net/%s/%s”%(帐户名称,容器名称,blob.name))
计数+=1
如果计数=每个容器的数量:
容器索引+=1
计数=0
新容器的标记=False

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

所发布的只是一个程序说明。请参阅Jon Skeet的帮助页面和博客文章。我们不能确定你想从我们这里得到什么。请在您的帖子中加入我们可以回答的有效问题。提醒:通过访问网站,确保您知道这里的主题;要求我们为您编写程序、建议和外部链接都是离题的。是否有任何模式可以对这些图像进行分类?按名字还是按时间陷阱等等?彼得,不,没有。图像的格式如下:RBG4906_1.jpg、RBG4906_2.jpg(因此同一事物有两个稍有不同的图像,后缀为1或2)。图像名称中的数字不是连续的,所以据我所知没有模式。@JassiL所以您只想将它们移动到具有平均数字大小的不同容器中。是吗?哦,哇-这是一个非常好而且非常有用的回答!谢谢:-)