gpt4 book ai didi

python - 使用 BlobServiceClient 和 Python V12 SDK 将本地文件夹上传到 Azure Blob 存储

转载 作者:行者123 更新时间:2023-12-03 00:59:19 25 4
gpt4 key购买 nike

总结问题:

我正在尝试使用 BlobServiceClient 和 Python 将本地文件夹上传到 Blob 存储。一些问题herehere不起作用,因为 create_blob_from_path() 在 V12 SDK 中不起作用,而且我不想回到旧版本。

我尝试过的:

我使用 os.walk 作为本地目录,但缺​​少最重要的部分,例如类似于 create_blob_from_path() 的函数。

示例代码:

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, PublicAccess
import os

base_file_path = '/path/to/my/local/directory/'
connect_str = '1q2w3e4r5t6y'
container_name = 'abc'

try:
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = 'abc' # already created in Azure
container_client = blob_service_client.get_container_client(container_name)

upload_local_file_path = base_file_path + 'csv-summary-output' # input folder path

for root, subdir, local_file in os.walk(upload_local_file_path):
if local_file:
for name in local_file:
dir_part = os.path.relpath(root, upload_local_file_path)
file_path = os.path.join(root, name)
==> missing parts here
except Exception as ex:
print('Exception:')
print(ex)

非常感谢任何帮助,我将查看 Azure Github,看看那里是否有任何有用的东西。

最佳答案

您还可以使用下面的代码(假设本地文件夹位于D:\aaa,请根据您的需要随意修改代码):

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient,PublicAccess
import os

def run_sample():
conn_str="xxx"
container_name="xxx"

path_remove = "D:\\"
local_path = "D:\\aaa" #the local folder

service_client=BlobServiceClient.from_connection_string(conn_str)
container_client = service_client.get_container_client(container_name)

for r,d,f in os.walk(local_path):
if f:
for file in f:
file_path_on_azure = os.path.join(r,file).replace(path_remove,"")
file_path_on_local = os.path.join(r,file)

blob_client = container_client.get_blob_client(file_path_on_azure)

with open(file_path_on_local,'rb') as data:
blob_client.upload_blob(data)


if __name__ == '__main__':
run_sample()
print("**completed**")

关于python - 使用 BlobServiceClient 和 Python V12 SDK 将本地文件夹上传到 Azure Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63413832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com