upyun结合carriwave实现图片的云同步

1.rails 项目中安装carriwave  如何使用我已经在博客中写到

 

2.申请又拍云https://www.upyun.com  申请账号登录 ,购买空间和流量

 

3.安装又拍云,又拍云结合carriwave插件

 

   gem 'carrierwave''0.10.0'  #图片上传

   gem 'upyun''~> 1.0.5'

   gem "carrierwave-upyun" 

   gem "rest-client"

   然后:      

   bundle install

 

 

4.在rails项目的/config/initializers/ 目录下添加 carrierwave.rb 文件内容如下

 

CarrierWave.configure do |config|

      config.storage = :upyun

      config.upyun_username = "管理员姓名"

      config.upyun_password = ”密码“

      config.upyun_bucket = "空间名"

     config.upyun_bucket_host = "默认域名"   (也就是又拍云主机将来存储图片的访问地址)

end

 

 

5. 修改carriwave 的配置文件例如PicUploader

 

class PicUploader < CarrierWave::Uploader::Base

      include CarrierWave::MiniMagick

      storage :upyun

 

      def store_dir

             "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

      end 

 

     def  extension_white_list

            %w(jpg jpeg gif png)

     end 

end

 

6. 在本地启动项目上传一张图片,该图片会存到 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 这个目录下,例如我的项目model的名字叫

mall_brands_offs那么图片会存到你本地的项目中该uploads/mall_brands_offs/目录下,同时也会被存到

    happyteam.b0.upaiyun.com/uploads/mall_brands_off/brand_image/15/basketball.jpg

 

7.访问上面的路径就可以看到你上传的图片被同步到了又拍云,那么也就实现了云同步。

 

8.为什么要用到又拍云?又拍云不仅仅支持图片的云同步,同时也支持cdn加速,又拍云在国内每个省份都会有自己的节点,支持域名绑定,加快图片缓存到你应用的速度。

 

posted on 2015-08-20 15:59  技生瑜合声靓  阅读(171)  评论(0编辑  收藏  举报

导航