簡體   English   中英

使用 Google Cloud Storage 和 Carrierwave 保存圖像時出錯

[英]Errors in saving image with Google Cloud Storage and Carrierwave

我使用了carrierwave、fog和AWS S3來保存我的圖像,

但我需要將這種方式更改為 Google Cloud Storage。

我找到了一些使用霧谷歌的方法,但這是過時的。

所以我用沒有霧的carrierwave-google-storage gem嘗試了這個,

但效果不佳。

我在carrierwave-google-storage git page 中做了完全相同的過程。

(鏈接: https : //github.com/metaware/carrierwave-google-storage

我附上了我的 image_uploader.rb、carrierwave.rb 和日志消息。

image_uploader.rb

class ImageUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :gcloud

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url(*args)
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process scale: [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process resize_to_fit: [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end
end

載波

CarrierWave.configure do |config|
  config.storage                             = :gcloud
  config.gcloud_bucket                       = 'fullout-linemanager-storage'
  config.gcloud_bucket_is_public             = false
  config.gcloud_authenticated_url_expiration = 600
  # config.gcloud_content_disposition          = 'attachment' // or you can skip this

  config.gcloud_attributes = {
    expires: 600
  }
  config.gcloud_credentials = {
    gcloud_project: "fullout-test",
    gcloud_keyfile: "../fullout-test-01ed5e95d6fd.json"
  }
end

開發日志

(...)
app/controllers/application_controller.rb:13:in `render_500'
Started POST "/api/reactions" for ::1 at 2020-01-31 11:31:35 +0900
Processing by Api::ReactionsController#create as JSON
  Parameters: {"name"=>"1", "reaction_type"=>"image", "contents"=>"[ NO TEXT ]", "tag"=>"ALL", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007fc4b8976a38 @tempfile=#<Tempfile:/var/folders/jb/qnz4lt193kz7wzj06xrp9q5w0000gn/T/RackMultipart20200131-12922-1n5x9ks.png>, @original_filename="disney2500.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image\"; filename=\"disney2500.png\"\r\nContent-Type: image/png\r\n">}
  [1m[36mUser Load (0.5ms)[0m  [1m[34mSELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1[0m
  ↳ app/controllers/api/reactions_controller.rb:188
Unpermitted parameter: :format
  [1m[36mOption Load (0.3ms)[0m  [1m[34mSELECT  `options`.* FROM `options` WHERE `options`.`id` IS NULL LIMIT 1[0m
  ↳ app/controllers/api/reactions_controller.rb:63
  [1m[35m (0.5ms)[0m  [1m[35mBEGIN[0m
  ↳ app/controllers/api/reactions_controller.rb:64
  [1m[36mReaction Create (0.5ms)[0m  [1m[32mINSERT INTO `reactions` (`name`, `contents`, `reaction_type`, `channel_id`, `tag`, `target_number`, `image`, `created_at`, `updated_at`) VALUES ('1', '[ NO TEXT ]', 'image', '1609035039', 'ALL', 0, 'disney2500.png', '2020-01-31 02:31:35', '2020-01-31 02:31:35')[0m
  ↳ app/controllers/api/reactions_controller.rb:64
  [1m[35m (41.1ms)[0m  [1m[31mROLLBACK[0m
  ↳ app/controllers/api/reactions_controller.rb:64
Completed 500 Internal Server Error in 80ms (ActiveRecord: 43.0ms)
(...)

反應控制器.rb

def create
    @reaction = Reaction.new(reaction_params)
    option_id = params[:match_option]
    tags = params[:tag].split(",")
    @option = Option.find_by(id: option_id)
    if @reaction.save   # <----reactions_controller.rb:64
      option_update(@option)
      tag_create(tags)
      render json: @reaction, status: :ok
    else
      render json: @reaction.errors, status: :unprocessable_entity
    end
  end

反應.rb(模型)

class Reaction < ApplicationRecord
  mount_uploader :image, ImageUploader
end

我建議您使用官方的 Ruby 雲存儲客戶端庫

要上傳文件,您可以執行以下操作:

# project_id        = "Your Google Cloud project ID"
# bucket_name       = "Your Google Cloud Storage bucket name"
# local_file_path   = "Path to local file to upload"
# storage_file_path = "Path to store the file in Google Cloud Storage"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name

file = bucket.create_file local_file_path, storage_file_path

puts "Uploaded #{file.name}"

文檔中,您可以找到更多示例,用於使用 Ruby 在 GCS 中執行其他任務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM