简体   繁体   English

尝试使用Paperclip和Amazon API保存到S3

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

Hey guys I've tried looking up the answer to my specific problem and couldn't find a solution. 嘿伙计们,我已经尝试查找我的具体问题的答案,但找不到解决方案。 I tried everything. 我尝试了一切。 I have a gift model that has paperclip to save an image. 我有一个礼品模型,有回形针来保存图像。 The gift instance is populated by an Amazon model ( which grabs information from the amazon api for product details including, image, name and price) but I can't save the image to S3. 礼品实例由亚马逊模型填充(该模型从亚马逊api获取产品详细信息,包括图像,名称和价格),但我无法将图像保存到S3。 I think my problem has to do with the fact paperclip is converting it to an amazon S3 link on the "new" route in my gift controller as opposed to when it actually creates the gift. 我认为我的问题与回形针将其转换为礼品控制器中“新”路线上的亚马逊S3链接的事实有关,而不是实际创建礼品时。 here's some code: 这是一些代码:

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

and my gift model : 和我的礼物模型:

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

I've tried adding a method in the gift model to URI.parse the link but it doesn't work. 我已经尝试在礼物模型中添加一个方法来URI.parse链接,但它不起作用。 Even tried doing it in the gift controller on the image but that didn't work either. 甚至尝试在图像上的礼品控制器中进行,但这也不起作用。 I'm assuming paperclip is processing the image even when it's not yet created and won't even let me create the gift afterwards. 我假设回形针正在处理图像,即使它尚未创建,甚至不会让我之后创建礼物。 Would appreciate some guidance! 会很感激一些指导!

Figured it out. 弄清楚了。 Ended up adding an image_url row in table and then making use of URI.parse(image_url) to save it to the image and it saves it to S3. 结束在表中添加image_url行,然后使用URI.parse(image_url)将其保存到图像,并将其保存到S3。

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

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