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