Gem [已解决] 图片在网页后台能通过 carrierwave-aliyun 上传到阿里云 OSS,却无法在网页前台显示

yixiu2017 · 2017年07月13日 · 最后由 yixiu2017 回复于 2017年07月14日 · 2667 次阅读

我在 rails 上建站,目前遇到了问题,请大家帮忙看看,之前把图片上传到七牛云都是正常显示的。谢谢。 我将上传到阿里云 oss 的图片地址复制到浏览器是可以打开的。

bucket 设为公共读。

安装的 gem 如下:

gem 'carrierwave'
gem 'mini_magick'
gem 'carrierwave-qiniu'
gem 'qiniu-rs'
gem 'figaro'
gem 'rest-client'
gem 'carrierwave-aliyun'

carrierwave.rb 配置如下

CarrierWave.configure do |config|
  config.storage              = :aliyun
  config.aliyun_access_id     = ENV["aliyun_access_id"]
  config.aliyun_access_key    = ENV["aliyun_access_key"]
  config.aliyun_bucket        = ENV["aliyun_bucket"]
  config.aliyun_host          = "https://xxx.oss-cn-shenzhen.aliyuncs.com"   #oss 给我的外网域名,我没有自定义
  config.aliyun_internal      = false
  config.aliyun_area          = "cn-shenzhen"
  config.aliyun_private_read  = false
end

image_uploader.rb 配置如下

 version :thumb do
   process resize_to_fit: [50, 50]
 end

 version :small do
   process resize_to_fit: [250, 250]
 end

 version :middle do
   process resize_to_fit: [800, 600]
 end

 version :large do
   process resize_to_fit: [1200, 768]
 end

if Rails.env.production? #远端
   storage :aliyun
 elsif Rails.env.development? #本地
   storage :file
 end

view 是这样设置的

<% if introduction.image.present? %>
  <%= link_to image_tag(introduction.image.small.url, class: "img-responsive center-block  introduction-image"), introduction_path(introduction) %>
<% else %>
  <%= link_to image_tag("http://placehold.it/250x250&text=No Pic", class: "img-responsive center-block"), introduction_path(introduction) %>
<% end %>

会不会是读写权限的问题

Bucket ACL 改成公共读试一下

2 楼 已删除
superkun 回复

我是设为公共读的,谢谢

  1. 先确定是否文件成功上传到 Aliyun OSS 了。
  2. 图片裁剪的功能,可以靠 OSS 自带的效果更佳。
  3. introduction.image.small.url 生成的 URL 发出来看看
  4. introduction.image.url 生成的 URL 是否能打开?发出来看看

image_uploader 有没有处理 filename, 中文文件名生成的 url 可能会有问题呢

42thcoder 回复

英文也试过了,不行。谢谢

huacnlee 回复

http://yixiu2017.oss-cn-shenzhen.aliyuncs.com/uploads/introduction/image/14/logo2.png http://yixiu2017.oss-cn-shenzhen.aliyuncs.com/uploads/introduction/image/14/small_logo2.png 这两个是通过我的网站后台上传到阿里云后,oss 给出的图片地址。 我的网站前台没能找图片地址,返回的是 no pic。谢谢 移除 no pic 会报错:“nil is not a valid asset source”

问题解决了,谢谢大家。 views 设置要微调 <% if introduction.image.present? %> 改 为 <% if introduction.image.url.present? %>

需要 登录 后方可回复, 如果你还没有账号请 注册新账号