【发布时间】:2019-10-13 04:21:12
【问题描述】:
我如何确保下面的方法返回一个 observable?
uploadFile(file, filename) {
const contentType = file.type;
const bucket = new S3(
{
accessKeyId: environment.awsAccessKeyId,
secretAccessKey: environment.awssecretAccessKey,
region: environment.awsRegion
}
);
const params = {
Bucket: environment.awsBucket,
Key: environment.awsKey + filename,
Body: file,
ACL: 'public-read',
ContentType: contentType
};
bucket.upload(params, function (err, data) {
if (err) {
this.logger.debug('There was an error uploading your file: ', err);
return null;
}
this.logger.debug('Successfully uploaded file.', data);
return data;
});
}
我想从一个组件中调用它并捕获返回输出:
this.ngxLoader.start();
this.uploadService.uploadFile(newFile, sermonName), ((data) => {
if (data) {
// this.sermon.id = data.id;
// this.logger.debug(`Posted file: ${JSON.stringify(data)}`);
// this.logger.debug(`Updating file id: ${JSON.stringify(this.sermon.id)}`);
// this.update();
this.ngxLoader.stop();
}
}, error => {
this.ngxLoader.stop();
this.modalService.displayMessage('Oops!', error.message);
});
【问题讨论】:
标签: angular amazon-s3 file-upload