Rails CarrierWave-能够保存远程URL图像,但无法将本地图像文件保存到数据库和AWS S3

时间:2019-01-11 00:05:30

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

当我发出发布“视觉”的POST请求时,它正确传递了两个必需参数。但是,保存后,图像列在HEROKU数据库中保留为零,并且也不在AWS S3中上载。

如何做到这一点,以便用户可以将本地文件上传到AWS和Heroku?

视野表:

body

vision.rb

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创建操作:

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

end

我正在这样发出POST请求(它可以正常工作,但只有在保存之前在visions_controller.rb中传递了远程URL时,图像列才会保存

        def create
            @vision = current_user&.visions.build(vision_params)
            @vision.remote_image_url = 'https://www.gstatic.com/webp/gallery3/1.sm.png'

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

setup_fog.rb

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_uploader.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

0 个答案:

没有答案