Azure blob:使用Python中的循环上传目录内容

时间:2016-04-29 10:00:21

标签: python azure azure-storage-blobs

我想使用循环将一些文件上传到blob容器。 iefiles xaa,xab,xac

我尝试过以下循环但没有成功

import string    
for i in string.lowercase[0:2]:
        block_blob_service.create_blob_from_path(
            'my_container',
            'xa%s' % i,
            '/pathtomylocalfile/xa%s' % i)

虽然这有效

block_blob_service.create_blob_from_path(
            'my_container',
            'xaa',
            '/pathtomylocalfile/xaa')

3 个答案:

答案 0 :(得分:1)

否则,您可以尝试使用format函数格式化字符串:

...
    block_blob_service.create_blob_from_path(
            'my_container',
            'xa{}'.format(i),
            '/pathtomylocalfile/xa{}'.format(i))

答案 1 :(得分:0)

奇怪的是,它似乎可以替代

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir('/mylocaldirectory/') if isfile(join('/mylocaldirectory/', f))]

for i in onlyfiles:
    block_blob_service.create_blob_from_path(
        'mycontainer',
        '%s' % i,
        '/mylocaldirectory/%s' % i)

答案 2 :(得分:0)

    import os

    FILE_PATH1="/pathtomylocalfile"
    container_name ='my_container'

    local_path=os.path.expanduser(FILE_PATH1)

    for root, dirs, allfiles in os.walk(FILE_PATH1):
        print(root)

        for f in allfiles:
            print(os.path.join(root, f))

            local_file_name = f

            full_path_to_file =os.path.join(root, local_file_name)


            if full_path_to_file.find(".") != False:
                print("Temp file = " + full_path_to_file)
                print("\nUploading to Blob storage as blob " + local_file_name)


                block_blob_service.create_blob_from_path(container_name, full_path_to_file[1:], full_path_to_file)