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.
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.
const sasURL = "https://myaccountname.blob.core.windows.net/" + container + "?" + accessKey;
const blobServiceClient = new BlobServiceClient(sasURL);
const containerClient = blobServiceClient.getContainerClient(container);
const blobName = parts[0].filename;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
const uploadBlobReponse = await blockBlobClient.upload(parts[0].data, parts[0].data.length);