Reputation: 165
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
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