在没有为我找到任何可行的解决方案后,我把我的Angular Electron应用代码粘贴过来。
控件
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
};
}
localPath是之前用fs.stats检查过的,所以错误不是因为文件不在那里。事实上,我能够在s3.putObject上使用它,才意识到上传的大小。
你使用的是fs,一个nodejs库,在浏览器中无法使用。如果你想直接从浏览器上传文件到s3,请查看aws文档中给出的这个例子。https:/docs.aws.amazon.comsdk-for-javascriptv2developer-guides3-example-photo-album.html。