aws-sdk bucket.getObject: 无'Access-Control-Allow-Origin'错误



>我正在尝试从 S3 存储桶获取对象,并已在我的存储桶上配置 CORS,但仍然得到

没有"访问控制允许来源">

错误

我已经尝试了在线提供的所有各种修复程序,但似乎没有任何效果。

我的角度代码

import { Component } from '@angular/core';
import * as AWS from 'aws-sdk';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'awsapp';
ngOnInit() {
AWS.config.credentials = new AWS.Credentials({
accessKeyId: 'my-access-key',
secretAccessKey: 'my-secret-key'
});
AWS.config.update({
region: 'us-east-1'
});
}
getObject() {
let s3 = new AWS.S3();
const params = {
Bucket: 'my-bucket-name',
Key: 'testfolder/tests3.txt'
};
s3.getObject(params, function (err, data) {
if (err) {
console.error(err);
}
else {
// const string = new TextDecoder('utf-8').decode(data.Body);
console.log(data);
}
});
}
}

CORS 需要在服务器端配置。在服务器配置中添加此内容

<AllowedHeader>*</AllowedHeader>

相关内容

最新更新