尝试使用stream.PassThrough尝试上传到Amazon S3时不支持的身体有效载荷对象 [英] Unsupported body payload object when trying to upload to Amazon S3 using stream.PassThrough
问题描述
在找不到适合我的解决方案后,我将粘贴Angular Electron应用程序代码.
After not finding any working solution to this problem for me, I am pasting my Angular Electron app code.
组件
const pipeline = this.syncService.uploadSong(localPath, filename);
pipeline.on('close', (data) => {
// upload finished
})
pipeline.on('error', (err) => {
console.error(err.toString());
})
服务是:
uploadSong(localPath: string, filename: string) {
const {writeStream, promise} = this.uploadStream(filename);
const readStream = fs.createReadStream(localPath);
return readStream.pipe(writeStream);
}
uploadStream(filename: string) {
const stream = require('stream'); // "stream": "0.0.2",
const pass = new stream.PassThrough();
const params = {
Body: pass,
Bucket: this.s3Bucket,
Key: filename,
};
const options = {partSize: 5 * 1024 * 1024, queueSize: 6};
return {
writeStream: pass,
promise: this.s3.upload(params,options).promise() // ERROR Because the Body
};
}
之前使用fs.stats检查了localPath,因此错误不是因为文件不存在.实际上,我可以在实现上传大小之前,在s3.putObject上使用它.
The localPath is checked before with fs.stats, so the error is not because the file is not there. In fact I am able to use it on a s3.putObject, before realising the size of the uploads.
推荐答案
您正在使用fs,它是一个nodejs库,在浏览器中不可用.如果要直接从浏览器将文件上传到s3,请查看AWS文档中提供的以下示例:
You are using fs, a nodejs library, which is not available in browser. If you want to upload files directly from browser to s3, check this example given in aws documentation: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html
这篇关于尝试使用stream.PassThrough尝试上传到Amazon S3时不支持的身体有效载荷对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!