[英]Azure Data Lake + Python: Auth failure
我正在尝试使用azure-storage-file-datalake pip package 使用访问密钥写入我的 Azure 存储帐户。 我按照此处找到的说明进行操作,但在尝试写入数据时不断收到以下错误(建立连接没有问题):
(NoAuthenticationInformation) Server failed to authenticate the request. Please refer to the information in the www-authenticate header.
我刚刚创建了我的存储帐户并启用了访问密钥访问权限,这是我的 Azure 实例上的唯一资源。 有谁知道我该如何纠正这个问题并写入存储帐户上的容器?
一些附加信息:这是我建立存储帐户连接的地方:
def connect(self, storage_account, storage_key):
try:
connect_string = "DefaultEndpointsProtocol=https;AccountName=" +\
storage_account + "; AccountKey=" +\
storage_key + ";EndpointSuffix=core.windows.net"
self.datalake_service_client = DataLakeServiceClient.from_connection_string(
conn_str=connect_string)
self.is_connected = 1
print('Azure connected')
except Exception as e:
self.is_connected = 0
print('Azure connection failed: ' + str(e))
这是我将数据写入容器的地方:
def write_data_to_azure(self, container, folder_name, file_name, data_to_write):
print('Writing to Azure: ')
print("--------------------")
print('Container: {}'.format(container))
print('Folder name: {}'.format(folder_name))
print('File name: {}'.format(file_name))
print("Data: {}".format(data_to_write))
try:
file_system_client = self.datalake_service_client.get_file_system_client(
container)
directory_client = file_system_client.create_directory(folder_name)
directory_client = file_system_client.get_directory_client(
folder_name)
try:
file_client = directory_client.get_file_client(
file_name)
file_client.get_file_properties().size
filesize_previous = file_client.get_file_properties().size
file_client.append_data(
data_to_write, offset=filesize_previous, length=len(data_to_write))
file_client.flush_data(filesize_previous + len(data_to_write))
print('azure write complete')
except Exception as e:
print('caught: ' + str(e))
file_client = directory_client.create_file(file_name)
filesize_previous = 0
file_client.append_data(
data_to_write, offset=filesize_previous, length=len(data_to_write))
file_client.flush_data(filesize_previous + len(data_to_write))
except Exception as e:
# handle Azure write fail erro
print('Write to azure failed: ' + str(e))
我写 function 开始时的打印确保我将其写入正确的容器等。
关于错误,您没有将连接字符串定义为正确的格式。 格式应该类似于DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix=core.windows.net
。 请将您的connect_string
更新为connect_string = "DefaultEndpointsProtocol=https;AccountName=" +\ storage_account + ";AccountKey=" +\ storage_key + ";EndpointSuffix=core.windows.net"
。
例如
storage_account='testadls05'
storage_key=''
connect_string = "DefaultEndpointsProtocol=https;AccountName=" +\
storage_account + ";AccountKey=" +\
storage_key + ";EndpointSuffix=core.windows.net"
datalake_service_client =DataLakeServiceClient.from_connection_string(conn_str=connect_string ,logging_enable=True)
container='test'
file_system_client = datalake_service_client.get_file_system_client(
container)
folder_name="test"
directory_client = file_system_client.get_directory_client(
folder_name)
if not directory_client.exists():
print('create folder:' + folder_name)
directory_client = file_system_client.create_directory(folder_name)
file_name="test.txt"
data_to_write=b'test'
try:
file_client = directory_client.get_file_client(
file_name)
file_client.get_file_properties().size
filesize_previous = file_client.get_file_properties().size
file_client.append_data(
data_to_write, offset=filesize_previous, length=len(data_to_write))
file_client.flush_data(filesize_previous + len(data_to_write))
print('azure write complete')
except Exception as e:
print('caught: ' + str(e))
file_client = directory_client.create_file(file_name)
filesize_previous = 0
file_client.append_data(
data_to_write, offset=filesize_previous, length=len(data_to_write))
file_client.flush_data(len(data_to_write))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.