shrine-aliyun-oss 0.1.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e5e90c1538c2a896e22c315a3f1a246f2e4722d79f693c3a852e762cb0d87e0c
4
+ data.tar.gz: c9258623a03ddbfa7f53d3f45610b260ec920f41db8f2064ecddad22ab718420
5
+ SHA512:
6
+ metadata.gz: 25ffe782c596ce4a48af0a7e53fb03531ccbfd3da0c557f1cd03878a10b4060491578a7c43ce6a83b30d3fabbcbf62ea2e541f74a1af7c90c832bd733208f5a0
7
+ data.tar.gz: acca262150775380b04d8f2e6ae145e4a5c90288533a6a887f09ea70210d574064f9328b4b51c3483f078f8b7f897dddf67f7485a86dc56497e4c370555a9ab2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Derrick Zhang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Shrine::Storage::AliyunOSS
2
+
3
+ Provides [Alibaba cloud object storage] for [Shrine].
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'shrine-aliyun-oss'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ require "shrine"
17
+ require "shrine/storage/aliyun_oss"
18
+
19
+ oss_options = {
20
+ endpoint: "oss-<region-name>.aliyuncs.com",
21
+ access_key_id: "<access_key_id>",
22
+ access_key_secret: "<access_key_secret>",
23
+ bucket: "<bucket-name>"
24
+ }
25
+
26
+ Shrine.storages = {
27
+ cache: Shrine::Storage::AliyunOSS.new(prefix: "cache", **oss_options),
28
+ store: Shrine::Storage::AliyunOSS.new(prefix: "store", **oss_options)
29
+ }
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zillou/shrine-aliyun-oss.
35
+
36
+ ## License
37
+
38
+ - [MIT](http://opensource.org/licenses/MIT)
39
+
40
+ [Alibaba cloud object storage]: https://www.alibabacloud.com/product/oss
41
+ [Shrine]: https://github.com/janko-m/shrine
42
+
@@ -0,0 +1,77 @@
1
+ require "shrine"
2
+ require "aliyun/oss"
3
+ require "down/chunked_io"
4
+
5
+ class Shrine
6
+ module Storage
7
+ class AliyunOSS
8
+ def initialize(prefix:, **oss_options)
9
+ bucket_name = oss_options.delete(:bucket)
10
+ @prefix = prefix
11
+ @client = Aliyun::OSS::Client.new(**oss_options)
12
+ @bucket = @client.get_bucket(bucket_name)
13
+ end
14
+
15
+ def upload(io, id, shrine_metadata: {}, **upload_options)
16
+ if copyable?(io)
17
+ bucket.copy_object(io.storage.object_key(io.id), object_key(id))
18
+ else
19
+ bucket.put_object(object_key(id)) { |stream| stream << io.read }
20
+ end
21
+ end
22
+
23
+ def url(id, sign: true, expiry: 900)
24
+ bucket.object_url(object_key(id), sign, expiry)
25
+ end
26
+
27
+ # Downloads the file from Aliyun OSS, and returns a `Tempfile`.
28
+ def download(id)
29
+ tempfile = Tempfile.new(["shrine-aliyun-oss", File.extname(id)], binmode: true)
30
+ object = bucket.get_object(id, file: tempfile)
31
+
32
+ tempfile.singleton_class.instance_eval { attr_accessor :content_type }
33
+ tempfile.content_type = object.headers[:content_type]
34
+ tempfile.tap(&:open)
35
+ rescue
36
+ tempfile.close! if tempfile
37
+ raise
38
+ end
39
+
40
+ def open(id)
41
+ chunks = nil
42
+ object = bucket.get_object(object_key(id)) do |chunk|
43
+ chunks = Enumerator.new do |y|
44
+ y << chunk
45
+ end
46
+ end
47
+
48
+ Down::ChunkedIO.new(
49
+ chunks: chunks,
50
+ size: object.size,
51
+ data: { object: object }
52
+ )
53
+ end
54
+
55
+ def exists?(id)
56
+ bucket.object_exists?(object_key(id))
57
+ end
58
+
59
+ def delete(id)
60
+ bucket.delete_object(object_key(id))
61
+ end
62
+
63
+ def object_key(id)
64
+ prefix ? "#{prefix}/#{id}" : id
65
+ end
66
+
67
+ private
68
+
69
+ attr_reader :prefix, :bucket
70
+
71
+ def copyable?(io)
72
+ io.is_a?(UploadedFile) &&
73
+ io.storage.is_a?(Storage::AliyunOSS)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "shrine-aliyun-oss"
3
+ spec.version = "0.1.0.beta1"
4
+ spec.authors = ["Derrick Zhang"]
5
+ spec.email = ["robot@zillou.me"]
6
+
7
+ spec.summary = "Provides Aliyun OSS storage for Shrine."
8
+ spec.homepage = "https://github.com/zillou/shrine-aliyun-oss"
9
+ spec.files = Dir["README.md", "LICENSE", "lib/**/*.rb", "shrine-aliyun-oss.gemspec"]
10
+
11
+ spec.require_paths = ["lib"]
12
+ spec.license = "MIT"
13
+
14
+ spec.add_dependency "shrine", "~> 2.2"
15
+ spec.add_dependency "aliyun-sdk", "~> 0.5.0"
16
+
17
+ spec.add_development_dependency "bundler", "~> 1.16"
18
+ spec.add_development_dependency "rake", "~> 10.0"
19
+ spec.add_development_dependency "minitest", "~> 5.0"
20
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-aliyun-oss
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - Derrick Zhang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shrine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aliyun-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description:
84
+ email:
85
+ - robot@zillou.me
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - lib/shrine/storage/aliyun_oss.rb
93
+ - shrine-aliyun-oss.gemspec
94
+ homepage: https://github.com/zillou/shrine-aliyun-oss
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">"
110
+ - !ruby/object:Gem::Version
111
+ version: 1.3.1
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.7.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Provides Aliyun OSS storage for Shrine.
118
+ test_files: []