簡體   English   中英

使用Paperclip / Rails為AWS S3正確設置URL / PATH

[英]Setting URL/PATH correctly for AWS S3 by using Paperclip/Rails

我有兩個模型: CollectionLetter Collection有很多Letter並且Letter顯然屬於Collection

以下是我的Letter.rb文件:

class Letter < ActiveRecord::Base
    belongs_to :collection

    has_attached_file :pdf,
                    :url => "/pdf/:id/:style/:basename.:extension",
                    :path => "/pdf/:id/:style/:basename.:extension",
                    :s3_host_name => host_name,
                    :storage => :s3,
                    :bucket  => ENV['S3_BUCKET_NAME'],
                    :s3_credentials => {
                                        :access_key_id =>#ENV['AWS_ACCESS_KEY_ID'],
                                        :secret_access_key =>ENV['AWS_SECRET_ACCESS_KEY']
                                    }

    validates_attachment :pdf,
                     :content_type => {
                         :content_type =>
                             ["application/pdf", "text/plain", /\Aimage\/.*\Z/, "application/msword"]
                     }

end

我對has_attached_file urlpath屬性有疑問。

我不想在路徑中設置字母模型的ID,而是要設置集合的ID。 另外,我也想把title作為Letter的屬性。 比方說, @collection的ID為1 @collection有@ letter1和@ letter2。 將文件保存到AWS S3時,我想將其保存在/pdf/1(which is collection_id)/:title 如何在urlpath編寫此內容?

在回形針中,您可以使用插值法。

你的has_attached_file方法看起來像這樣;

has_attached_file :image, :default_url => "/pdf/:collection_id/:title/:basename.:extension"

config/initializers目錄中創建一個名為paperclip.rbinterpolations.rb的插值文件(rails在啟動時會拾取該文件夾中的任何腳本),其中包含類似於以下代碼的代碼;

Paperclip.interpolates :collection_id do |attachment, style|
    attachment.instance.collection_id
end

以相同的方式添加:title插值; 將其添加到您的has_attached_file網址中,並為此創建第二個插值。

您可以在https://github.com/thoughtbot/paperclip/wiki/Interpolations上了解更多信息

對於您的情況,我建議在URL中也包含Letter的:id,因為用戶可能會上傳兩個標題相同的文檔,這可能會產生沖突。

has_attached_file :image, :default_url => "/pdf/:collection_id/:id/:title"

回形針默認使用插值:basename:extension:style為文件創建唯一路徑。

  • :basename是上載文件的基本文件名
  • :extension是該上傳文件的擴展名
  • :style是該上傳文件的“樣式”或大小

您可以指定多種樣式(例如各種版本的縮略圖)。 默認樣式為“原始”,它將包含原始上載的文件。

在此處閱讀有關樣式的更多信息; https://github.com/thoughtbot/paperclip/wiki/縮略圖生成

始終嘗試保留原始文件,以備將來使用。 當您的網站/應用程序布局更改並且需要新的縮略圖大小時。 您可以從原始版本重建/重新生成整個縮略圖庫。

在此處閱讀有關生成/重新生成縮略圖的更多信息。 https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation#generatingregenerating-your-thumbnails

將gem aws-sdk用於ror應用程序以將圖像上載到s3將圖像上載到具有每個對象或應用程序不同文件夾的固定存儲桶。 s3限制了存儲桶創建的數量,而存儲桶內的內容沒有限制。

此代碼將使用aws-sdk gem將用戶的圖片上傳到s3。 將存儲桶和上傳的圖像公開,以便直接訪問上傳的圖像。 輸入的內容是圖像的完整路徑,要上傳圖像的文件夾以及要為其上傳圖像的user_id。

  def save_screenshot_to_s3(image_location, folder_name,user_id)
    service = AWS::S3.new(:access_key_id => ACCESS_KEY_ID,
                          :secret_access_key => SECRET_ACCESS_KEY)
    bucket_name = "app-images"
    if(service.buckets.include?(bucket_name))
      bucket = service.buckets[bucket_name]
    else
      bucket = service.buckets.create(bucket_name)
    end
    bucket.acl = :public_read
    key = folder_name.to_s + "/" + File.basename(image_location)
    s3_file = service.buckets[bucket_name].objects[key].write(:file => image_location)
    s3_file.acl = :public_read
    user = User.where(id: user_id).first
    user.image = s3_file.public_url.to_s
    user.save
  end   

使用key = folder_name.to_s +“ /” + File.basename(image_location)來定制您想要的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM