简体   繁体   中英

Using carrierwave to upload images to google cloud storage, the file name ends up being saved and not the public link to the image in the bucket

I'm trying to implement image upload to google cloud storage from my rails 4.2 app using the carrierwave gem. Whenever I go to upload the image it is uploaded to the bucket fine but its saved in the db as the original image name eg image.png , not as the google cloud storage public link for the image eg https://storage.googleapis.com/project/bucket/image.png

Not too sure what is needed to be done from here to get it saving the public link from the bucket and not just the file name.

carrierwave.rb file

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider:                         'Google',
    google_storage_access_key_id:     'key',
    google_storage_secret_access_key: 'secret key'
  }
  config.fog_directory = 'bucket-name'
end

uploaders/check_item_value_image_uploader.rb

class CheckItemValueImageUploader < 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 :fog

  # 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
    "check-item-value-images/#{model.id}"
  end

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

end

related gems

gem 'gcloud'
gem "fog"
gem 'google-api-client', '~> 0.8.6'
gem "mime-types"

check_category_item_value model

mount_uploader :value, CheckItemValueImageUploader

check_category_item_value update method

if @check_category_item_value.save 
   flash[:success] = "Successfully updated"
   redirect_to category_items_edit_path(@guide, @category, @category_item)
else
   render 'category_items/edit'
end

edit form

 <%= form_for(@check_category_item_value) do |f| %>
   <%= f.file_field :value, :value => item_key.value, accept: "image/jpeg, image/jpg, image/gif, image/png" %>
   <%= f.submit "Submit" %><hr>
 <% end %>

The forms works fine but the value saved is the original image name not the google cloud storage public link for the image.

I used the carrierwave docs , this post , and this video by google cloud platform to get what I have now. What am I missing?

update

adding config.fog_public = true does nothing

CarrierWave.configure do |config|
  config.fog_credentials = {
    provider:                         'Google',
    google_storage_access_key_id:     'key',
    google_storage_secret_access_key: 'secret key'
  }
  config.fog_public = true
  config.fog_directory = 'bucket-name'
end

To set the link public, please check this config this in your config file:

# You may set it false now
config.fog_public = true

For filename you may overwrite in your CheckItemValueImageUploader , here is an example:

class CheckItemValueImageUploader < CarrierWave::Uploader::Base
  def filename
    "#{model.id}-#{original_filename}.#{file.extension}" if original_filename.present?
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM