0

我正在尝试使用carrierwave、fog 和谷歌云存储来保存我的图像文件。

所以当我提交数据和图像字段时

<ActionDispatch::Http::UploadedFile>

但收到错误“Unpermitted parameter: :format”。

这在 Amazon S3 上运行良好,但我已经为 gcp 付费了,所以我想在这里保存图像。

请给我一些提示。谢谢

image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end

雾.rb

CarrierWave.configure do |config|
    config.fog_provider = 'fog/google'
    config.fog_credentials = {
        provider:              'Goggle',
        aws_access_key_id:     ENV['GOOGLE_ACCESS_KEY'],
        aws_secret_access_key: ENV['GOOGLE_SEC_KEY'],
        #region:                'ap-northeast-1',
        #endpoint:              'https://s3-ap-northeast-1.amazonaws.com'
        endpoint:              'https://storage.googleapis.com'
    }
    config.fog_directory  = 'fullout-linemanager-storage'
    config.fog_public     = true
    config.fog_attributes = { }

    config.remove_previously_stored_files_after_update = true
end

反应.rb(模型)

class Reaction < ApplicationRecord
  mount_uploader :file, ImageUploader
end

反应控制器.rb

(...)
def reaction_params
    if params[:reaction].present?
      params[:reaction][:channel_id] = current_user.target_channel
      params[:reaction][:target_number] = 0
      params.require(:reaction).permit(
        :id, :name, :contents, :reaction_type ,:channel_id, :tag, :target_number,
        :image, :created_at, :updated_at, :match_option
        )
    else
      params[:channel_id] = current_user.target_channel
      params[:target_number] = 0
      params.permit(
        :id, :name, :contents, :reaction_type ,:channel_id, :tag, :target_number, :created_at, :updated_at, :match_option, :image
        )
    end
  end

日志

Parameters: {"name"=>"text_welcome_message", "reaction_type"=>"image", "contents"=>"[ NO TEXT ]", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f91f79a9968 @tempfile=#<Tempfile:/var/folders/jb/qnz4lt193kz7wzj06xrp9q5w0000gn/T/RackMultipart20200204-6757-1h92oyl.png>, @original_filename="Ad-2.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"Ad-2.png\"\r\nContent-Type: image/png\r\n">, "tag"=>"ALL", "match_option"=>"1"}
12:09:10 web.1       |   User Load (0.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
12:09:10 web.1       |   ↳ app/controllers/api/reactions_controller.rb:188
12:09:10 web.1       | Unpermitted parameter: :format
12:09:10 web.1       |   Option Load (7.7ms)  SELECT  `options`.* FROM `options` WHERE `options`.`id` = 1 LIMIT 1
12:09:10 web.1       |   ↳ app/controllers/api/reactions_controller.rb:63
12:09:10 web.1       |    (0.3ms)  BEGIN
12:09:10 web.1       |   ↳ app/controllers/api/reactions_controller.rb:64
12:09:10 web.1       |    (0.4ms)  ROLLBACK
12:09:10 web.1       |   ↳ app/controllers/api/reactions_controller.rb:64
12:09:10 web.1       | Completed 500 Internal Server Error in 21ms (ActiveRecord: 8.8ms)
4

2 回答 2

0

我相信您需要对提供的雾配置进行更多更改。首先,我认为您需要提供者Google而不是Goggle. 接下来,您需要将当前使用的 aws 相关凭证密钥更改为特定于 google 的密钥(我怀疑这是您的实际错误的原因,fwiw)。因此,您将需要and ) 而不是aws_access_key_idand 。您可以在这里的carrierwave自述文件中看到更多相关信息:https ://github.com/carrierwaveuploader/carrierwave#using-google-storage-for-developersaws_secret_access_keygoogle_storage_access_keygoogle_storage_secret_access_key

总而言之,生成的 fog.rb 看起来像这样:

CarrierWave.configure do |config|
    config.fog_provider = 'fog/google'
    config.fog_credentials = {
        provider:                         'Google',
        google_storage_access_key:        ENV['GOOGLE_ACCESS_KEY'],
        google_storage_secret_access_key: ENV['GOOGLE_SEC_KEY'],
    }
    config.fog_directory  = 'fullout-linemanager-storage'
    config.fog_public     = true
    config.fog_attributes = { }

    config.remove_previously_stored_files_after_update = true
end

希望有帮助!

于 2020-02-05T17:20:19.387 回答
0

无论您在前端应用程序中使用什么请求客户端,都在发送format控制器中显然不允许的参数。它可能只是一个无害的format: 'json'. 如果是这种情况,您可以使用except操作中的方法过滤此参数,问题可能会得到解决:

params.except(:format).require(:reaction).permit(:id, :name, :contents, :reaction_type ,:channel_id, :tag, :target_number, :image, :created_at, :updated_at, :match_option)

params.except(:format).permit(:id, :name, :contents, :reaction_type ,:channel_id, :tag, :target_number, :created_at, :updated_at, :match_option, :image)
于 2020-02-04T04:33:39.130 回答