onlyoffice_s3_wrapper 0.1.0 → 0.4.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 +5 -5
- data/lib/onlyoffice_s3_wrapper.rb +89 -33
- data/lib/onlyoffice_s3_wrapper/name.rb +6 -0
- data/lib/onlyoffice_s3_wrapper/path_helper.rb +17 -0
- data/lib/onlyoffice_s3_wrapper/version.rb +4 -1
- metadata +150 -13
- data/README.md +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f371300c8b28c568d47bf7dc85ce6815026833b540bfc8e9b9ac602856f15c82
|
4
|
+
data.tar.gz: 5d08e52e477eb493d027914258196b55b3af4a0230b3215e0b62eb0871126cc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca9c3f1b34c432beb95f37e4d1bab93d5a6bc74c132a0f76c680f90a06f59bd87f75f151cdecca8062e5c044a51b9fb5dd99e78966fc24e839a9b1a5ae07e4f0
|
7
|
+
data.tar.gz: 163fe1a244b2bc4a41fcd1ba8469501e36a3b24a48f16511818eda6a7af267e68a30a8be0257eeafdd58fa80cf16d2ef9b7aecc9886ca94de68bfe2a50f8221f
|
@@ -1,25 +1,25 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws-sdk-s3'
|
2
4
|
require 'open-uri'
|
3
5
|
require 'securerandom'
|
4
6
|
require 'onlyoffice_file_helper'
|
7
|
+
require 'onlyoffice_s3_wrapper/path_helper'
|
5
8
|
require 'onlyoffice_s3_wrapper/version'
|
6
9
|
|
10
|
+
# Namespace for Gem
|
7
11
|
module OnlyofficeS3Wrapper
|
8
12
|
# Class for working with amazon s3
|
9
13
|
class AmazonS3Wrapper
|
10
|
-
|
14
|
+
include PathHelper
|
15
|
+
attr_accessor :s3, :bucket, :download_folder
|
16
|
+
# [String] Amazon key
|
17
|
+
attr_writer :access_key_id
|
18
|
+
# [String] Amazon secret key
|
19
|
+
attr_writer :secret_access_key
|
11
20
|
|
12
21
|
def initialize(bucket_name: 'nct-data-share', region: 'us-west-2')
|
13
|
-
|
14
|
-
@secret_access_key = ENV['S3_PRIVATE_KEY']
|
15
|
-
if @access_key_id.nil? || @secret_access_key.nil?
|
16
|
-
begin
|
17
|
-
@access_key_id = File.read(Dir.home + '/.s3/key').delete("\n")
|
18
|
-
@secret_access_key = File.read(Dir.home + '/.s3/private_key').delete("\n")
|
19
|
-
rescue Errno::ENOENT
|
20
|
-
raise Errno::ENOENT, "No key or private key found in #{Dir.home}/.s3/ directory. Please create files #{Dir.home}/.s3/key and #{Dir.home}/.s3/private_key"
|
21
|
-
end
|
22
|
-
end
|
22
|
+
read_keys
|
23
23
|
Aws.config = { access_key_id: @access_key_id,
|
24
24
|
secret_access_key: @secret_access_key,
|
25
25
|
region: region }
|
@@ -28,67 +28,123 @@ module OnlyofficeS3Wrapper
|
|
28
28
|
@download_folder = Dir.mktmpdir('amazon-s3-downloads')
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
# Get files by prefix
|
32
|
+
# @param prefix [String] prefix to filter
|
33
|
+
# @param field [Symbol] field to get
|
34
|
+
# @return [Array<Object>] result set
|
35
|
+
def get_files_by_prefix(prefix = nil, field: :key)
|
36
|
+
@bucket.objects(prefix: prefix)
|
37
|
+
.collect(&field)
|
38
|
+
.reject { |file| folder?(file) }
|
33
39
|
end
|
34
40
|
|
35
41
|
# param [String] prefix
|
36
|
-
# return [Array] of folder names with '/'
|
42
|
+
# return [Array] of folder names with '/'
|
43
|
+
# in end and filenames with fullpath (started ad prefix)
|
37
44
|
def get_elements_by_prefix(prefix = nil)
|
38
45
|
@bucket.objects(prefix: prefix).collect(&:key)
|
39
46
|
end
|
40
47
|
|
41
|
-
|
48
|
+
# Is string path to folder
|
49
|
+
# @param str [String] path
|
50
|
+
# @return [True, False]
|
51
|
+
def folder?(str)
|
42
52
|
str.end_with? '/'
|
43
53
|
end
|
44
54
|
|
55
|
+
# Get object by name
|
56
|
+
# @param obj_name [String] name of object
|
57
|
+
# @return [Object]
|
45
58
|
def get_object(obj_name)
|
46
59
|
@bucket.object(obj_name)
|
47
60
|
end
|
48
61
|
|
49
|
-
|
50
|
-
|
51
|
-
|
62
|
+
# @param file_name [String] path to file in S3 bucket
|
63
|
+
# @param download_location [String] path to save file.
|
64
|
+
# Can be full path to file, or just the directory to save
|
65
|
+
# @return [String] full path to file
|
66
|
+
def download_file_by_name(file_name, download_location = nil)
|
52
67
|
object = get_object(file_name)
|
53
|
-
download_object(object, download_folder)
|
68
|
+
temp_location = download_object(object, @download_folder)
|
69
|
+
OnlyofficeLoggerHelper.log("Temp downloaded file: #{temp_location}")
|
70
|
+
|
71
|
+
return temp_location unless download_location
|
72
|
+
|
73
|
+
download_location = "#{download_location}/#{File.basename(file_name)}" if File.directory?(download_location)
|
74
|
+
FileUtils.mv(temp_location, download_location)
|
75
|
+
download_location
|
54
76
|
end
|
55
77
|
|
78
|
+
# Download object
|
79
|
+
# @param object [Object] to download
|
80
|
+
# @param download_folder [String] path to save
|
81
|
+
# @return [String] path to downloaded file
|
56
82
|
def download_object(object, download_folder = @download_folder)
|
83
|
+
file_name = "#{download_folder}/#{File.basename(object.key)}"
|
57
84
|
link = object.presigned_url(:get, expires_in: 3600)
|
58
|
-
|
59
|
-
|
60
|
-
IO.copy_stream(open(link), f)
|
85
|
+
File.open(file_name, 'w') do |f|
|
86
|
+
IO.copy_stream(URI.parse(link).open, f)
|
61
87
|
end
|
62
|
-
|
63
|
-
rescue StandardError
|
64
|
-
raise("File
|
88
|
+
file_name
|
89
|
+
rescue StandardError => e
|
90
|
+
raise("File #{file_name} download failed with: #{e}")
|
65
91
|
end
|
66
92
|
|
93
|
+
# Upload file
|
94
|
+
# @param file_path [String] file to upload
|
95
|
+
# @param upload_folder [String] path to upload
|
96
|
+
# @return [nil]
|
67
97
|
def upload_file(file_path, upload_folder)
|
68
|
-
|
69
|
-
|
70
|
-
@bucket.object(
|
98
|
+
path = bucket_file_path(File.basename(file_path),
|
99
|
+
upload_folder)
|
100
|
+
@bucket.object(path).upload_file(file_path)
|
71
101
|
end
|
72
102
|
|
103
|
+
# Make file public
|
104
|
+
# @param file_path [String] file to make public
|
105
|
+
# @return [Array<String, String>] public url and permissions
|
73
106
|
def make_public(file_path)
|
74
107
|
@bucket.object(file_path).acl.put(acl: 'public-read')
|
75
108
|
permission = @bucket.object(file_path).acl.grants.last.permission
|
76
109
|
[@bucket.object(file_path).public_url.to_s, permission]
|
77
110
|
end
|
78
111
|
|
112
|
+
# Get permissions for file
|
113
|
+
# @param file_path [String] path to file
|
114
|
+
# @return [Aws::S3::ObjectAcl] permissions
|
79
115
|
def get_permission_by_link(file_path)
|
80
116
|
@bucket.object(file_path).acl
|
81
117
|
end
|
82
118
|
|
83
|
-
|
119
|
+
# Upload file/folder and make public
|
120
|
+
# @param file_path [String] file to upload
|
121
|
+
# @param upload_folder [True, False] is this a folder
|
122
|
+
# @return [String] public url
|
123
|
+
def upload_file_and_make_public(file_path, upload_folder = nil)
|
84
124
|
upload_file(file_path, upload_folder)
|
85
|
-
make_public(
|
86
|
-
@bucket.object(
|
125
|
+
make_public(bucket_file_path(File.basename(file_path), upload_folder))
|
126
|
+
@bucket.object(bucket_file_path(File.basename(file_path),
|
127
|
+
upload_folder)).public_url
|
87
128
|
end
|
88
129
|
|
130
|
+
# Delete file by name
|
131
|
+
# @param file_path [String] name of file
|
132
|
+
# @return [nil]
|
89
133
|
def delete_file(file_path)
|
90
|
-
file_path.sub
|
134
|
+
file_path = file_path.sub('/', '') if file_path[0] == '/'
|
91
135
|
get_object(file_path).delete
|
92
136
|
end
|
137
|
+
|
138
|
+
# Get S3 key and S3 private key
|
139
|
+
# @param key_location [String] Path to search for key files
|
140
|
+
# @return [nil]
|
141
|
+
def read_keys(key_location = "#{Dir.home}/.s3")
|
142
|
+
@access_key_id = File.read("#{key_location}/key").strip
|
143
|
+
@secret_access_key = File.read("#{key_location}/private_key").strip
|
144
|
+
rescue Errno::ENOENT
|
145
|
+
raise Errno::ENOENT, "No key or private key found in #{key_location} "\
|
146
|
+
"Please create files #{key_location}/key "\
|
147
|
+
"and #{key_location}/private_key"
|
148
|
+
end
|
93
149
|
end
|
94
150
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OnlyofficeS3Wrapper
|
4
|
+
# Module for method to work with paths
|
5
|
+
module PathHelper
|
6
|
+
# @param filename [String] name of file to upload
|
7
|
+
# @param folder [String] folder to upload file
|
8
|
+
# @return [String] correct full path to file
|
9
|
+
def bucket_file_path(filename, folder)
|
10
|
+
return filename unless folder
|
11
|
+
|
12
|
+
folder = folder.sub('/', '') if folder[0] == '/'
|
13
|
+
folder = folder.chop if folder?(folder)
|
14
|
+
"#{folder}/#{filename}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onlyoffice_s3_wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ONLYOFFICE
|
@@ -10,36 +10,168 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-11-28 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name: aws-sdk
|
16
|
+
name: aws-sdk-s3
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
19
|
- - "~>"
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - "~>"
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '
|
28
|
+
version: '1'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: onlyoffice_file_helper
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
32
32
|
requirements:
|
33
33
|
- - "~>"
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: '0
|
35
|
+
version: '0'
|
36
36
|
type: :runtime
|
37
37
|
prerelease: false
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
40
|
- - "~>"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '0
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: overcommit
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - "~>"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rake
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '13'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '13'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: rspec
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '3'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: rubocop
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - "~>"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '1'
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - "~>"
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '1'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: rubocop-performance
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - "~>"
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '1'
|
106
|
+
type: :development
|
107
|
+
prerelease: false
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - "~>"
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '1'
|
113
|
+
- !ruby/object:Gem::Dependency
|
114
|
+
name: rubocop-rake
|
115
|
+
requirement: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - "~>"
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - "~>"
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: rubocop-rspec
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - "~>"
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '2'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - "~>"
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '2'
|
141
|
+
- !ruby/object:Gem::Dependency
|
142
|
+
name: simplecov
|
143
|
+
requirement: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - "~>"
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
type: :development
|
149
|
+
prerelease: false
|
150
|
+
version_requirements: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - "~>"
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
- !ruby/object:Gem::Dependency
|
156
|
+
name: yard
|
157
|
+
requirement: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - "~>"
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
- - ">="
|
163
|
+
- !ruby/object:Gem::Version
|
164
|
+
version: 0.9.20
|
165
|
+
type: :development
|
166
|
+
prerelease: false
|
167
|
+
version_requirements: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - "~>"
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: 0.9.20
|
43
175
|
description: ONLYOFFICE Helper Gem for S3. Used in QA
|
44
176
|
email:
|
45
177
|
- shockwavenn@gmail.com
|
@@ -48,13 +180,19 @@ executables: []
|
|
48
180
|
extensions: []
|
49
181
|
extra_rdoc_files: []
|
50
182
|
files:
|
51
|
-
- README.md
|
52
183
|
- lib/onlyoffice_s3_wrapper.rb
|
184
|
+
- lib/onlyoffice_s3_wrapper/name.rb
|
185
|
+
- lib/onlyoffice_s3_wrapper/path_helper.rb
|
53
186
|
- lib/onlyoffice_s3_wrapper/version.rb
|
54
|
-
homepage: https://github.com/
|
187
|
+
homepage: https://github.com/ONLYOFFICE-QA/onlyoffice_s3_wrapper
|
55
188
|
licenses:
|
56
189
|
- AGPL-3.0
|
57
|
-
metadata:
|
190
|
+
metadata:
|
191
|
+
bug_tracker_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_s3_wrapper/issues
|
192
|
+
changelog_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_s3_wrapper/blob/master/CHANGELOG.md
|
193
|
+
documentation_uri: https://www.rubydoc.info/gems/onlyoffice_s3_wrapper
|
194
|
+
homepage_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_s3_wrapper
|
195
|
+
source_code_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_s3_wrapper
|
58
196
|
post_install_message:
|
59
197
|
rdoc_options: []
|
60
198
|
require_paths:
|
@@ -63,15 +201,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
201
|
requirements:
|
64
202
|
- - ">="
|
65
203
|
- !ruby/object:Gem::Version
|
66
|
-
version: '
|
204
|
+
version: '2.5'
|
67
205
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
206
|
requirements:
|
69
207
|
- - ">="
|
70
208
|
- !ruby/object:Gem::Version
|
71
209
|
version: '0'
|
72
210
|
requirements: []
|
73
|
-
|
74
|
-
rubygems_version: 2.6.13
|
211
|
+
rubygems_version: 3.1.4
|
75
212
|
signing_key:
|
76
213
|
specification_version: 4
|
77
214
|
summary: ONLYOFFICE Helper Gem for S3
|
data/README.md
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
# OnlyofficeS3Wrapper
|
2
|
-
|
3
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/onlyoffice_s3_wrapper`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'onlyoffice_s3_wrapper'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install onlyoffice_s3_wrapper
|
22
|
-
|
23
|
-
## Usage
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
26
|
-
|
27
|
-
## Development
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/onlyoffice_s3_wrapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
-
|
37
|
-
## Code of Conduct
|
38
|
-
|
39
|
-
Everyone interacting in the OnlyofficeS3Wrapper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/onlyoffice_s3_wrapper/blob/master/CODE_OF_CONDUCT.md).
|