Uploading CSV to Azure Blob Stoarge using Axios

183 Views Asked by At

I'm trying to upload a CSV file to an Azure container using Axios in a backend NodeJS application. This is my code:

const BlobUriWithSASToken = 'https://myContainerName.blob.core.windows.net/folder1/folder2/folder3/folder4/mytestfile.csv?sv=<SAS Token>';
const form = new FormData();
    form.append(
      'file',
      fs.createReadStream(path.join(__dirname + '/mytestfile.csv')),
    );

    const request_config = {
      headers: {
        'Content-Length': 52,
        'x-ms-blob-type': 'BlockBlob',
        ...form.getHeaders(),
      },
    };

    try {
      const response = await axios.put(
        BlobUriWithSASToken,
        form,
        request_config,
      );
      expect(response.status).toEqual(201);
    } catch (err) {
      console.log(err);
    }

However, running this test returns a 403 - Forbidden. These are the error details:

  status: 403,
  statusText: 'This request is not authorized to perform this operation.',

My CSV is a small 52 bytes file.

1

There are 1 best solutions below

1
Sourav On

status: 403, statusText: 'This request is not authorized to perform this operation.'

Initially, I got the same error when I run the same code in my environment.

enter image description here

The 403 error indicates that the request was not authorized to perform the operation. There could be some possible reasons.

  • Make sure your SAS token has the correct permissions to perform the operation. In this case, since you are uploading a file, you need to have "Write" and "Create" permissions.
  • Also when you copy the SAS token check if the ? there if it is there remove and paste it because you have already mentioned it in the URL.
  • Check that the storage account name, container name, and blob name in your URL are correct. Make sure they match the values in your Azure Storage account.

I tried with the below code to upload CSV files using Axios in a backend NodeJS application.

Code:

const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');

const accountName = 'venkat123';
const containerName = 'test2';
const blobName = 'folder1/folder2/folder3/folder4/mytestfile.csv';
const sasToken = 'you sas token without ?';

const blobUriWithSasToken = `https://${accountName}.blob.core.windows.net/${containerName}/${blobName}?${sasToken}`;

const form = new FormData();
form.append('file', fs.createReadStream(path.join(__dirname, "Employee Sample Data.csv")));

const headers = {
  ...form.getHeaders(),
  'x-ms-blob-type': 'BlockBlob',
  'Content-Length': fs.statSync(path.join(__dirname, 'Employee Sample Data.csv')).size,
};

const config = {
  headers,
};

axios.put(blobUriWithSasToken, form, config)
  .then(response => {
    console.log('Success:', response.status);
  })
  .catch(error => {
    console.log('Error:', error.message);
  });

Output:

The above code is executed and uploaded CSV file successfully.

Success: 201

enter image description here

Portal: enter image description here