Authentication error while uploading to Azure Blob Storage :

Paarthvi Sharma 15 Reputation points
2023-06-19T17:02:05.0366667+00:00

Hello Team, I am trying to upload pdf to blob storage using Flask and React application. This is how my flask application looks like :

sas_token = 'sv=sastokenexample'
blob_client = BlobServiceClient(account_url=f"https://{AZURE_STORAGE_ACCOUNT}.blob.core.windows.net/${sas_token}")
blob_container = blob_client.get_container_client('upload-conainer')

@app.route('/upload_file', methods= ['GET','POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        filename = secure_filename(file.filename)
        try:
            blob_container.upload_blob(filename, file)
            return jsonify({"Success" : "Upload"})
            
        except Exception as e:
            logging.exception("Exception in /upload_file")
            return jsonify({"error": str(e)}), 202


I get the below error when the above code is deployed. I have generated the SAS token for blob as well as enable CORS. Not sure why this is happening.

{
    "error": "Server failed to authenticate the request. Please refer to the information in the www-authenticate header.\nRequestId:dddb127c-201e-004c-3fcd-a2201e000000\nTime:2023-06-19T16:46:42.6306933Z\nErrorCode:NoAuthenticationInformation\nContent: <?xml version=\"1.0\" encoding=\"utf-8\"?><Error><Code>NoAuthenticationInformation</Code><Message>Server failed to authenticate the request. Please refer to the information in the www-authenticate header.\nRequestId:dddb127c-201e-004c-3fcd-a2201e000000\nTime:2023-06-19T16:46:42.6306933Z</Message></Error>"
}
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,141 questions
{count} votes

1 answer

Sort by: Most helpful
  1. B santhiswaroop naik 400 Reputation points
    2023-06-19T17:12:36.71+00:00

    Hello

    check the below code

    Changes made:

    1. Added necessary imports for Flask, secure_filename, BlobServiceClient, and logging.
    2. Created a Flask app using Flask(__name__).
    3. Changed the account URL construction by removing the $ before the sas_token.
    4. Renamed the container name to 'upload-container'.
    5. Added additional error handling for cases where no file is found or an empty filename is provided.
    6. Modified the response status code for error responses to be 400 for client errors and 500 for server errors.
    7. Added a check for if __name__ == '__main__': to run the Flask app.

    Please make sure to replace <your_storage_account_name> with the actual name of your Azure Storage account before running the code.

    
    Certainly! Here's the corrected code:
    
    python
    Copy code
    from flask import Flask, request, jsonify
    from werkzeug.utils import secure_filename
    from azure.storage.blob import BlobServiceClient
    import logging
    
    app = Flask(__name__)
    
    sas_token = 'sastokenexample'
    AZURE_STORAGE_ACCOUNT = '<your_storage_account_name>'
    blob_client = BlobServiceClient(account_url=f"https://{AZURE_STORAGE_ACCOUNT}.blob.core.windows.net/{sas_token}")
    blob_container = blob_client.get_container_client('upload-container')
    
    @app.route('/upload_file', methods=['POST'])
    def upload():
        if 'file' not in request.files:
            return jsonify({"error": "No file found in the request"}), 400
        
        file = request.files['file']
        if file.filename == '':
            return jsonify({"error": "Empty filename"}), 400
        
        filename = secure_filename(file.filename)
        try:
            blob_container.upload_blob(name=filename, data=file)
            return jsonify({"Success": "Upload"})
        
        except Exception as e:
            logging.exception("Exception in /upload_file")
            return jsonify({"error": str(e)}), 500
    
    if __name__ == '__main__':
        app.run()
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.