首页 > 解决方案 > 在 Python 中附加到 Blob 的问题

问题描述

如果 Blob 已经存在,我正在尝试追加到 Blob,但是从下面的代码中我只能创建一个文件,但不能追加到现有的 Blob。

filename = x + '.csv'
    file_system_client = service_client.get_file_system_client(file_system=date_time+"9")
    file_client = file_system_client.create_file(filename)
    local_file = open(filename, 'r') # Change the Path over here !!!
    file_contents = local_file.read()
    file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
    file_client.flush_data(len(file_contents))

我尝试使用下面的代码附加,但我认为我使用了来自 azure 的错误语法

file_system_client = service_client.get_file_system_client(file_system="test-data")
    # Get the Blob Names from the Container
    container_client = blob_service_client.get_container_client("test-data")
    blobs_list = container_client.list_blobs()
    # Check the Blob name is present or not
    for blob in blobs_list:
        if blob.name == sourceid + ".csv":
            flag = True
            break
    if flag:
        file_client = file_system_client.get_file_client(sourceid + ".csv")
    else:
        file_client = file_system_client.create_file(sourceid + ".csv")
    local_file = gzip.open(filename, 'r')  # Change the Path over here !!!
    file_contents = local_file.read()
    file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
    file_client.flush_data(len(file_contents))

标签: pythonazure

解决方案


通过以下代码片段解决的问题......终于得到了通过python附加在csv中的blob的语法......

flag = False
    blob_service_client = BlobServiceClient.from_connection_string(
        "DefaultEndpointsProtocol=https;AccountName=***********;AccountKey=*************;EndpointSuffix=core.windows.net")
    service_client = DataLakeServiceClient(
        account_url="{}://{}.dfs.core.windows.net".format("https", "********"),
        credential="************")
    file_system_client = service_client.get_file_system_client(file_system="test-data")
    # Get the Blob Names from the Container
    container_client = blob_service_client.get_container_client("test-data")
    blobs_list = container_client.list_blobs()
    # Check the Blob name is present or not
    for blob in blobs_list:
        if blob.name == sourceid + ".csv":
            flag = True
            break
    if flag:
        file_client = file_system_client.get_file_client(sourceid + ".csv")
        file_client.get_file_properties().size
        filesize_previous = file_client.get_file_properties().size
        local_file = gzip.open(filename, 'r')  # Change the Path over here !!!
        file_contents = local_file.read()
        file_client.append_data(data=file_contents, offset=filesize_previous, length=len(file_contents))
        file_client.flush_data(filesize_previous + len(file_contents))
    else:
        file_client = file_system_client.create_file(sourceid + ".csv")
        local_file = gzip.open(filename, 'r')  # Change the Path over here !!!
        file_contents = local_file.read()
        file_client.append_data(data=file_contents, offset=0, length=len(file_contents))
        file_client.flush_data(len(file_contents))

推荐阅读