[英]How to integrate Nodejs running on AWS EC2 and static Angularjs content on AWS S3?
[英]AWS S3 Running on the same EC2
我有一項服務在運行 AWS-S3 的同一實例上運行,在這種情況下,我不必在將文件推送到 S3 存儲桶時提供憑證。
這是我的代碼,但它顯示了這個錯誤:
message: 'Access Denied',
code: 'AccessDenied',
region: null,
time: 2019-12-13T22:30:56.325Z,
requestId: '3F6AA6FAC6787B36',
extendedRequestId:'yQ4y6uthaTDBr6SuoXbI0g0cLWY40SWleATXrBijKrAbhcmsWTYAL+wCBlZpPM/6ytRc8qbIJP4=',
cfId: undefined,
statusCode: 403,
retryable: false,
retryDelay: 44.582589819600926
這是代碼:
public async writeFileToS3(datasetFile: any): Promise<boolean> {
try {
this.s3Bucket = new S3({ region: 'us-east-1' });
const readFile = this.util.promisify(this.fileWriter.readFile);
const unlinkFile = this.util.promisify(this.fileWriter.unlink);
const s3BucketName = this.appConfig.get<string>(
'infra.fileWriter.bucket'
);
const data = await readFile(datasetFile.path);
const params = {
Bucket: s3BucketName,
Key: datasetFile.originalname,
Body: data,
ACL: 'public-read'
};
return await new Promise<boolean>((resolve, reject) => {
this.s3Bucket.upload(params, function(err: any) {
unlinkFile(datasetFile.path);
if (err) {
console.log('await call: ' + err);
reject(err);
} else {
resolve(true);
}
});
});
} catch (err) {
console.log(err);
throw new OperationError('Error writing file to S3');
}
注意:AWS 配置良好,因為我有另一個使用 Python 與 S3 交互的服務,它運行良好,但該服務使用 aws-sdk 進行 Javascript。
問題已解決,是因為提供了region
和ACL
。 只需刪除它們即可解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.