How to retrieve attachment url with Rails Active Storage with S3

后端 未结 5 1312
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 20:19
rails version 5.2

I have a scenario that I need to access the public url of Rails Active Storage with Amazon s3 to make a zip file with Sidekiq bac

5条回答
  •  既然无缘
    2020-12-13 20:58

    My use case was to upload images to S3 which would have public access for ALL images in the bucket so a job could pick them up later, regardless of request origin or URL expiry. This is how I did it. (Rails 5.2.2)

    First, the default for new S3 bucked is to keep everything private, so to defeat that there are 2 steps.

    1. Add a wildcard bucket policy. In AWS S3 >> your bucket >> Permissions >> Bucket Policy
    {
        "Version": "2008-10-17",
        "Statement": [
            {
                "Sid": "AllowPublicRead",
                "Effect": "Allow",
                "Principal": "*",
                "Action": "s3:GetObject",
                "Resource": "arn:aws:s3:::your-bucket-name/*"
            }
        ]
    }
    
    1. In your bucket >> Permissions >> Public Access Settings, be sure Block public and cross-account access if bucket has public policies is set to false

    Now you can access anything in your S3 bucket with just the blob.key in the url. No more need for tokens with expiry.

    Second, to generate that URL you can either use the solution by @Christian_Butzke: @post.header_image.service.send(:object_for, @post.header_image.key).public_url

    However, know that object_for is a private method on service, and if called with public_send would give you an error. So, another alternative is to use the service_url per @George_Claghorn and just remove any params with a url&.split("?")&.first. As noted, this may fail in localhost with a host missing error.

    Here is my solution or an uploadable "logo" stored on S3 and made public by default:

    #/models/company.rb
    has_one_attached :logo
    def public_logo_url
        if self.logo&.attachment
            if Rails.env.development?
                self.logo_url = Rails.application.routes.url_helpers.rails_blob_url(self.logo, only_path: true)
            else
                self.logo_url = self.logo&.service_url&.split("?")&.first
            end
        end
        #set a default lazily
        self.logo_url ||= ActionController::Base.helpers.asset_path("default_company_icon.png")
    end
    

    Enjoy ^_^

提交回复
热议问题