当前位置: 首页 > 工具软件 > Paperclip > 使用案例 >

paperclip-qiniu

雍阳
2023-12-01

使用七牛云上传静态资源

paperclip-qiniu
https://github.com/lidaobing/paperclip-qiniu/

gem 'paperclip'
gem 'paperclip-qiniu'
# application.yml

QINIU_ACCESS_KEY: xxxxx
QINIU_SECRET_KEY: xxxxx
QINIU_HOST: 'xxxx'
QINIU_BUCKET: 'xxx'
# config/initializers/paperclip.rb

Paperclip::Attachment.default_options[:storage] = :qiniu
Paperclip::Attachment.default_options[:qiniu_credentials] = {
  :access_key => ENV['QINIU_ACCESS_KEY'] || raise("set env QINIU_ACCESS_KEY"),
  :secret_key => ENV['QINIU_SECRET_KEY'] || raise("set env QINIU_SECRET_KEY")
}
Paperclip::Attachment.default_options[:bucket] = 'paperclip-qiniu-example'
Paperclip::Attachment.default_options[:use_timestamp] = false
Paperclip::Attachment.default_options[:qiniu_host] =
  'http://cdn.example.com'
# model

class Image < ActiveRecord::Base
  attr_accessible :file
  has_attached_file :file, :path => ":class/:attachment/:id/:basename.:extension"
  validates :file, :attachment_presence => true
  validates_attachment_content_type :file, :content_type => /\Aimage\/.*\Z/
end
# view

<%= qiniu_image_tag @image.file.url, :thumbnail => '300x300>', :quality => 80 %>
or
<%= image_tag qiniu_image_path(@image.file.url, :thumbnail => '300x300>', :quality => 80) %>

相关参考:
http://paperclip-qiniu-example.herokuapp.com/

Deleting an Attachment (删除图片文件)
Set the attribute to nil and save.
@user = User.find id
@user.avatar = nil
@user.save

 类似资料:

相关阅读

相关文章

相关问答