简体   繁体   English

无法在Amazon S3上放置图像

[英]Unable to PUT image on Amazon S3

I want to upload a image file (base64) on amazon S3 but i am continously getting this error : 我想在亚马逊S3上上传图像文件(base64),但我不断收到此错误:

在此处输入图片说明

I am building an IONIC app and my code is : 我正在构建IONIC应用 ,我的代码是:

    var reader = new FileReader();

            reader.readAsDataURL(value);

            reader.onloadend = function(e) {
                console.log(this.result);

                // AWS
                AWS.config.update({ 
                    accessKeyId: $rootScope.config.access_key, 
                    secretAccessKey: $rootScope.config.secret_key
                });

                AWS.config.region = 'us-west-2';

                var bucket = new AWS.S3({ params: { Bucket:     $rootScope.config.bucket } });

                var params = { 
                    Key: value.name, 
                    ContentType: value.type, 
                    Body: this.result, 
                    ServerSideEncryption: 'AES256',
                    ACL: "public-read"
                };

                bucket.putObject(params, function(err, data) {
                  if(err) {
                    // There Was An Error With Your S3 Config
                    // alert(err.message);
                    console.log(JSON.stringify(err));
                    return false;
                  }
                  else {
                    // Success!
                    console.log('Upload Done');
                  }
                })
                .on('httpUploadProgress',function(progress) {
                  // Log Progress Information
                  console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
                });

Where value contains a dataURI . 其中value包含dataURI

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAA..... How to solve this problem ? data:image / jpeg; base64,/ 9j / 4AAQSkZJRgABAQAAAQABAA .....如何解决这个问题?

In your Amazon S3 account go to bucket "Properties" -> "Permissions" -> "Edit CORS Configuration" 在您的Amazon S3帐户中,转到存储桶“属性”->“权限”->“编辑CORS配置”

Set: 组:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
        <AllowedHeader>Content-Type</AllowedHeader>
        <AllowedHeader>Origin</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

You can restrict the allowed origin to only your host (eg " http://www.example.com " in <AllowedOrigin> instead of * ), but since your bucket is only writable with authentication anyway, this shouldn't be necessary. 您可以将允许的来源仅限制为您的主机(例如, <AllowedOrigin>http://www.example.com ”,而不是* ),但是由于您的存储桶无论如何都只能通过身份验证进行写入,因此这不是必需的。

I had to use a more permissive config to get it to work: 我必须使用更宽松的配置才能使其正常工作:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule> 
</CORSConfiguration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM