gpt4 book ai didi

ruby-on-rails - 使用 carrierwave 将图像上传到谷歌云存储,文件名最终被保存而不是存储桶中图像的公共(public)链接

转载 作者:数据小太阳 更新时间:2023-10-29 08:07:20 24 4
gpt4 key购买 nike

我正在尝试使用 carrierwave gem 从我的 Rails 4.2 应用程序将图像上传到谷歌云存储。每当我上传图像时,它都会很好地上传到存储桶中,但它会作为原始图像名称保存在数据库中,例如image.png,而不是图片的谷歌云存储公共(public)链接,例如https://storage.googleapis.com/project/bucket/image.png

不太确定从这里需要做什么才能从存储桶中保存公共(public)链接而不仅仅是文件名。

carrierwave.rb文件

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

上传者/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

相关 gem

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

check_category_item_value 模型

mount_uploader :value, CheckItemValueImageUploader

check_category_item_value更新方法

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

编辑表格

 <%= 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 %>

表格工作正常,但保存的值是原始图像名称,而不是图像的谷歌云存储公共(public)链接。

我使用了 carrierwave docs , this post , 和 this video通过谷歌云平台获得我现在拥有的。我错过了什么?

更新

添加 config.fog_public = true 什么都不做

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

最佳答案

要设置公开链接,请在您的配置文件中检查此配置:

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

对于文件名,您可以在 CheckItemValueImageUploader 中覆盖,这是一个示例:

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

关于ruby-on-rails - 使用 carrierwave 将图像上传到谷歌云存储,文件名最终被保存而不是存储桶中图像的公共(public)链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com