簡體   English   中英

嘗試使用Paperclip和Amazon API保存到S3

[英]Trying to save to S3 with Paperclip & Amazon API

嘿伙計們,我已經嘗試查找我的具體問題的答案,但找不到解決方案。 我嘗試了一切。 我有一個禮品模型,有回形針來保存圖像。 禮品實例由亞馬遜模型填充(該模型從亞馬遜api獲取產品詳細信息,包括圖像,名稱和價格),但我無法將圖像保存到S3。 我認為我的問題與回形針將其轉換為禮品控制器中“新”路線上的亞馬遜S3鏈接的事實有關,而不是實際創建禮品時。 這是一些代碼:

class GiftsController < ApplicationController
  skip_before_action :verify_authenticity_token
  before_action :amazon_url, only: [:new, :new_extension]
  before_action :authenticate_user!

  def new
    @friend = Friend.find_by_id(params[:id])
    if @gift_data.failure == nil
      @gift = Gift.new(
          name: @gift_data.name,
          image: @gift_data.image,
          price: @gift_data.price
      )
    else
      @gift = Gift.new()
      if params[:search]
        @failure = @gift_data.failure
        respond_to do |format|
          format.html
          format.json { render :json => {gift: @gift} }
        end
      end
    end
  end

和我的禮物模型:

class Gift < ActiveRecord::Base
  belongs_to :category
  has_many :ideas
  has_many :friends, :through => :ideas
  # this friends association doesn't work, need to troubleshoot more or figure that out
  # also figure out whether we need it
  has_many :goods


  has_attached_file :image,
                    :storage  => :s3,
                    :styles => { :medium => "370x370", :thumb => "100x100" }
                    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  validates_presence_of :name
  # validates_presence_of :category_id
  # validates :url, :url => {:allow_blank => false}
  # validates :price, presence: true, format: {with: /\A\d+(?:\.\d{0,2})?\z/}, numericality: {greater_than: 0, less_than: 1000000}

end

我已經嘗試在禮物模型中添加一個方法來URI.parse鏈接,但它不起作用。 甚至嘗試在圖像上的禮品控制器中進行,但這也不起作用。 我假設回形針正在處理圖像,即使它尚未創建,甚至不會讓我之后創建禮物。 會很感激一些指導!

弄清楚了。 結束在表中添加image_url行,然后使用URI.parse(image_url)將其保存到圖像,並將其保存到S3。

暫無
暫無

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

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