如何使用axios / fetch(nodejs)将CSV文件上传到Blob存储

我正在尝试自动化(用于测试自动化)上载/下载/验证Azure云中托管的应用程序的csv文件数据,并关注以下文章:

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

并尝试按照指示实施它,但是,在以下(action.js)中找不到' actionTypes ':

import * as types from './actionTypes';并迷路了。

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


根据我的学习,我相信axios或fetch可以用于执行任务(我更喜欢axios),并且我需要一些帮助来简化解决方案或在正确的方向上完成任务。

我知道有人问过与此场景相关的类似问题,但是,到目前为止,这些问题都没有解决,或者属于不同的工具和技术栈。

请提出更好的方法,工具或示例。

Blob存储链接的示例为:

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

ddyyqqrr 回答:如何使用axios / fetch(nodejs)将CSV文件上传到Blob存储

尝试使用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 
  });  

使用剩余的API将文件上传到存储非常复杂。使用SDK会容易得多。希望对您有所帮助。

,

Axios方法的替代方法,可以将以下方法用作简单的解决方案:

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

本文链接:https://www.f2er.com/3168978.html

大家都在问