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

送分小仙女□ 提交于 2021-01-29 10:41:09

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/58687519/how-to-upload-a-csv-file-to-blob-storage-using-axios-fetch-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!