我正在尝试自动化(用于测试自动化)上载/下载/验证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存储链接的示例为:
答案 0 :(得分:1)
尝试使用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会容易得多。希望对您有所帮助。
答案 1 :(得分:0)
Axios方法的替代方法,可以将以下方法用作简单的解决方案:
https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs-v10