如何使用axios/fetch(Node.js)将CSV文件上传到Blob存储 [英] How to upload a csv file to blob storage using axios/fetch (nodejs)

查看:149
本文介绍了如何使用axios/fetch(Node.js)将CSV文件上传到Blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

将*作为'./actionTypes'; 中的类型导入,

[错误:找不到模块'./actionTypes']


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

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

请提出一个更好的方法,工具或示例.

Blob存储链接的示例为:

解决方案

尝试使用axios:

  var axios = require('axios').default;var fs = require('fs');var crypto = require('crypto');var storageKey =<您的存储密钥>"var accountName =<您的存储帐户名称>"var containerName =<您的容器名称>"var fileName =<文件名>"var filePath =<文件路径,包括文件名>"var fileLength = fs.statSync(filePath).sizevar 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 +"/" + fileNamevar 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 +"/" + fileNamevar stringToSign = requestMethod +"\ n \ n \ n" + fileLength +"\ n \ napplication/x-www-form-urlencoded \ n \ n \ n \ n \ n \ n \ n" + canonicalizedHeaders +"\ n/"+规范化资源var signature = crypto.createHmac('sha256',Buffer.from(storageKey,'base64')).update(stringToSign,'utf-8').digest('base64');varauthorizationHeader ="SharedKey" + accountName +:" +签名const result = axios({baseURL:requestURL,方法:requestMethod,数据:fileStream,标头:{'Content-Length':fileLength,'x-ms-blob-type':blobType,'x-ms-date':日期,'x-ms-version':blobServiceVersion,'授权':authorizationHeader}}).then(function(response){console.log(response);}).catch(函数(错误){console.log(错误);}).finally(function(){//如果需要的话,最后添加功能}); 

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

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.

这篇关于如何使用axios/fetch(Node.js)将CSV文件上传到Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