How to upload a file to a specific Azure Storage container using an Azure Function?

hampton123 1,140 Reputation points
2023-05-03T17:56:19.4866667+00:00

Currently I am working on an API that allows customers to upload their files to their own containers in my storage account. I provide them with an SAS token and a container name, and then they upload their file to their specific container. Here is a snippet of my current code:

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

"container" is the name of the container that the customer is uploading to, and "accessKey" is the token I provide them with. This code is not working, and I'm not sure what is wrong with it. Azure states that the value with uploadBlobResponse is never read but I don't know how to fix the issue. Thank you.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,519 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,563 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. hampton123 1,140 Reputation points
    2023-05-04T16:39:41.5033333+00:00

    Fixed the issue, the new code is below. I found out that my BlobServiceClient was not properly defined.

            const sasURL = "https://myaccountname.blob.core.windows.net/" + "?" + 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);
    
    1 person found this answer helpful.