代码之家  ›  专栏  ›  技术社区  ›  aahrens

使用ActiveStorage和AmazonS3检索图像时的错误请求

  •  0
  • aahrens  · 技术社区  · 5 年前

    我得到了 Bad Request 400 当试图 GET S3的图像。我怀疑这可能是一个ACL问题,但我有点茫然,因为我能够成功地上传到S3,并在AWS控制台中看到图像文件。

    索引文件

    <%= image_tag url_for(post.photo) %>
    

    生成此链接的

    http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--aeb32f210c13064270e52b720f34901fda2bd5a6/Kevin.jpg
    

    这导致了 GET 400 Bad Request

    https://myapplication.s3.us-east-2.amazonaws.com/jEtGANzYnkc8FAZyV9pFWRtG?response-content-disposition=inline%3B%20filename%3D%22Kevin.jpg%22%3B%20filename%2A%3DUTF-8%27%27Kevin.jpg&response-content-type=image%2Fjpeg

    日志显示了这一点

    Started GET "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--aeb32f210c13064270e52b720f34901fda2bd5a6/Kevin.jpg"
    
    Processing by ActiveStorage::BlobsController#show as JPEG
    
    Parameters: {"signed_id"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBDQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--aeb32f210c13064270e52b720f34901fda2bd5a6", "filename"=>"Kevin"}
    
    ActiveStorage::Blob Load (2.5ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
    
    S3 Storage (1.5ms) Generated URL for file at key: jEtGANzYnkc8FAZyV9pFWRtG (https://myapplication.s3.amazonaws.com/jEtGANzYnkc8FAZyV9pFWRtG)
    
    Redirected to https://myapplication.s3.amazonaws.com/jEtGANzYnkc8FAZyV9pFWRtG
    
    Completed 302 Found in 10ms (ActiveRecord: 2.9ms)
    

    邮政银行

    class Post < ApplicationRecord
      belongs_to :user
      has_one_attached :photo
    end
    

    存储.yml,区域为 us-east-1

    amazon:
      service: S3
      access_key_id: <%= Rails.application.credentials.aws[:access_key_id] %>
      secret_access_key: <%= Rails.application.credentials.aws[:secret_access_key] %>
      region: <%= Rails.application.credentials.aws[:region] %>
      bucket: <%= Rails.application.credentials.aws[:bucket] %>
    

    环境/开发.rb

    config.active_storage.service = :amazon
    

    Posts_控制器.rb

     def create
      @post = current_user.posts.build(post_params)
    
      if @post.save
        flash[:notice] = 'Post has been saved!'
      else
        flash[:alert] = 'Unable to save'
      end
    
      redirect_to posts_path
     end
    
    
     private
    
     def post_params
       params.require(:post).permit(:content, :photo)
     end
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   aahrens    5 年前

    问题是我需要一个桶政策。类似的东西。

    {
    "Version": "2012-10-17",
    "Id": "Policy1548358022010",
    "Statement": [
        {
            "Sid": "Stmt1548358017405",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::number:user/name_of_a_user"
            },
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::name_of_my_bucket"
        }
      ]
    }
    
    推荐文章