Right now I am working on an API that allows customers to upload their files to their own containers. I provide them with a container name and a SAS Token, then they input this information through the API to upload to their container. The following is a snippet of my code, where "container" is the container name and "accessKey" is the SAS token they submit:
var bodyBuffer = Buffer.from(req.body);
var boundary = multipart.getBoundary(req.headers['content-type']);
var parts = multipart.Parse(bodyBuffer, boundary);
const sasURL = "https://myaccountname.blob.core.windows.net/" + container + "?" + accessKey;
const containerClient = new BlobContainerClient(sasURL);
const blobName = parts[0].filename;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobReponse = await blockBlobClient.upload(parts[0].data, parts[0].data.length);
Azure states that uploadBlobResponse is declared but its value is never read, but I don’t know how to fix that issue.
2
Answers
After using a try/catch block to explicitly see what issues were in the code I found out that my BlobServiceClient was not properly defined. I created the blobServiceClient as a new BlobServiceClient first, then I created a new containerClient. The new upload code is shown below.
You might be getting 500 error due to authentication issue/sas url.
I have referred this document for creating CORS rule and SAS URL. Create the Javascript project and install the listed packages in the document.
Packages:
index.js file:
index.html file:
package.json:
SAS URL :
Console:
I copied the
URL
and paste it in the browser which gives below pageUpload a file. once successfully uploaded, it will show
done
Go to the storage account, to see the uploaded file