基于Angular/quickstart上传到Angular 2项目中的AWS S3 bucket
我需要从Angular.io的Angular 2快速入门库上传(他们不使用网页包)基于Angular/quickstart上传到Angular 2项目中的AWS S3 bucket,angular,amazon-s3,cors,Angular,Amazon S3,Cors,我需要从Angular.io的Angular 2快速入门库上传(他们不使用网页包) 我使用npm安装aws sdk 我在index.html中添加了以下内容: 这是组件中的代码: @Injectable() export class S3Service { private uploadSuccess = true; private creds = { "bucket": "nameOfBucket", "access_key": "accessKey", "sec
@Injectable()
export class S3Service {
private uploadSuccess = true;
private creds = {
"bucket": "nameOfBucket",
"access_key": "accessKey",
"secret_key": "secretKey",
"region": "us-east-1"
}
upload(file: File){
if (file){
console.log('verified with file');
}else{
console.log('without file');
}
console.log('filetype verified as images/png: ', file.type);
AWS.config.update({
accessKeyId: this.creds.access_key,
secretAccessKey: this.creds.secret_key,
});
AWS.config.region = this.creds.region;
AWS.config.sslEnabled = false;
console.log('aws.s3 is verified to be a function: ', AWS.S3);
let bucket = new AWS.S3({ params: { Bucket: this.creds.bucket }});
let key = `categories/${file.name}`;
console.log('verified key is : ', key);
let params = {Key: key, Body: file, ContentType: file.type, ServerSideEncryption: 'AES256'};
bucket.putObject(params, function (err: Response | any, data: Response) {
if (err){
console.log('there is an error: ', err);
}
else{
console.log('there is no error in s3 upload');
}
});
}<CORSConfiguration xmlns="removed this from being displayed">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedOrigin>http://localhost:3000</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
<ExposeHeader>x-amz-request-id</ExposeHeader>
<ExposeHeader>x-amz-id-2</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
*
http://localhost:3000
邮递
得到
放
3000
x-amz-服务器端加密
x-amz-request-id
x-amz-id-2
*
Angular 1代码成功上载图像
请提供帮助和高级感谢。您在S3存储桶上的CORS配置对于您描述的场景来说很好 我认为问题在于由代码生成的端点URL。我有一个类似的问题,它是混乱的,很难找到 出于某种原因,AWS SDK似乎会根据您用于设置区域信息和/或bucket名称的方法生成不同的端点URL。当生成的URL不包含区域信息(您的URL不包含区域信息)时,会导致飞行前请求失败,从而导致浏览器控制台中出现关于CORS的错误消息(有时可能是飞行前失败的原因) “问题”端点格式:
http://.s3.amazonaws.com/
“所需”端点格式:
http://s3-.amazonaws.com//
尝试使用我在这里提供的组件来验证您的AWS S3配置、访问和CORS设置。然后,如果您愿意,您可以轻松地将S3特定的内容提取到服务中
以下是步骤:
)(请参见下面的app.component.ts)从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`
你好{{name}
`,
})
导出类AppComponent{name='Angular';}
AWS S3铲斗CORS配置:注意:您可能希望根据您的安全需要,使您的应用程序更具限制性
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1485926968000",
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::<your bucketname>/*"
]
}
]
}
*
得到
放
邮递
删除
3000
*
AWS IAM策略(附加到用户或组):注意:您几乎肯定希望根据您的安全需要,使允许的操作更具限制性 注意:用适当的值替换
{
“版本”:“2012-10-17”,
“声明”:[
{
“Sid”:“Stmt1485926968000”,
“效果”:“允许”,
“行动”:[
“s3:*”
],
“资源”:[
“arn:aws:s3::/*”
]
}
]
}
如果这不能解决您的问题,请使用Chrome开发工具并查看“网络”选项卡,查看S3API的选项请求,并用完整的响应更新您的问题。当AWS S3预飞行失败时,它们通常会在响应中提供良好的信息。您在S3存储桶上的CORS配置对于您描述的场景来说很合适 我认为问题在于由代码生成的端点URL。我有一个类似的问题,它是混乱的,很难找到 出于某种原因,AWS SDK似乎会根据您用于设置区域信息和/或bucket名称的方法生成不同的端点URL。当生成的URL不包含区域信息(您的URL不包含区域信息)时,会导致飞行前请求失败,从而导致浏览器控制台中出现关于CORS的错误消息(有时可能是飞行前失败的原因) “问题”端点格式:
http://.s3.amazonaws.com/
“所需”端点格式:
http://s3-.amazonaws.com//
尝试使用我在这里提供的组件来验证您的AWS S3配置、访问和CORS设置。然后,如果您愿意,您可以轻松地将S3特定的内容提取到服务中
以下是步骤:
(function (global) {
System.config({
...
map: {
...
'aws-sdk': 'npm:aws-sdk'
},
packages: {
...
'aws-sdk': {
defaultExtension: 'js',
main: 'dist/aws-sdk.js',
format: 'global'
}
}
});
})(this);
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { S3UploadTestComponent } from './s3-upload-test.component';
@NgModule({
imports: [BrowserModule],
declarations: [
AppComponent,
S3UploadTestComponent,
],
bootstrap: [AppComponent]
})
export class AppModule { }
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Hello {{name}}</h1>
<s3-upload-test></s3-upload-test>
`,
})
export class AppComponent { name = 'Angular'; }
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1485926968000",
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::<your bucketname>/*"
]
}
]
}