Cornel Verster
Cornel Verster

Reputation: 1783

Azure Data Lake + Python: Auth failure

I'm trying to use azure-storage-file-datalake pip package to write to my Azure storage account using an access key. I follow the instructions found here but I keep getting the following error when I try to write data (the connection is established without an issue):

(NoAuthenticationInformation) Server failed to authenticate the request. Please refer to the information in the www-authenticate header.

I've just created my storage account and I have access key access enabled, and it's the only resouce on my Azure instance. Does anyone know how I can correct this and write to my containers on the storage account?

Some additional info: Here is where I make my storage account connection:

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))

And here is where I write data to the container:

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))

The print at the start of my write function ensures me that its writing to the right containers etc.

Upvotes: 0

Views: 359

Answers (1)

Jim Xu
Jim Xu

Reputation: 23141

Regarding the error, you did not define your connection string as right formate. The formate should be like DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix=core.windows.net. Please update your connect_string as connect_string = "DefaultEndpointsProtocol=https;AccountName=" +\ storage_account + ";AccountKey=" +\ storage_key + ";EndpointSuffix=core.windows.net".

For example

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))

enter image description here

Upvotes: 1

Related Questions