简体   繁体   English

Rails 4,Paperclip,Amazon S3配置Amazon Path

[英]Rails 4, Paperclip, Amazon S3 Config Amazon Path

I'm trying to configure the endpoint which is returned from paperclip when my object is successfully uploaded to Amazon's S3 service. 我正在尝试配置当我的对象成功上传到亚马逊的S3服务时从回形针返回的端点。 The upload and everything is working correctly, but the URL that is being returned is incorrect for displaying the upload. 上传和一切正常,但返回的URL不正确显示上传。

Right now, the url that is being returned is http://s3.amazonaws.com/path/to/my/items (as seen in the picture below). 现在,返回的网址是http://s3.amazonaws.com/path/to/my/items (如下图所示)。

Instead of s3.amazonaws.com , I would like the root to be specific to the bucket's location (eg s3-us-west-1.amazonaws.com/path/to/my/items ) 而不是s3.amazonaws.com ,我希望根特定于桶的位置(例如s3-us-west-1.amazonaws.com/path/to/my/items

在此输入图像描述

Where should I try and configure a different url path (from s3.amazonaws.com to something else)? 我应该在哪里尝试配置不同的URL路径(从s3.amazonaws.com到其他地方)? I've tried to add a url with the above path into my configuration file like: 我试图将上面路径的URL添加到我的配置文件中,如:

  #Paperclip Amazon S3
  config.paperclip_defaults = {
      :storage => :s3,
      :url => "https://s3-us-west-1.amazonaws.com/",
      :s3_credentials => {
          :bucket => ENV['S3_BUCKET_NAME'],
          :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
          :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']

      }

  }

Which did not appear to have any effect. 哪个似乎没有任何影响。 Please advise on where I should be setting this option! 请告知我应该在哪里设置此选项!

Thanks in advance! 提前致谢!

If you're going to use S3, we've found that you have to include the S3 credentials in your actual model (not just the config files). 如果您打算使用S3,我们发现您必须在实际模型中包含S3凭据(而不仅仅是配置文件)。 Here's what we do: 这就是我们的工作:

Model 模型

#Image Upload 
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
        :styles => { :medium => "x300", :thumb => "x100" },
        :default_url => "****",
        :storage => :s3,
        :bucket => '****',
        :s3_credentials => S3_CREDENTIALS,
            :url => "/:image/:id/:style/:basename.:extension",
            :path => ":image/:id/:style/:basename.:extension"

config/application.rb 配置/ application.rb中

  # Paperclip (for Amazon) (we use EU servers)
  config.paperclip_defaults = {
    :storage => :s3,
    :s3_host_name => 's3-eu-west-1.amazonaws.com'
  }

config/s3.yml 配置/ s3.yml

#Amazon AWS Config
development:
  access_key_id: **********
  secret_access_key: **************
  bucket: ****

production:
  access_key_id: ***********
  secret_access_key: ***********
  bucket: ****

Hope this helps? 希望这可以帮助?

I also had the same problem when migrating to Spree 2.2 and am still not sure how to solve it the correct way. 迁移到Spree 2.2时我也遇到了同样的问题,我仍然不确定如何以正确的方式解决它。 It seems like Paperclip should have been updating the path from the configuration, but it isn't. 似乎Paperclip应该从配置中更新路径,但事实并非如此。

Lacking a better solution, I've overridden the Spree::Image class like this: 由于缺乏更好的解决方案,我已经覆盖了Spree :: Image类,如下所示:

1 Spree::Image.class_eval do
2   has_attached_file :attachment, 
3     styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' },
4     default_style: :product,
5     url: '/spree/products/:id/:style/:basename.:extension',
6     path: 'products/:id/:style/:basename.:extension',
7     convert_options: { all: '-strip -auto-orient -colorspace sRGB' }·
8 end 

After some experimentation I have found that setting :s3_host_name globally suffices. 经过一些实验,我发现设置:s3_host_name全局就足够了。 I ended up with the same problem because I was setting :s3_region , which was being used by Paperclip (post-4.3.1, with aws-sdk 2) for storing attachments, but not when generating the URLs. 我最后:s3_region了同样的问题,因为我设置了:s3_region ,Paperclip(4.3.1之后,aws-sdk 2)用于存储附件,但在生成URL时却没有。

This may also be of interest to readers who end up on this problem: https://github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3 对于最终解决这个问题的读者来说,这也可能是有趣的: https//github.com/thoughtbot/paperclip/wiki/Restricting-Access-to-Objects-Stored-on-Amazon-S3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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