我要做的是:
将文件上传到 亚马逊,使用point1中的signedUrl。
function uploadFileSilent(patientIdFromDb, fileName) {
console.log("patientIdFromDb ", patientIdFromDb);
console.log("FileName : ", fileName);
var path = invokeUrl + '/prd/patient/' + patientIdFromDb + '/signeduploadurl';
var data = '{fileName:"' + fileName + '",type:"eeg"}';
var options = {};
options.headers = { 'Authorization': token, 'Content-Type': 'application/json' };
needle.post(path, data, options, function (err, resp) {
if (err) {
console.log("Error ", err)
}
else {
console.log("resp : ", resp.body);
var url = resp.body.signedUrl;
var noProtocolUrl = url.replace("https://", '');
var fileLocation = outputLocation + '' + outputFilename
fileLocation = fileLocation.replace(/\\\\/g, '\\');
var host = noProtocolUrl.substring(0, noProtocolUrl.indexOf('/'));
var path = noProtocolUrl.substring(noProtocolUrl.indexOf('/'), noProtocolUrl.length);
var FormData = require('form-data');
var http = require("https");
var form = new FormData();
form.append('file', fs.createReadStream(fileLocation));
var options = {
"method": "PUT",
"hostname": host,
"port": 443,
"path": path,
"headers": form.getHeaders()
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
// Pipe form to request
form.pipe(req);
}
});
}
文件发布完成后,我们收到以下错误:
我们计算的请求签名与您提供的签名不符。检查您的密钥和签名方法。
当我们使用Postman尝试流程时,它可以工作。我认为问题可能是文件没有传递给请求的主体。
无论如何,请问如何将二进制文件传递给PUT请求的主体?