我正在尝试运行以下代码:
const uploadParams = {Bucket: bucketName, Key: '', Body: ''};
const file = '/home/a/bars/img1.png';
const fileStream = fs.createReadStream(file);
fileStream.on('error', function(err) {
console.log('File Error', err);
});
uploadParams.Body = fileStream;
var path = require('path');
uploadParams.Key = path.basename(file);
但是我在代码的uploadParams.Body = fileStream;
行得到以下错误:
Type 'ReadStream' is not assignable to type 'string'.ts(2322)
我该怎么补救呢?
1条答案
按热度按时间nfs0ujit1#
只需指定对象键
Body
可以保存的类型,如下所示:解决方案:
或者,如果使用
require
,则可以使用typeof
关键字:虽然我觉得用空字符串来保存流很奇怪,但为什么不直接这么做呢?
示范-TSPlayground