paperclip-oracle-object-storage 0.2.0
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: 74757b0832bbddbcfac8c67a3df9960e8c88987b61663b690a02e7a2888016d1
|
|
4
|
+
data.tar.gz: e9ad53eae7206b1861a6629d812f710c03eae02959937e9210c18ab6583dca72
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e3bb72a8fa83b7b9f1030166b62f115ef9347235d26b3baf920a1c878549ff9e1876997003778e9811e71788a3e7219b2d59d709f8f4c58b1bce4bbd4ed436ba
|
|
7
|
+
data.tar.gz: 278182025b77b4f420a801a39fe244c26273c9e68fdc382d0ec89fc47a7ccfd80d0ee5320797468c36ed37e2dc1dfc7aef1e7a2d96cdea6a3e9c6764583c39f2
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Paperclip
|
|
4
|
+
module Storage
|
|
5
|
+
module OracleObjectStorage
|
|
6
|
+
def self.extended(base)
|
|
7
|
+
begin
|
|
8
|
+
require "oci"
|
|
9
|
+
rescue LoadError => e
|
|
10
|
+
raise("#{e.message} (You may need to install the oci gem)")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
base.instance_eval do
|
|
14
|
+
#TODO: Load config by file.
|
|
15
|
+
# if oci_credentials[:config_file]
|
|
16
|
+
# @config = OCI::ConfigFileLoader.load_config(
|
|
17
|
+
# oci_credentials[:config_file],
|
|
18
|
+
# oci_credentials[:profile] || "DEFAULT"
|
|
19
|
+
# )
|
|
20
|
+
# end
|
|
21
|
+
@config = OCI::Config.new
|
|
22
|
+
|
|
23
|
+
@config.user = oci_credentials[:user]
|
|
24
|
+
@config.fingerprint = oci_credentials[:fingerprint]
|
|
25
|
+
@config.tenancy = oci_credentials[:tenancy]
|
|
26
|
+
@config.region = oci_credentials[:region]
|
|
27
|
+
@config.key_file = oci_credentials[:key_file]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@bucket_name = oci_credentials[:bucket_name]
|
|
31
|
+
@options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "")
|
|
32
|
+
@options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def oci_credentials
|
|
37
|
+
@oci_credentials ||= parse_credentials(@options[:oci_credentials])
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def bucket_name
|
|
41
|
+
@bucket_name
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def namespace
|
|
45
|
+
@namespace ||= begin
|
|
46
|
+
configured = @options[:namespace] || oci_credentials[:namespace]
|
|
47
|
+
|
|
48
|
+
return configured if configured.present?
|
|
49
|
+
|
|
50
|
+
object_storage_client.get_namespace.data
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def object_storage_client
|
|
55
|
+
# Doc of OCI client: https://docs.oracle.com/en-us/iaas/tools/ruby/2.22.0/OCI/ObjectStorage/ObjectStorageClient.html#put_object_lifecycle_policy-instance_method
|
|
56
|
+
@object_storage_client ||= OCI::ObjectStorage::ObjectStorageClient.new(config: @config)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def object_name(style = default_style)
|
|
60
|
+
path(style).sub(%r{\A/}, "").unicode_normalize(:nfc).then { |p| I18n.transliterate(p) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def exists?(style = default_style)
|
|
64
|
+
return false unless original_filename
|
|
65
|
+
|
|
66
|
+
object_storage_client.head_object(
|
|
67
|
+
namespace,
|
|
68
|
+
bucket_name,
|
|
69
|
+
object_name(style)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
true
|
|
73
|
+
rescue OCI::Errors::ServiceError => e
|
|
74
|
+
return false if e.status_code == 404
|
|
75
|
+
|
|
76
|
+
raise
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def expiring_url(time = 3600, style = default_style)
|
|
80
|
+
expires_at = Time.now.utc + time
|
|
81
|
+
|
|
82
|
+
details =
|
|
83
|
+
OCI::ObjectStorage::Models::CreatePreauthenticatedRequestDetails.new(
|
|
84
|
+
name: "paperclip-#{SecureRandom.hex(8)}",
|
|
85
|
+
object_name: object_name(style),
|
|
86
|
+
access_type: "ObjectRead",
|
|
87
|
+
time_expires: expires_at
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
response =
|
|
91
|
+
object_storage_client.create_preauthenticated_request(
|
|
92
|
+
namespace,
|
|
93
|
+
bucket_name,
|
|
94
|
+
details
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
endpoint = object_storage_client.endpoint
|
|
98
|
+
|
|
99
|
+
"#{endpoint.chomp("/")}#{response.data.access_uri}"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def flush_writes #:nodoc:
|
|
103
|
+
@queued_for_write.each do |style, file|
|
|
104
|
+
begin
|
|
105
|
+
log("saving to Oracle object storage #{path(style)}")
|
|
106
|
+
object_storage_client.put_object(
|
|
107
|
+
object_storage_client.get_namespace.data,
|
|
108
|
+
bucket_name,
|
|
109
|
+
object_name(style),
|
|
110
|
+
File.open(file.path),
|
|
111
|
+
{
|
|
112
|
+
content_type: file.content_type
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
rescue => e
|
|
116
|
+
log("error saving to Oracle object storage #{path(style)}")
|
|
117
|
+
raise e
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
@queued_for_write = {}
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def flush_deletes #:nodoc:
|
|
124
|
+
@queued_for_delete.each do |path|
|
|
125
|
+
begin
|
|
126
|
+
log("deleting #{path}")
|
|
127
|
+
object_storage_client.delete_object(
|
|
128
|
+
object_storage_client.get_namespace.data,
|
|
129
|
+
bucket_name,
|
|
130
|
+
object_name
|
|
131
|
+
)
|
|
132
|
+
rescue
|
|
133
|
+
log("error deleting #{path}")
|
|
134
|
+
raise e
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
@queued_for_delete = []
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def copy_to_local_file(style, local_dest_path)
|
|
141
|
+
log("copying #{path(style)} to #{local_dest_path}")
|
|
142
|
+
|
|
143
|
+
response =
|
|
144
|
+
object_storage_client.get_object(
|
|
145
|
+
namespace,
|
|
146
|
+
bucket_name,
|
|
147
|
+
object_name(style)
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
File.binwrite(local_dest_path, response.data)
|
|
151
|
+
rescue OCI::Errors::ServiceError => e
|
|
152
|
+
warn("#{e} - cannot copy #{path(style)}")
|
|
153
|
+
false
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def create_bucket
|
|
157
|
+
details =
|
|
158
|
+
OCI::ObjectStorage::Models::CreateBucketDetails.new(
|
|
159
|
+
name: bucket_name,
|
|
160
|
+
compartment_id: compartment_id
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
object_storage_client.create_bucket(
|
|
164
|
+
namespace,
|
|
165
|
+
details
|
|
166
|
+
)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
private
|
|
170
|
+
|
|
171
|
+
def compartment_id
|
|
172
|
+
@options[:compartment_id] ||
|
|
173
|
+
oci_credentials[:compartment_id]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def parse_credentials(creds)
|
|
177
|
+
creds = creds.call(self) if creds.respond_to?(:call)
|
|
178
|
+
|
|
179
|
+
case creds
|
|
180
|
+
when File
|
|
181
|
+
load_credentials_from_file(creds.path)
|
|
182
|
+
when String, Pathname
|
|
183
|
+
load_credentials_from_file(creds)
|
|
184
|
+
when Hash
|
|
185
|
+
creds.symbolize_keys
|
|
186
|
+
when NilClass
|
|
187
|
+
{}
|
|
188
|
+
else
|
|
189
|
+
raise ArgumentError,
|
|
190
|
+
"Credentials given are not a path, file, proc, or hash."
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def load_credentials_from_file(path)
|
|
195
|
+
YAML.safe_load(
|
|
196
|
+
ERB.new(File.read(path)).result,
|
|
197
|
+
aliases: true
|
|
198
|
+
).symbolize_keys
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: paperclip-oracle-object-storage
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Luiz Filipe
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: oci
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 2.22.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - '='
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 2.22.0
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: kt-paperclip
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 6.4.1
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 6.4.1
|
|
40
|
+
description: Paperclip storage lib to save to Oracle Object Storage
|
|
41
|
+
email:
|
|
42
|
+
- luiz.neves@prosas.com.br
|
|
43
|
+
- luizfilipeneves@gmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- lib/paperclip-oracle-object-storage.rb
|
|
49
|
+
- lib/paperclip/storage/oracle_object_storage.rb
|
|
50
|
+
- lib/paperclip/storage/oracle_object_storage/version.rb
|
|
51
|
+
homepage: https://github.com/prosas/paperclip-oracle-object-storage
|
|
52
|
+
licenses:
|
|
53
|
+
- MIT
|
|
54
|
+
metadata:
|
|
55
|
+
homepage_uri: https://github.com/prosas/paperclip-oracle-object-storage
|
|
56
|
+
source_code_uri: https://github.com/prosas/paperclip-oracle-object-storage
|
|
57
|
+
changelog_uri: https://github.com/prosas/paperclip-oracle-object-storage/CHANGELOG.md
|
|
58
|
+
rdoc_options: []
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: 2.7.4
|
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
requirements: []
|
|
72
|
+
rubygems_version: 4.0.1
|
|
73
|
+
specification_version: 4
|
|
74
|
+
summary: Paperclip storage lib to save to Oracle Object Storage
|
|
75
|
+
test_files: []
|