简体   繁体   English

使用带有 Python V12 SDK 的 BlobServiceClient 将本地文件夹上传到 Azure Blob 存储

[英]Upload local folder to Azure Blob Storage using BlobServiceClient with Python V12 SDK

Summarize the problem:总结问题:

I am trying to upload a local folder to Blob Storage using BlobServiceClient with Python.我正在尝试使用带有 Python 的 BlobServiceClient 将本地文件夹上传到 Blob 存储。 Some of the questions here and here do not work because create_blob_from_path() doesn't work in V12 SDK and I wouldn't want to go back to older version. 这里这里的一些问题不起作用,因为create_blob_from_path()在 V12 SDK 中不起作用,我不想 go 回到旧版本。

What I've tried:我试过的:

I am using os.walk for local directory but missing the most important part like a function similar to create_blob_from_path() .我将os.walk用于本地目录,但缺少最重要的部分,例如类似于create_blob_from_path()的 function 。

Sample code:示例代码:

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)

Any help is much appreciated and I will take a look at Azure Github to see if anything useful there.非常感谢任何帮助,我将看看 Azure Github 看看那里是否有任何有用的东西。

You can also use the code below(Assume the local folder is in D:\aaa , please feel free to modify the code as per your need):您也可以使用下面的代码(假设本地文件夹在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**")

OK.好的。 With the help of source code from Git , I was able to figure out the solution and I am posting here for future references.Git 的源代码的帮助下,我能够找出解决方案,并在此处发布以供将来参考。 I was very confused about dest variable and was even looking for container's url to give an upload path.我对dest变量感到非常困惑,甚至在寻找容器的 url来提供上传路径。 It's actually been taken care of in upload_dir function.它实际上已在upload_dir function 中得到处理。 Any other suggestions are also welcomed.也欢迎任何其他建议。

Sample code:示例代码:

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

base_file_path = '/path/to/your/local/directory/'
# target_folder is the subfolder under container_name 
target_folder = 'xyz' 
 
connect_str = '1q2w3e4r5t6y7u8i9o0p'
container_name = 'abc'

def upload_file(source, dest):
    
    print(f'Uploading {source} to {dest}')
    with open(source, 'rb') as data:
      client.upload_blob(name=dest, data=data)

def upload_dir(source, dest):

    prefix = '' if dest == '' else dest + '/'
    prefix += os.path.basename(source) + '/'
    for root, dirs, files in os.walk(source):
        for name in files:
            dir_part = os.path.relpath(root, source)
            dir_part = '' if dir_part == '.' else dir_part + '/'
            file_path = os.path.join(root, name)
            blob_path = prefix + dir_part + name
            upload_file(file_path, blob_path)
try:
    source = base_file_path + target_folder
    dest = '' # dest is the target folder name  
    service_client = BlobServiceClient.from_connection_string(connect_str)
    client = service_client.get_container_client(container_name)
except Exception as ex:
    print('Exception:')
    print(ex)

if __name__ == '__main__':
    upload_dir(source=source, dest=dest)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 存储 Blob (v12) Python API:在获取租约时重试 - Azure Storage Blob (v12) Python API: Retry while acquiring lease 使用适用于 Python 的 Azure 存储 SDK 将多个文件从文件夹上传到 Azure Blob 存储 - Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python 使用 python 将图像上传到 azure blob 存储 - Upload image to azure blob storage using python 如何使用 python sdk 将 blob 上传到带有子目录的 azure 存储容器中? - How to upload a blob into azure storage container with sub directories using the python sdk? 如何使用 Python 递归地将文件夹上传到 Azure blob 存储 - How to recursively upload folder to Azure blob storage with Python 如何将a文件夹上传到Azure blob存储中,同时保留Python中的结构? - How to upload the a folder into Azure blob storage while preserving the structure in Python? 将文件从本地上传到 python 中的 Azure Blob 存储的问题 - Problem to upload a file from local to Azure Blob Storage in python 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python Azure Python SDK:BlobServiceClient 与 BlobClient? - Azure Python SDK: BlobServiceClient vs. BlobClient? 从“azure.storage.blob”导入“BlobServiceClient”时出错 - error importing 'BlobServiceClient' from 'azure.storage.blob'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM