我希望将文件以流的形式上载到Azure存储中,但我不想使用Azure SDK
它,而是想使用REST API
and not 以更通用的方式来做BlobServiceClient
。
有办法吗?相同的参考链接可以在这里找到:
但是这里提到的链接提出了使用的解决方案Azure SDK
。我想没有 Azure SDK
这是代码:
const CryptoJS = require("crypto-js");
const request = require("request");
const fs = require("fs");
const account = process.env.ACCOUNT_NAME || "";
const key = process.env.ACCOUNT_KEY || "";
const containerName = "demo";
const blobName = "dummyfile.txt";
var strTime = new Date().toUTCString();
// read file to Stream
var filePath = `${__dirname}/README.md`;
const readStream = fs.createReadStream(filePath);
var stat = fs.statSync(filePath);
string_params = {
verb: "PUT",
"Content-Encoding": "",
"Content-Language": "",
"Content-Length": stat.size,
"Content-MD5": "",
"Content-Type": "application/octet-stream",
Date: "",
"If-Modified-Since": "",
"If-Match": "",
"If-None-Match": "",
"If-Unmodified-Since": "",
Range: "",
CanonicalizedHeaders:
"x-ms-blob-type:BlockBlob\nx-ms-date:" +
strTime +
"\nx-ms-version:" +
"2020-04-08\n",
CanonicalizedResource: `/${account}/${containerName}/${blobName}`,
};
var strToSign = `${string_params["verb"]}\n${string_params["Content-Encoding"]}\n${string_params["Content-Language"]}\n${string_params["Content-Length"]}\n${string_params["Content-MD5"]}\n${string_params["Content-Type"]}\n${string_params["Date"]}\n${string_params["If-Modified-Since"]}\n${string_params["If-Match"]}\n${string_params["If-None-Match"]}\n${string_params["If-Unmodified-Since"]}\n${string_params["Range"]}\n${string_params["CanonicalizedHeaders"]}${string_params["CanonicalizedResource"]}`;
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = `SharedKey ${account}:` + hashInBase64;
const options = {
url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}`,
headers: {
Authorization: auth,
"x-ms-blob-type": "BlockBlob",
"x-ms-date": strTime,
"x-ms-version": "2020-04-08",
"Content-Type": "application/octet-stream",
"Content-Length": stat.size,
},
body: readStream,
};
function callback(error, response, body) {
console.log(response.statusCode);
console.log(response.statusMessage);
if (!error && response.statusCode == 200) {
console.log(error);
console.log(response);
console.log(body);
}
}
request.put(options, callback);
我得到的错误是:
TypeError: Cannot read property 'statusCode' of undefined
编辑:问题已由Pamela的代码解决+我发现的问题是初始化.env
变量时出错。
您可以使用Put Blob Rest API上传视频流。有一个使用node.js的示例。
const CryptoJS = require("crypto-js");
const request = require("request");
const fs = require('fs');
const account = "account-name";
const key = "account-key";
var strTime = new Date().toUTCString();
var filePath = 'your-file-path';
const readStream = fs.createReadStream(filePath);
var stat = fs.statSync(filePath);
string_params = {
'verb': 'PUT',
'Content-Encoding': '',
'Content-Language': '',
'Content-Length': stat.size,
'Content-MD5': '',
'Content-Type': 'application/octet-stream',
'Date': '',
'If-Modified-Since': '',
'If-Match': '',
'If-None-Match': '',
'If-Unmodified-Since': '',
'Range': '',
'CanonicalizedHeaders': 'x-ms-blob-type:BlockBlob\nx-ms-date:' + strTime + '\nx-ms-version:' + '2020-04-08\n',
'CanonicalizedResource': `/${account}/containername/myblob`
}
var strToSign = `${string_params['verb']}\n${string_params['Content-Encoding']}\n${string_params['Content-Language']}\n${string_params['Content-Length']}\n${string_params['Content-MD5']}\n${string_params['Content-Type']}\n${string_params['Date']}\n${string_params['If-Modified-Since']}\n${string_params['If-Match']}\n${string_params['If-None-Match']}\n${string_params['If-Unmodified-Since']}\n${string_params['Range']}\n${string_params['CanonicalizedHeaders']}${string_params['CanonicalizedResource']}`
console.log(strToSign);
var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = `SharedKey ${account}:` + hashInBase64;
console.log(auth)
const options = {
url: `https://${account}.blob.core.windows.net/containername/myblob`,
headers: {
Authorization: auth,
'x-ms-blob-type': 'BlockBlob',
"x-ms-date": strTime,
"x-ms-version": "2020-04-08",
'Content-Type': "application/octet-stream",
'Content-Length': stat.size
},
body: readStream
};
function callback(error, response, body) {
console.log(response.statusCode);
console.log(response.statusMessage);
if (!error && response.statusCode == 200) {
console.log(error);
console.log(response);
console.log(body);
}
}
request.put(options, callback);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句