Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,141 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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>"
}
Hello
check the below code
Changes made:
Flask(__name__)
.$
before the sas_token
.'upload-container'
.400
for client errors and 500
for server errors.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()