Aleksandr Panasyuk
Aleksandr Panasyuk

Reputation: 333

How to generate S3 expiring_url for custom host name?

I have the S3 bucket (named 'img.example.com') and want to generate S3 expiring url with custom host name for paperclip attachment. Code:

model:

#app/models/my_model.rb
...
has_attached_file :attachment, s3_url_options: { endpoint: 'img.example.com' }
...

config:

#config/initializers/paperclip.rb
...
Paperclip::Attachment.default_options[:s3_host_alias] = 'img.example.com'
...

#config/environments/development.rb
...
config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
      :bucket => 'img.example.com',
      :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
      :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  },
  :s3_permissions => :private
}
...

And #expiring_url generates this: "http://img.example.com/[bucket name]/[path]?[params]"

But i want it to generate url like this: "http://img.example.com/[path]?[params]"

rails 4.1 paperclip 4.1.1 aws-sdk 1.11.1

Upvotes: 0

Views: 981

Answers (1)

user740584
user740584

Reputation:

You need to define :url in config.paperclip_defaults. By default the url will use ":s3_path_url" which includes the bucket. i.e. <protocol>//<host_name>/<bucket>/<path>

config.paperclip_defaults = {
  ...
  :url => ":s3_alias_url"
  ...
}

This will generate a url in the format <protocol>//<host_alias>/<path>.

Upvotes: 0

Related Questions