Marco Jr
Marco Jr

Reputation: 6806

Uploading files to Azure using Shared Access Permissions with nodeJS and a web or mobile client

On my Azure Portal, I've this blob: Home / Storage accounts / my_blob_account / Blobs / users

What I am trying to archive: A user can upload images to the Azure Blob. He basically call the node server to generate a url so I can put the files.

This is the nodeJS code:

export const generateBlobUrl = (format) => {
  const fileName = generateRandonChars(16) + '.' + format.toLowerCase()
  const blob = Azure.createBlobService(my_blob_account, BLOB_SECRET)
  const sharedAccessPolicy = { AccessPolicy: {
    Expiry: Azure.date.minutesFromNow(60),
    Permissions: Azure.Constants.BlobConstants.SharedAccessPermissions.READ
  }}
  const sasToken = blob.generateSharedAccessSignature('users', fileName, sharedAccessPolicy);
  return blob.getUrl('users', fileName, sasToken)
}

this will return a url like https://my_blob_account.blob.core.windows.net/users/h4KfK04GighEh8kb.png?se=2019-09-25T11%3A37%3A37Z&sp=r&sv=2018-03-28&sr=b&sig=uRtOdj8hBUGXxtX7BhXZzItnKilFq0t1zhwKYcAVFCY%3D

and on the client side, I've this:

RNFetchBlob.fetch('PUT', "the url that came from the nodeJS", {
        'Content-Type': 'application/octet-stream',
        'x-ms-blob-type': 'BlockBlob'
    }, RNFetchBlob.wrap("path to local file"))
        .then(r =>{
            console.log(r)
        })
        .catch(err =>{
            console.log(err)
        })

What's wrong ? I am receiving this error:

AuthorizationPermissionMismatchThis request is not authorized to perform this operation using this permission. RequestId:63ce7eea-a01e-001c-298d-73cedd000000 Time:2019-09-25T10:37:37.7337049Z

Upvotes: 0

Views: 168

Answers (1)

rickvdbosch
rickvdbosch

Reputation: 15571

You're creating a SAS token with READ permissions:

Permissions: Azure.Constants.BlobConstants.SharedAccessPermissions.READ

You should create a token that has WRITE permissions:

Permissions: Azure.Constants.BlobConstants.SharedAccessPermissions.WRITE

Upvotes: 1

Related Questions