自动将本地目录中的文件上传到 Azure 存储资源管理器 blob 存储

Automatically upload files in local directory to Azure Storage Explorer blob storage

我编写了 Python 代码以将文件从本地目录上传到 Azure 存储资源管理器中的 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")

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

如果是这样,那么Scheduler应该能帮到你。