GithubHelp home page GithubHelp logo

carrierwave-upyun's Introduction

CarrierWave for UpYun (又拍云存储)

This gem adds support for upyun.com to CarrierWave

Gem Version Build Status Code Climate

Installation

$ bundle add carrierwave-upyun

Or modify your Gemfile to add:

gem "carrierwave-upyun"

NOTE: 此 Gem 是一个 CarrierWave 的组件,你需要配合 CarrierWave 一起使用。

Configuration

You'll need to configure the to use this in config/initializers/carrierwave.rb

CarrierWave.configure do |config|
  config.storage = :upyun
  config.upyun_username = "xxxxxx"
  config.upyun_password = 'xxxxxx'
  config.upyun_bucket = "my_bucket"
  config.upyun_bucket_host = "http://my_bucket.files.example.com"
end

And then in your uploader, set the storage to :upyun:

class AvatarUploader < CarrierWave::Uploader::Base
  storage :upyun
end

You can override configuration item in individual uploader like this:

class AvatarUploader < CarrierWave::Uploader::Base
  storage :upyun

  self.upyun_bucket = "avatars"
  self.upyun_bucket_host = "https://avatars.files.example.com"
end

Configuration for use UpYun "Image Space"

# The defined image name versions to limit use
IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES = %(320 640 800)
class ImageUploader < CarrierWave::Uploader::Base
  def store_dir
    "#{model.class.to_s.underscore}/#{mounted_as}"
  end

  def default_url
    # You can use FTP to upload a default image
    "#{Setting.upload_url}/blank.png#{version_name}"
  end

  # Override url method to implement with "Image Space"
  def url(version_name = "")
    @url ||= super({})
    version_name = version_name.to_s
    return @url if version_name.blank?
    if not version_name.in?(IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES)
      # To protected version name using, when it not defined, this will be give an error message in development environment
      raise "ImageUploader version_name:#{version_name} not allow."
    end
    [@url,version_name].join("!") # thumb split with "!"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    if super.present?
      model.uploader_secure_token ||= SecureRandom.uuid.gsub("-","")
      Rails.logger.debug("(BaseUploader.filename) #{model.uploader_secure_token}")
      "#{model.uploader_secure_token}.#{file.extension.downcase}"
    end
  end
end

To see more details about use UpYun: http://huacnlee.com/blog/rails-app-image-store-with-carrierwave-upyun/

carrierwave-upyun's People

Contributors

dependabot[bot] avatar huacnlee avatar huobazi avatar kaichen avatar lupeipei avatar nowa avatar vvdpzz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

carrierwave-upyun's Issues

如何实现多个upyun的帐号配置?

一个帐号时直接在config/initializers/carrierwave.rb 里配置即可

CarrierWave.configure do |config|
  config.storage = :upyun
  config.upyun_username = ENV['UPYUN_USERNAME']
  config.upyun_password = ENV['UPYUN_PASSWORD']
  config.upyun_bucket = ENV['UPYUN_BUCKET']
  config.upyun_bucket_domain = ENV['UPYUN_BUCKET_DOMAIN']
end

可是在实际中需要多个帐号,比如:头像上传、图片上传、文件上传等,
因为后期所做处理不同,对应帐号及bucket都不同。
请问该如何配置?

未处理的 HTTP 异常

RestClient::Forbidden (403 Forbidden):
app/controllers/photos_controller.rb:62:in block in create' app/controllers/photos_controller.rb:60:ineach'
app/controllers/photos_controller.rb:60:in `create'

又拍由于不能往 图片空间里面上传 Gif ,所以抛出了 403
但是这类异常得处理,不能直接抛出

没有改动任何代码,之前是可以上传的,后来出现403异常

A RestClient::Forbidden occurred in audios#create:

403 Forbidden
rest-client (1.6.7) lib/restclient/abstract_response.rb:48:in `return!'

...
...


