[不允许的参数::使用Google Cloud Storage保存图像时的格式

问题描述 投票:0回答:2

我正在尝试使用载波,雾气和Google云存储来保存图像文件。

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

<ActionDispatch::Http::UploadedFile>

但出现错误“不允许的参数::格式”。

这在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

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

reaction.rb(Model)

class Reaction < ApplicationRecord
  mount_uploader :file, ImageUploader
end

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

log

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)
ruby-on-rails google-cloud-storage carrierwave fog
2个回答
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)

0
投票

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

总的来说,生成的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

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.