【发布时间】:2021-10-02 07:21:44
【问题描述】:
按照 Microsoft 文档:
使用帐户密钥连接到 Azure Data Lake Storage Gen2:
def initialize_storage_account(storage_account_name, storage_account_key):
try:
global service_client
service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https", storage_account_name), credential=storage_account_key)
except Exception as e:
print(e)
将文件上传到目录:
def upload_file_to_directory():
try:
file_system_client = service_client.get_file_system_client(file_system="my-file-system")
directory_client = file_system_client.get_directory_client("my-directory/filter")
file_client = directory_client.create_file("my_csv_write.csv")
local_file = open("C:\\Users\\my_csv_read.csv",'r')
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))
print("File uploaded")
except Exception as e:
print(e)
我可以使用此功能将文件从本地上传到 Azure 存储,并且可以正常工作。
但我想做的是从天蓝色存储中读取文件并在天蓝色存储中写入。 我做了什么
def read_and_write_to_directory():
try:
file_system_client = service_client.get_file_system_client(file_system="my-file-system")
directory_client_read = file_system_client.get_directory_client("my-directory")
directory_client_write = file_system_client.get_directory_client("my-directory/filter")
file_client_read = directory_client_read.get_file_client("my_csv_read.csv")
file_path = open(file_client_read,'r')
file_contents = file_path.read()
file_client_write = directory_client_write.create_file("my_csv_write.csv")
file_client_write.append_data(file_contents, overwrite=True)
except Exception as e:
print(e)
但它不起作用,
错误
expected str, bytes or os.PathLike object, not DataLakeFileClient
那么从 azure Lake Storage 读取文件并在 azure Lake Storage 中写入的正确方法是什么?
【问题讨论】:
标签: python azure azure-data-lake-gen2