rest-client (1.6.7) lib/restclient/abstract_response.rb:48:in `return!'
rest-client (1.6.7) lib/restclient/request.rb:230:in `process_result'
rest-client (1.6.7) lib/restclient/request.rb:178:in `block in transmit'
/usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/http.rb:851:in `start'
rest-client (1.6.7) lib/restclient/request.rb:172:in `transmit'
rest-client (1.6.7) lib/restclient/request.rb:64:in `execute'
rest-client (1.6.7) lib/restclient/request.rb:33:in `execute'
rest-client (1.6.7) lib/restclient/resource.rb:76:in `put'
carrierwave-upyun (0.1.6) lib/carrierwave/storage/upyun.rb:39:in `put'
carrierwave-upyun (0.1.6) lib/carrierwave/storage/upyun.rb:136:in `store'
carrierwave-upyun (0.1.6) lib/carrierwave/storage/upyun.rb:183:in `store!'
carrierwave (0.8.0) lib/carrierwave/uploader/store.rb:59:in `block in store!'
carrierwave (0.8.0) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.8.0) lib/carrierwave/uploader/store.rb:58:in `store!'
carrierwave (0.8.0) lib/carrierwave/mount.rb:371:in `store!'
carrierwave (0.8.0) lib/carrierwave/mount.rb:223:in `store_data!'

无法删除对应的目录

如果我使用id作为文件夹名字,比如
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

当删除图片的时候,无法删除包含图片的文件夹。

请问代码作用域问题

https://github.com/nowa/carrierwave-upyun/blob/master/lib/carrierwave/upyun/configuration.rb#L12
这里的end结束了 CarrierWave :: UpYun:: Configuration 这个 module。
下面ClassMethods里定义的add_config是没用到。
实际上在https://github.com/carrierwaveuploader/carrierwave/blob/master/lib/carrierwave/uploader/configuration.rb#L106 内已有同功能的code。

同样的问题在下面两个gem里也有。
https://github.com/huacnlee/carrierwave-tfs
https://github.com/huacnlee/carrierwave-aliyun

@nowa @huacnlee

上传过去太慢了

加上本地图片裁剪,基本上都要 3s 一个...

3张图片

大的 118K
中图 118K
小图 26K

一共耗费时间: 3139ms

盛大云的服务器过去

Need to implement #cache! if you want to use CarrierWave::Storage::UpYun as a cache storage

when I upgrade carrierwave to 2.0.0, using carrierwave-upyun 0.2.2

call this exception:

image

      def cache!(new_file)
        raise NotImplementedError.new("Need to implement #cache! if you want to use #{self.class.name} as a cache storage.")
      end

      def retrieve_from_cache!(identifier)
        raise NotImplementedError.new("Need to implement #retrieve_from_cache! if you want to use #{self.class.name} as a cache storage.")
      end

      def delete_dir!(path)
        raise NotImplementedError.new("Need to implement #delete_dir! if you want to use #{self.class.name} as a cache storage.")
      end

      def clean_cache!(seconds)
        raise NotImplementedError.new("Need to implement #clean_cache! if you want to use #{self.class.name} as a cache storage.")
      end

At new carrierwave version, these interfaces need be implmented.

password will leak with this sdk

from your sdk, you use a non-https host of upyun and you sent username and password with simple http auth, so the password will leak if the package is captured.

RestClient::Unauthorized Issue

不管用什么AjaxUpload控件,上传文件都会遇到这个问题:

RestClient::Unauthorized at photos/

401 Unauthorized

我想这个是csrf token问题吧,请教如何解决?

NoMethodError: undefined method `uploader_secure_token'

uploader 就是按 readme 里写的。保存图片的时候会有这个错误,是否跟我是用 carrierwave 远程抓取图片的有关?

# encoding: utf-8
# app/uploaders/photo_uploader.rb

IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES = %(:thumb :tiny)
class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :upyun

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

  # Override url method to implement with "Image Space"
  def url(version_name = "")
    @url ||= super({})
    version_name = version_name.to_s
    return @url if version_name.blank?
    if not version_name.in?(IMAGE_UPLOADER_ALLOW_IMAGE_VERSION_NAMES)
      # To protected version name using, when it not defined, this will be give an error message in development environment
      raise "ImageUploader version_name:#{version_name} not allow."
    end
    [@url,version_name].join("!") # thumb split with "!"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  def filename
    if super.present?
      model.uploader_secure_token ||= SecureRandom.uuid.gsub("-","")
      Rails.logger.debug("(BaseUploader.filename) #{model.uploader_secure_token}")
      "#{model.uploader_secure_token}.#{file.extension.downcase}"
    end
  end
end
# app/models/photo.rb
class Photo < ActiveRecord::Base
  mount_uploader :image, PhotoUploader

  attr_accessible :image, :remote_image_url

  belongs_to :imageable, :polymorphic => true
end

upyun_bucket_domain 需要支持多个域名

以便优化的时候可以设置多个上传文件的域名提高客户端并发下载

config.upyun_bucket_domain = %(http://cdn1.demo.com http://cdn2.demo.com http://cdn3.demo.com)

Sometimes the old uploaded files will lost

场景:

  • 上传一张图, foo.jpg
  • 一段时间以后,foo.jpg 异常丢失,但我确信是没有删除的动作

最早搞 IMAX.im 的时候就会遇到,到现在我依然没查到原因在哪里,现在我的另外一个项目里面再次遇到。

目前我的猜测

class User
  mount_uploader :avatar, AvatarUploader
end
  1. 我在怀疑是否有可能用户在修改数据(非修改 :avatar 字段),遇到 Bug,无意中触发了 Carrierwave 删除旧图片的功能?
  2. 用户实际上是在修改 :avatar,Carrierwave 上传成功,并删除了旧图片。接着程序挂了,或执行步骤异常中断了,新的图片地址没有存入数据库。

第二个情况似乎比较容易发生。

上传文件出现 Update failed: concurrent put or delete

正常挂载到一个 Model

Update failed: {"msg":"concurrent put or delete","code":42900007,"id":"bc4cfec734c3bc4dfaaa8cbeba66ea38"}

在一个 rake 任务中执行的, 所以肯定是单线程运行的脚本.

初步排查过,可能跟最近换的 Faraday 有关系

store_dir 的父节点问题

  • 只用 carrierwave 时,store_dir 默认出现在 Rails.public_path 下。
  • 添加 carrierwave-upyun 后,store_dir 出现在 Rails.root 下。

看了一下 carrierwave-upyun 没有修改 store_dir 的逻辑,不知道是哪里影响了。

上传文件名含中文的文件,报错URI::InvalidURIError

你好,如题,上传文件,文件名含中文,报错 URI::InvalidURIError。
version:
rails 6.0.2.1
ruby 2.6.0
carrierwave 2.0.2
carrierwave-upyun 1.0.2
看报错 的full trace, 显示 carrierwave/storage/upyun/file.rb:100:in headers' lib/carrierwave/storage/upyun/file.rb的 headers:

      def headers
        @headers ||= begin
          conn.get(@path).headers
        rescue Faraday::ClientError
          {}
        end
      end

debug后,发现 @path 是原文件名,含中文,未encode,将 conn.get(@path) 改为 conn.get(escaped_path) 后,没有问题了,应该是个bug。

更新到最新版本,url会在地址里自动加上了本站域名!

更新到最新版本,url会在地址里自动加上了本站域名!

http://localhost:3000/images/meiyuewuyou.b0.upaiyun.com/picture/image/04b4b15a2586497a83a723964b0c4bab.png!50x50

正确应该是

http://meiyuewuyou.b0.upaiyun.com/picture/image/04b4b15a2586497a83a723964b0c4bab.png!50x50

retrieve_from_store! not working

Given the file name I want to retrieve the stored data. While when I execute the following code:
image_uploader = ImgUploader.new
image_uploader.retrieve_from_store!("ee93f14e-33a8-4b6b-a1ba-d9df2b5702d0.png")

I got following out put:
=> [:retrieve_versions_from_store!]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.