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

时间:2019-03-17 19:28:23

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

我在一个Blob存储中有一个约有200k图像的容器。我想用Python编写一个脚本,将这些图像的20k批次复制到新容器中,这些容器称为imageset1,imageset2,...,imageset20(最后一个容器中的图像少于20k,这很好)。 / p>

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

<Midterm>
    <StudentData>
        <Gnumber>   G12654312   </Gnumber>
        <ResidenceStatus>   In-state    </ResidenceStatus>
        <FirstName> ALBERTO </FirstName>
        <MiddleInitial> L   </MiddleInitial>
        <LastName>  SMITH   </LastName>
        <DOB>   2/3/1981    </DOB>
        <ProgramName>   MS Health Informatics   </ProgramName>
        <Concentration> Data analytics  </Concentration>
        <StartYear> 2014    </StartYear>
        <course> 
            <Department>    HAP </Department>
            <CatalogNumber> 463 </CatalogNumber>
            <Semester>  SPRING  </Semester>
            <Year>  2014    </Year>
            <LetterGrade>   B+  </LetterGrade>
        </course>
    </StudentData>
</Midterm>

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

1 个答案:

答案 0 :(得分: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代替它。