AccessDenied occurs when using CloudFront

26 views
Skip to first unread message

n....@bike-startup.co.jp

unread,
Jul 17, 2018, 11:52:18 PM7/17/18
to Dragonfly
I just started running Ruby on Rails app using Dragonfly.
S3-upload is succeeded, but when I use CDN(CloudFront) AccessDenied Err occurs.


Error message when I access the image file
```
<Error>
<script/>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
```


Error message on Google Chrome
```
Cross-Origin Read Blocking (CORB) blocked cross-origin response
```


Gemfile
```
gem 'dragonfly'
gem 'dragonfly-s3_data_store'
gem 's3_file_field'
```


initializers/dragonfly.rb
```
require 'dragonfly'

# Configure
Dragonfly.app.configure do
  plugin :imagemagick

  secret ENV['DRAGONFLY_SECRET']

  url_format "/media/:job/:name"

  url_host ENV['AWS_ASSET_HOST']

  datastore :s3,
    bucket_name: ENV['AWS_BUCKET'],
    access_key_id: ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
    region: ENV['AWS_REGION']
end

# Logger
Dragonfly.logger = Rails.logger

# Mount as middleware
Rails.application.middleware.use Dragonfly::Middleware

# Add model functionality
ActiveSupport.on_load(:active_record) do
  extend Dragonfly::Model
  extend Dragonfly::Model::Validations
end
```


model/post_image.rb
```
class PostImage < ApplicationRecord

  # Image Uploader
  dragonfly_accessor :image do
    storage_options :opts_for_storage
  end

  # association
  belongs_to :post

  # dragonfly storage path
  def opts_for_storage
    dir = Rails.env
    { path: "#{dir}/uploads/#{self.class.name.to_s.underscore}/#{Time.zone.now.strftime("%Y%m%d%H%M%S")}" }
  end

end
```


views/post_images/show.html.haml
```
= image_tag @post_image.image.thumb('500x500').url, size: '500x500'
```


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

So how can I fix the err?

Best Regards

Jun
Reply all
Reply to author
Forward
0 new messages