首页 > 解决方案 > 自动将本地目录中的文件上传到 Azure 存储资源管理器 blob 存储

问题描述

我编写了 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")

标签: pythonazureazure-storageazure-blob-storageazure-webjobs

解决方案


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

如果是这种情况,那么调度程序应该可以帮助您。


推荐阅读