Rails CarrierWave-我无法将本地文件保存到S3和Heroku,但是远程URL可以很好地工作。我想念什么?

时间:2019-01-11 20:32:18

标签: ruby ruby-on-rails-4 amazon-s3 carrierwave fog

问题: 好吧,所以我花了最后三天,没有任何进展。我想念什么?我正在使用CarrierWaveFogmini_magick这些宝石来上传与react-native-image-picker一起使用本机的照片。

这是表格:

class CreateVisions < ActiveRecord::Migration[5.1]
  def change
    create_table :visions do |t|
      t.string :image
      t.text :description

      t.timestamps
    end
  end
end

visions_controller.rb:


  def create
    @vision = current_user&.visions.build(vision_params)
    @vision.remote_image_url = 'https://www.gstatic.com/webp/gallery3/1.sm.png' 
#without this line, whatever I pass as my image 
#parameter in my HTTP request on react native, it returns 
#'nil' for the image column. Without this line, even 
#when I pass a URL on react native, it saves the image column as nil.

    @vision.save
    render :create, status: :created
  end

vision.rb

class Vision < ApplicationRecord
    belongs_to :user
    mount_uploader :image, ImageUploader

end

响应本机HTTP请求

let response = await fetch('https://prana-app.herokuapp.com/v1/visions/', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-User-Email': this.state.email,
                'X-User-Token': this.state.accessToken
            },
            body: JSON.stringify({
              vision: {
                description: 'dsfsfdsfdsfsd',
                image: this.state.uploadFile


# image param is being overwritten by that one line of code in my create 
# action in the visions_controller.rb
              }
            })
        });

setup_fog.rb

CarrierWave.configure do |config|
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'] || '',
      aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY_ID'] || '',  
      region: 'us-west-2'
    }

    config.fog_directory = 'BUCKET_NAME'
    config.fog_public = false
    config.fog_attributes = {
      'Cache-Control' => "max-age=#{365.day.to_i}"
    }
  end

当我的visions_controller中没有那一行代码时: @vision.remote_image_url = 'https://www.gstatic.com/webp/gallery3/1.sm.png'

将本地图像路线作为图像参数传递给视觉对象,如下所示:

Parameters: {"vision"=>{"description"=>"dsfsfdsfdsfsd", "image"=>"file:///BLABLABLA.jpg"}}

但是,保存时,它另存为[[image:nil],[description:'dsfsfdsfdsfsd']]

为什么会这样? 我曾尝试在create动作中以各种方式注入传递的参数,但这似乎不起作用:

  1. @vision.remote_image_url = params[:image]
  2. @vision.remote_image_url = params[:image][:tempfile]
  3. @vision.image = params[:image]

如何成功传递本地图像文件以保存到S3和HEROKU?

0 个答案:

没有答案