自动将本地目录中的文件上载到Azure Storage Explorer blob存储

时间:2018-04-27 23:03:18

标签: python azure azure-storage azure-storage-blobs azure-webjobs

我编写了Python代码,用于将文件从本地目录上传到Azure Storage Explorer中的blob容器,现在我正在尝试自动执行此操作(即如果目录中有文件,则会自动上传)。 有什么方法可以做到这一点吗?

import os
from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContentSettings


accountName = "accountName"
ContainerSAS = "SAS_Key"
containerName = "containerName"

# Create reference to container using account name and SAS key
try:
    sas_service = BlockBlobService(account_name=accountName, 
    sas_token=ContainerSAS)
except Exception as e:
    print("Error during SAS service creation. Details: {0}".format(e))
print("Created SAS service with account {0}".format(accountName))

# Upload files to container from path
# directory_path = "< path to your directory >"
directory_path = "F:/dat_files_test"
for filename in os.listdir(directory_path):
    print(filename)
    blobName = filename
    localFile = directory_path + "/" + filename

    try:
        sas_service.create_blob_from_path(
        containerName,
        blobName,
        localFile,
        content_settings=ContentSettings(content_type='DPS/dat')
        )
    except Exception as e:
        print("Error during blob uploading. Details: {0}".format(e))
print("All files uploaded")

1 个答案:

答案 0 :(得分:0)

如果我理解了这个问题,您希望不断检查文件夹中是否存在文件,如果存在,请对文件执行某些操作(例如发送到Azure)。

如果是这种情况,那么Scheduler应该会帮助你。