0

Trying to upload a file to azure blob storage using @azure/storage-blob sdk in nodejs:

module.exports.createBlob = (blobName, containerName, blobContent) => {
  return new Promise(async (resolve, reject) => {
    try {
      const sharedKeyCredential = await this.createSharedAccessToken(blobName, 'c')
      const blobServiceClient = new BlobServiceClient(
    `https://${process.env.AZURE_BLOB_ACCOUNT}.blob.core.windows.net`,
    sharedKeyCredential
      )
      const containerClient = blobServiceClient.getContainerClient(containerName)
      const blockBlobClient = containerClient.getBlockBlobClient(blobName)
      const blob = await blockBlobClient.upload(blobContent, blobContent.length) // implement later
      resolve(blob)
    } catch (err) {
      console.log(err)
      reject(err)
    }
  })
}

module.exports.createSharedAccessToken = (blobName, permission) => {
  return new Promise(async (resolve, reject) => {
    const sharedKeyCredential = new StorageSharedKeyCredential(process.env.AZURE_BLOB_ACCOUNT, process.env.AZURE_BLOB_KEY)
    const containerName = process.env.AZURE_CONTAINER_NAME
    const startsOn = new Date()
    expiresOn.setMinutes(expiresOn.getMinutes() + parseInt(autoLogoutDuration.KeyValue))
    const blobSAS = generateBlobSASQueryParameters({
      containerName, // Required
      blobName, // Required
      permissions: BlobSASPermissions.parse(permission), // Required
      startsOn: startsOn, // Required
    },
    sharedKeyCredential // StorageSharedKeyCredential - `new StorageSharedKeyCredential(account, accountKey)`
    ).toString()
    resolve(decodeURI(blobSAS))
})
}

It keeps throwing a "NoAuthenticationInformation" error. The same creds work for downloading an existing blob but uploading is not working no matter what I try. Any help would be appreciated.

  • You can refer to [Avoiding “AuthorizationFailed” error when hand-crafting Shared Access Signature for Azure Storage](https://gauravmantri.com/2020/02/21/avoiding-authorizationfailed-error-when-hand-crafting-shared-access-signature-for-azure-storage/), [How to upload images and files to Azure Blob Node.js](https://stackoverflow.com/questions/60458504/how-to-upload-images-and-files-to-azure-blob-node-js) and [Stream uploaded file to Azure blob storage with Node](https://stackoverflow.com/questions/18317904/stream-uploaded-file-to-azure-blob-storage-with-node) – Ecstasy Nov 20 '21 at 04:38

1 Answers1

0

Followed by this MS DOC , I tried to reproduce your issue ,but able to upload files into my azure blob container using Node.js without any authentication error.

As you are using shared key credential we need to have all those permissions in our portal as shown below:

enter image description here

Also i am using @azure/storage-blob sdk in nodejs in my package.json .

enter image description here

Also added @azure/storage-blob sdk in my testupload.js file

enter image description here

And added the below code into my testupload.js file as i have already created container i just commented the above screenshot code .

const  account="testa";

const  sharedKeyCredential = new  StorageSharedKeyCredential("yourstorageaccountname", "your storage key connection string");

const  blobServiceClient1 = new  BlobServiceClient(

// When using AnonymousCredential, following url should include a valid SAS or support public access

`https://${account}.blob.core.windows.net`,

sharedKeyCredential

);

  

const  blobnewname="example2.txt"

blobContent="hi hello";

const  containerClient1 = blobServiceClient.getContainerClient("test")

const  blockBlobClient1= containerClient.getBlockBlobClient(blobnewname)

const  blob = blockBlobClient1.upload(blobContent, blobContent.length)

Then i can able to test and upload my files to Azure blob container Here are screenshot for reference:

enter image description here

enter image description here

For more information please refer this MS DOC : Azure Storage samples using v12 JavaScript client libraries

UPDATE:- As suggested by @Setanjan Roy

Alternate way:- To get rid of this we can use new BlobServiceClient( https://${process.env.AZURE_BLOB_ACCOUNT}.blob.core.windows.net${sharedKeyCredential} )

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • I am pretty sure the creds I am using has necessary permissions as the frontend team using Angular is able to upload a file easily. I actually was able to solve the issue by looking at their code. The issue was how the sas was being passed to the blob service client constructor. Here is how I did it : new BlobServiceClient( `https://${process.env.AZURE_BLOB_ACCOUNT}.blob.core.windows.net${sharedKeyCredential}` ) Somehow it worked. – Setanjan Roy Nov 25 '21 at 13:38