ruby-shrine-aliyun-oss 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e4f268633e8856b7c769bb41112dc8c8d21eaa7
4
+ data.tar.gz: 1330138f06783aa25137e015acaf47d454741e8d
5
+ SHA512:
6
+ metadata.gz: 2d3eee062aad46c4f8327c0795aad5cc8f30a6a519c5ff03bf664bb5357ca2e5e898be078775d9035f52c0f3f6bb40d2ec6f934354895cccb805c4339b807ac2
7
+ data.tar.gz: 1a813167d521793a841fb7b88299f2ba2840978114a76b5fa824fc01add42370461bf7d59667d2bb99ade3d71b84445dd478906425d9a2ff83a0c3d9a83256d1
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.
@@ -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,80 @@
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
+ content_type = shrine_metadata["mime_type"]
17
+
18
+ if copyable?(io)
19
+ bucket.copy_object(io.storage.object_key(io.id), object_key(id))
20
+ else
21
+ # bucket.put_object(object_key(id), content_type: content_type) { |stream| stream << io.read }
22
+ bucket.put_object(object_key(id)) { |stream| stream << io.read }
23
+ end
24
+ end
25
+
26
+ def url(id, sign: true, expiry: 900)
27
+ bucket.object_url(object_key(id), sign, expiry)
28
+ end
29
+
30
+ # Downloads the file from Aliyun OSS, and returns a `Tempfile`.
31
+ def download(id)
32
+ tempfile = Tempfile.new(["shrine-aliyun-oss", File.extname(id)], binmode: true)
33
+ object = bucket.get_object(object_key(id), file: tempfile)
34
+
35
+ tempfile.singleton_class.instance_eval { attr_accessor :content_type }
36
+ tempfile.content_type = object.headers[:content_type]
37
+ tempfile.tap(&:open)
38
+ rescue
39
+ tempfile.close! if tempfile
40
+ raise
41
+ end
42
+
43
+ def open(id)
44
+ chunks = nil
45
+ object = bucket.get_object(object_key(id)) do |chunk|
46
+ chunks = Enumerator.new do |y|
47
+ y << chunk
48
+ end
49
+ end
50
+
51
+ Down::ChunkedIO.new(
52
+ chunks: chunks,
53
+ size: object.size,
54
+ data: { object: object }
55
+ )
56
+ end
57
+
58
+ def exists?(id)
59
+ bucket.object_exists?(object_key(id))
60
+ end
61
+
62
+ def delete(id)
63
+ bucket.delete_object(object_key(id))
64
+ end
65
+
66
+ def object_key(id)
67
+ prefix ? "#{prefix}/#{id}" : id
68
+ end
69
+
70
+ private
71
+
72
+ attr_reader :prefix, :bucket
73
+
74
+ def copyable?(io)
75
+ io.is_a?(UploadedFile) &&
76
+ io.storage.is_a?(Storage::AliyunOSS)
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-shrine-aliyun-oss
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - 'Ken based on Derrick '
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-13 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.7.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.7.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
+ - zhdhui@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - lib/shrine/storage/aliyun_oss.rb
93
+ homepage: https://github.com/catsky/shrine-aliyun-oss
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.10
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Provides Aliyun OSS storage for Shrine.
117
+ test_files: []