简体   繁体   中英

How do I reference images in my S3 bucket from local dev app?

Please see: On Amazon S3 I'm able to give an individual object download rights but cannot for the entire bucket for the next evolution of this question.

I've got a bucket with all my images uploaded. I'm trying to access them via their url as instructed by https://devcenter.heroku.com/articles/s3 . I of course want to make sure I can access them before I upload my latest changes to my live app. I've got the yml set up appropriately as far as I can tell, but the images are not loading when I visit the site running locally on WEBRICK. I have not made any changes to the AWS files in my rubygems directory. Here's an example of the reference I'm using to display the image:

<%= image_tag("https://s3-us-west-2.amazonaws.com/mybucket/someimage.jpg") %>

Can anyone provide me with some instructions? I know there are instructions for adding code to the included AWS files, but I cannot understand them enough to go in there and monkey around. Thanks.

I added the following to my bucket policy:

{
  "Id": "somepolicy",
  "Statement": [
    {
      "Sid": "someid",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::myapp/mykey",
      "Principal": {
        "AWS": [
          "*"
        ]
      }
    }
  ]
}

I am still unable to access the images. Is GetObject the appropriate action to allow?

You should be using the paperclip gem for image uploading. I know it doesn't seem necessary, but when it comes to allowing users to upload to disk, it's better to leave it to the professionals. (Why re-invent the wheel?).

With paperclip, you can do things like

User.profile_pic.url

or

User.images.first.image.url

I certainly think it's the way to go.

Paperclip is great solution for uploading and serving files but it sounds like you have already uploaded all of the content you intend to serve. This is a permissions issue with your S3 bucket.

Amazon Sample bucket policies:
http://docs.aws.amazon.com/AmazonS3/latest/dev/AccessPolicyLanguage_UseCases_s3_a.html

Big thanks to everyone that worked with me on this problem, especially @LarryMcKenzie. The problem is that you need to set file permissions on upload and there is no way to mass assign permissions to all objects in a bucket.

Solution: I reuploaded all of the images with the Open/Download flag set to true for everyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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