Giter Club home page Giter Club logo

paperclip-storage-aliyun's Introduction

Coverage Status Build Status Maintainability

Aliyun Open Storage Service for Paperclip

This gem implement the support for Aliyun open storage service(OSS) to Paperclip.

Installation

gem install paperclip-storage-aliyun

Or, if you are using a bundler, you can append the following line into your Gemfile:

gem 'paperclip-storage-aliyun'

Configuration

In order to make all the things work, you should do some important configurations through a initializer:

If you are developing a Rails application, you can append a new initializer like:

# [rails_root]/config/initializers/paperclip-aliyun-configuration.rb
Paperclip::Attachment.default_options[:aliyun] = {
  access_id: '3VL9XMho8iCushj8',
  access_key: 'VAUI2q7Tc6yTh1jr3kBsEUzZ84gEa2',
  bucket: 'xx-test',
  data_center: 'cn-hangzhou',
  internal: false,
  protocol: 'https',
  protocol_relative_url: false
}

Then, in the model which defines the attachment, specify your storage and other options, for example:

# [rails_root]/app/models/image.rb
class Image < ActiveRecord::Base
  has_attached_file :attachment, {
    storage: :aliyun,
    styles: { thumbnail: "60x60#"},
    path: 'public/system/:class/:attachment/:id_partition/:style/:filename',
    url: ':aliyun_upload_url'
  }
end

Similar to Paperclip::Storage::S3, there are four options for the url by now:

  • :aliyun_upload_url : the url based on the options you give
  • :aliyun_internal_url : the internal url, no matter what options[:aliyun][:internal] is
  • :aliyun_external_url : the external url, no matter what options[:aliyun][:internal] is
  • :aliyun_alias_url : the alias url based on the host_alias you give, typically used together with CDN

Please note the values above are all strings, not symbols. You could still make your own url if only you know what you are doing.

Data Centers

A list of available regions can be found at https://intl.aliyun.com/help/doc-detail/31837.htm. You can use the "Region Expression" column value as it is for the data center, or you can remove the "oss-" prefix. For example: oss-cn-hangzhou and cn-hangzhou are both valid options.

Test

  1. Update connection settings in spec/spec_helper.rb:
# Aliyun defaults
OSS_CONNECTION_OPTIONS = {
  access_id: 'your_access_key_id',
  access_key: 'your_access_key_secret',
  bucket: 'your_bucket',
  data_center: 'your_data_center',
  internal: false,
  protocol: 'https'
}
  1. Run bundle exec rspec spec.

paperclip-storage-aliyun's People

Contributors

aidistan avatar ayaman avatar martin91 avatar

Stargazers

 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

paperclip-storage-aliyun's Issues

data_center参数值被改变引起的bug

data_center.rb#33
代码: data_center.prepend('oss-')
会直接修改传递过来的值如"cn-beijing"为 "oss-cn-beijing",
但传递过来的参数,通常是在全局设置的CONFIG,如下:

...
data_center: CONFIG['aliyun_data_center'],
...

所以外部代码在不知情的情况下, CONFIG['aliyun_data_center'] 值就被修改了,导致其它地方出错。
建议这里不要直接修改传递过来的参数值本身。使用 data_center = 'oss-' + data_center 这样的方式。

Relative path returned by model.attachment.url method

还遇到一个问题:
user has attachment named photo, user.photo.url 返回了一个relative的path, 如

/advisor_2/created_at_1369288191/original.jpg?1413099173
而不是绝对的url。 我想应该expect返回的是绝对的url吧

More regions

Can you add all the available data centers to the gem?

Require a graceful way to define default url style of attachments

README.md 示例使用了 has_attached_file 的参数来定义的 url 风格,此时在模型中 include Paperclip::Storage::Aliyun 尚可接受。但若希望设定全局默认 url 风格时,将众多方法 extend 到 main 对象中未免有些不妥:

# config/initializers/paperclip.rb

include Paperclip::Storage::Aliyun

Paperclip::Attachment.default_options[:aliyun] = {
  access_id: ENV['ALIYUN_ACCESS_ID'],
  access_key: ENV['ALIYUN_ACCESS_KEY'],
  #...
}

Paperclip::Attachment.default_options[:path] = (Rails.env.production? ? '' : ':rails_env') + '/:class/:attachment/:id.:extension'
Paperclip::Attachment.default_options[:url] = 'http://' + oss_connection.fetch_file_host + '/' + Paperclip::Attachment.default_options[:path]

事实上 Paperclip::Attachment.default_options[:aliyun] 已经唯一地决定了 Aliyun::Connect#fetch_file_host 的结果,而与具体实例无关,此处可以有一个类方法帮助我们更优雅地配置全局默认 url 风格。

Allow local credentials overiwriting global default credentials

目前 paperclip-storage-aliyun 每次建立 OSS 连接都使用了默认配置:

oss_connection ||= ::Aliyun::Connection.new Paperclip::Attachment.default_options[:aliyun]

我们可以更灵活地支持在模型定义 has_attached_file 时覆盖默认配置,例如:

#  paperclip-storage-aliyun/lib/paperclip/storage/aliyun.rb
oss_connection ||= ::Aliyun::Connection.new Paperclip::Attachment.default_options[:aliyun].merge(local_options)

这样我们就可以有

has_attached_file :download, storage: :aliyun, aliyun: { bucket: 'another-place'}
has_attached_file :secret, storage: :aliyun, aliyun: { access_id: 'another-id', access_key: 'another-key', bucket: 'secret-place'}

Unable to fetch image

HI

I'm able to upload an image. Othewise, The URL returned by Image.source.url is https://mybucket.oss-ap-southeast-1.aliyuncs.com?1474196962 and not the url to the uploaded picture.

Gemfile:

gem 'paperclip-storage-aliyun', github: 'Martin91/paperclip-storage-aliyun'

paperclip.rb

Paperclip::Attachment.default_options[:aliyun] = {
  access_id: ENV['ALIYUN_ACCESS_ID'],
  access_key: ENV['ALIYUN_ACCESS_KEY'],
  bucket: 'mybucket',
  data_center: 'oss-ap-southeast-1',
  internal: false
}

image.rb

  attr_accessor :source_file_name, :delete_source

  has_attached_file :source, {
    storage: :aliyun,
    path: ':class/:attachment/:id_partition/:filename',
    url: 'https://mybucket.oss-ap-southeast-1.aliyuncs.com'
  }
  before_validation { self.source.clear if self.delete_source == '1' }

  validates_attachment_content_type :source, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

Aliyun protocol

Do you think about to add an empty string option for Aliyun protocol?
Setting an empty string to generate protocol-relative URLs.

Provide internal_url and external_url at the same time

默认的 url 应当跟随配置中的 :internal 选项,但是实际中我们可能同时需要内部地址和外部地址,此时可以考虑给 Paperclip::Attachment 添加两个新方法:internal_url & external_url

上传时,按服务器位置配置 :internal 选项,按 object key 命名规则配置 :path 即可自动生成 url, internal_urlexternal_url。使用时,可以有:

# in photos_controller.rb
@faces = detect_faces(@photo.internal_url) # 使用 internal_url 以节约流量和加快速度
-# in views/photos/show.html.haml
= image_tag(@photo.external_url)
%caption There are #{@faces.count} faces in the above photo.

仅作建议:)

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.