简体   繁体   中英

How to upload a csv file to blob storage using axios/fetch (nodejs)

I am trying to automate (for test automation) the upload/download/validate the csv file data for an application hosted in Azure cloud and was following the article:

https://medium.com/@fakiolinho/handle-blobs-requests-with-axios-the-right-way-bb905bdb1c04

And tried to implement it as directed, however, couldn't find the ' actionTypes ' in the following (action.js):

import * as types from './actionTypes'; and got a bit lost to start over.

[Error: Cannot find module './actionTypes']


Based on my learning, I believe the axios or fetch could be used to perform the task (I prefer axios) and I need some help to jot down the solution or help in the right direction to complete the task.

I understand that there are similar questions that have been asked related to this scenario, however, either none of them have been resolved so far or they belong to different tools and tech-stack.

Please suggest a better approach, tool or example.

Sample of Blob Storage link is:

https://portal.azure.com/#blade/Microsoft_Azure_Storage/ContainerMenuBlade/overview/storageAccountId/%2Fsubscriptions%2F0d2c6-7dba6272e3a1%2FresourceGroup%2Fpre-prod-net%2Fproviders%2FMicrosoft.Storage%2FstorageAccounts%2Fapistorage/path/input-folder/etag/%210x8D6B31B54E2%22

Try this using axios:

var axios = require('axios').default;
var fs = require('fs');
var crypto =  require('crypto');

var storageKey = "<your storage key>"
var accountName = "<your storage account name>"
var containerName="<your container name>"
var fileName="<file name>"
var filePath = "<file path ,including file name>"


var fileLength= fs.statSync(filePath).size
var fileStream = fs.createReadStream(filePath);

var blobType ="BlockBlob"
var date = new Date().toUTCString()
var blobServiceVersion = "2014-02-14"

var storageBlobEndpoint = "https://"+ accountName +".blob.core.windows.net"
var requestURL = storageBlobEndpoint + "/" + containerName + "/" + fileName
var requestMethod = "PUT"


var canonicalizedHeaders = "x-ms-blob-type:"+ blobType +"\nx-ms-date:"+ date +"\nx-ms-version:" + blobServiceVersion;
console.log("headers :"+canonicalizedHeaders);
var canonicalizedResource = accountName + "/" + containerName + "/" +  fileName

var stringToSign = requestMethod+"\n\n\n"+fileLength+"\n\napplication/x-www-form-urlencoded\n\n\n\n\n\n\n" + canonicalizedHeaders + "\n/" + canonicalizedResource

var signature = crypto.createHmac('sha256', Buffer.from(storageKey, 'base64')).update(stringToSign, 'utf-8').digest('base64');

var authorizationHeader = "SharedKey "+accountName + ":" + signature


  const result = axios({
        baseURL: requestURL,
        method: requestMethod,
        data:fileStream,
        headers: {
            'Content-Length':fileLength,
            'x-ms-blob-type': blobType,
            'x-ms-date':date,
            'x-ms-version':blobServiceVersion,
            'Authorization' : authorizationHeader
            }
        }).then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
  .finally(function () {
    //add finally function here if needed 
  });  

It is pretty complex using rest APIs to upload files to storage. Using SDK will be much easier. Hope it helps.

Alternative to Axios approach, the following could be used as an easy solution:

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs-v10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM