Auston Barboza
Auston Barboza

Reputation: 165

how to upload a file to Azure storage blob Container after getting sasQueryParameter in SDK v10 using nodejs?

I would like to have some help with uploading a file to Azure blob storage. I actually want to be able to get SAS token from the backend for the container and want to be able to upload a file to that container using react web app.

I used this for reference, but couldn't get going.

Upvotes: 0

Views: 2076

Answers (1)

Auston Barboza
Auston Barboza

Reputation: 165

use generateBlobSASQueryParameters function from @azure/storage-blob package.

the way I used generateBlobSASQueryParameters is :

const {
  BlobSASPermissions,
  SharedKeyCredential,
  generateBlobSASQueryParameters,
} = require("@azure/storage-blob");

const {
  STORAGE_ACCOUNT_KEY,
  STORAGE_ACCOUNT_NAME,
  CONTAINERS,
} = require("./Constants/blobStorage"); // get these from azure storage account

const generateSasQueryToken = () => {
  const sharedKeyCredential = new SharedKeyCredential(STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY);
  const permissions = BlobSASPermissions.parse("racwd");
  const startDate = new Date();
  const expiryDate = new Date(startDate);
  expiryDate.setMinutes(startDate.getMinutes() + 100);
  startDate.setMinutes(startDate.getMinutes() - 100);
  const queryParams = generateBlobSASQueryParameters(
    {
      containerName: CONTAINERS.IMAGE,
      permissions: permissions.toString(),
      startTime: startDate,
      expiryTime: expiryDate,
    },
    sharedKeyCredential,
  );
  return {
    SAS_STRING: queryParams.toString(),
    CONTAINER_NAME: CONTAINERS.IMAGE,
    URL: `https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net/`,
  };
};

module.exports = generateSasQueryToken; 

Upvotes: 3

Related Questions