python - 在 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))
解决方案
通过以下代码片段解决的问题......终于得到了通过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))
推荐阅读
- javascript - 尝试使用 SQL COUNT(*) 查询的值作为 NodeJS 中另一个查询中的值时卡住了
- java - JAVA - 打印节点+二叉搜索树的深度
- r - 在 R 中的嵌套组中重新采样
- arrays - 如何优化删除重复元素的代码?
- go - 将 XML 解组为 Go 结构不适用于数组
- linux - 线程之间的切换是否比 Linux 中的进程更有效?
- sql-server - 即使使用 DateTime2(4) 字段,MS SQL 导入导出向导也会截断亚秒
- java - MainActivity 中的片段内的片段
- java - 使用 Java 实现 Redis 数据库
- objective-c - swift 中 [CBUUID UUIDWithString: @"9abd"] 的等价物