如何将图像上传到s3存储桶 [英] how to upload image to s3 bucket
问题描述
我正在尝试将名为"quotes.png"的图像上传到Ionic 2应用程序中的s3存储桶(使用节点不使用),但显示网络故障".
I am trying to upload an image named 'quotes.png' to s3 bucket in my Ionic 2 apps (WITHOUT using node) but it says 'Network Failure'.
这是我的代码的样子(省略了敏感信息):
This is how my codes look like (with sensitive info omitted):
import AWS from 'aws-sdk';
import S3 from 'aws-sdk/clients/s3';
AWS.config.update({ accessKeyId: 'myaccesskeyid', secretAccessKey: 'mysecretaccesskey' })
var s3 = new AWS.S3();
var params = {
Bucket: 'mybucketname',
Key: 'assets/img/quotes.png',
Body: "hello"
};
s3.putObject(params, function (err, res) {
if (err) {
console.log(err);
} else {
debugger;
console.log("Successfully uploaded data to myBucket/myKey");
}
});
(更新),这是错误的详细信息:
(UPDATED)This is the details of the error:
XMLHttpRequest cannot load https://mybucket-name.s3-ap-southeast-
1.amazonaws.com/assets/img/quotes.png. Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is
present on the requested resource. Origin 'http://localhost:8100' is
therefore not allowed access. The response had HTTP status code 403.
Error: Network Failure
at XMLHttpRequest.<anonymous> (xhr.js:52)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (ng_zone.js:227)
at t.invokeTask (polyfills.js:3)
at e.runTask (polyfills.js:3)
at XMLHttpRequest.invoke (polyfills.js:3)
注意:我不在我的Ionic 2应用程序中使用节点js.我正在使用带有Angular 2的Parse JavaScript SDK,AWS S3,AWS EC2,Elastic beanstalk和Ionic 2.
NOTE: I am NOT using node js in my Ionic 2 app. I am using Parse JavaScript SDK, AWS S3, AWS EC2, Elastic beanstalk and Ionic 2 with Angular 2.
问题是:我编写的代码是否正确/我的代码有问题吗?谢谢:)
So the question: Did I write my code properly/is there something wrong with my code? Thanks :)
推荐答案
您需要在AWS.config
中指定存储桶所在的区域.
You need to specify the region where the bucket is located in AWS.config
.
示例:
AWS.config.update({region: 'us-west-2'});
在调用new AWS.S3()
之前插入此行.
这篇关于如何将图像上传到s3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!