python - 自动将本地目录中的文件上传到 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")
解决方案
如果我理解这个问题,您希望不断检查文件夹中是否存在文件,如果存在,请对文件执行某些操作(例如发送到 Azure)。
如果是这种情况,那么调度程序应该可以帮助您。
推荐阅读
- powershell - 在 PowerShell 选择对象中未选择列
- android - 模块可以使用包含在另一个模块中的视图类而不依赖它。如何?
- javascript - 在 Javascript 中使用 googleapis 发布我的 youtube 频道的新描述
- c - 在 C 中使用 cot(x)
- python - 在词典的帮助下制作标记的词性语料库
- python - 对象不包括列表理解中的方法
- sql-server - 由于带有 OR 条件的 LEFT JOIN,SQL 查询极慢
- python - 形状与 Keras 功能模型和 VGG16 模型不兼容
- c++ - (C++) 你如何显示一个包含 Char 和 Int 值的二维数组?
- html - 在日历中保存事件