onlyoffice_s3_wrapper 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/onlyoffice_s3_wrapper.rb +30 -18
- data/lib/onlyoffice_s3_wrapper/path_helper.rb +17 -0
- data/lib/onlyoffice_s3_wrapper/version.rb +3 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4af2be8cb335b101363b5cef8e5cc0887399dd737283d5c28229f1468b37c5df
|
4
|
+
data.tar.gz: 6bdd4346707a555268285f21ee1b0a75d821998d94b5df73fc92f518a6cf1d20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e974d934b9c74d0d50e43ac55e1e20f6c8e81a939074c558e3cc574b26ac58f388e446765bb640212de4b8bca0f121b8fc56ca3ac04c0a32fa13ff755c415e8a
|
7
|
+
data.tar.gz: 7fe4920dfd38dd251b4c6f6161e194d2a6b118f3c685275f8f8e75fc7d139c98fa26fa52aa521bbbab2ff6ae71369d7e0407fbfb16c66373699c608365a0a0bf
|
@@ -1,13 +1,21 @@
|
|
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
|
|
7
10
|
module OnlyofficeS3Wrapper
|
8
11
|
# Class for working with amazon s3
|
9
12
|
class AmazonS3Wrapper
|
10
|
-
|
13
|
+
include PathHelper
|
14
|
+
attr_accessor :s3, :bucket, :download_folder
|
15
|
+
# [String] Amazon key
|
16
|
+
attr_writer :access_key_id
|
17
|
+
# [String] Amazon secret key
|
18
|
+
attr_writer :secret_access_key
|
11
19
|
|
12
20
|
def initialize(bucket_name: 'nct-data-share', region: 'us-west-2')
|
13
21
|
read_keys
|
@@ -20,7 +28,9 @@ module OnlyofficeS3Wrapper
|
|
20
28
|
end
|
21
29
|
|
22
30
|
def get_files_by_prefix(prefix = nil, field: :key)
|
23
|
-
@bucket.objects(prefix: prefix)
|
31
|
+
@bucket.objects(prefix: prefix)
|
32
|
+
.collect(&field)
|
33
|
+
.reject { |file| folder?(file) }
|
24
34
|
end
|
25
35
|
|
26
36
|
# param [String] prefix
|
@@ -39,30 +49,29 @@ module OnlyofficeS3Wrapper
|
|
39
49
|
end
|
40
50
|
|
41
51
|
def download_file_by_name(file_name, download_folder = @download_folder)
|
42
|
-
OnlyofficeLoggerHelper.log("Download file with name #{file_name} to "\
|
43
|
-
"folder #{download_folder}")
|
44
|
-
OnlyofficeLoggerHelper.log('Try to find file:')
|
45
52
|
object = get_object(file_name)
|
46
53
|
download_object(object, download_folder)
|
54
|
+
OnlyofficeLoggerHelper.log("Downloaded file with name #{file_name} to "\
|
55
|
+
"folder #{download_folder}")
|
47
56
|
end
|
48
57
|
|
49
58
|
def download_object(object, download_folder = @download_folder)
|
50
59
|
link = object.presigned_url(:get, expires_in: 3600)
|
51
|
-
OnlyofficeLoggerHelper.log(
|
52
|
-
"to
|
60
|
+
OnlyofficeLoggerHelper.log('Try to download object with name '\
|
61
|
+
"#{object.key} to #{download_folder}")
|
53
62
|
File.open("#{download_folder}/#{File.basename(object.key)}", 'w') do |f|
|
54
|
-
IO.copy_stream(
|
63
|
+
IO.copy_stream(URI.parse(link).open, f)
|
55
64
|
end
|
56
65
|
OnlyofficeLoggerHelper.log("File with name #{object.key} successfully "\
|
57
66
|
"downloaded to folder #{download_folder}")
|
58
67
|
rescue StandardError
|
59
|
-
raise("File
|
68
|
+
raise("File #{object.key} is not found un bucket #{@bucket.name}")
|
60
69
|
end
|
61
70
|
|
62
71
|
def upload_file(file_path, upload_folder)
|
63
|
-
|
64
|
-
|
65
|
-
@bucket.object(
|
72
|
+
path = bucket_file_path(File.basename(file_path),
|
73
|
+
upload_folder)
|
74
|
+
@bucket.object(path).upload_file(file_path)
|
66
75
|
end
|
67
76
|
|
68
77
|
def make_public(file_path)
|
@@ -75,14 +84,15 @@ module OnlyofficeS3Wrapper
|
|
75
84
|
@bucket.object(file_path).acl
|
76
85
|
end
|
77
86
|
|
78
|
-
def upload_file_and_make_public(file_path, upload_folder)
|
87
|
+
def upload_file_and_make_public(file_path, upload_folder = nil)
|
79
88
|
upload_file(file_path, upload_folder)
|
80
|
-
make_public(
|
81
|
-
@bucket.object(
|
89
|
+
make_public(bucket_file_path(File.basename(file_path), upload_folder))
|
90
|
+
@bucket.object(bucket_file_path(File.basename(file_path),
|
91
|
+
upload_folder)).public_url
|
82
92
|
end
|
83
93
|
|
84
94
|
def delete_file(file_path)
|
85
|
-
file_path.sub
|
95
|
+
file_path = file_path.sub('/', '') if file_path[0] == '/'
|
86
96
|
get_object(file_path).delete
|
87
97
|
end
|
88
98
|
|
@@ -92,10 +102,11 @@ module OnlyofficeS3Wrapper
|
|
92
102
|
# @return [Array <String>] list of keys
|
93
103
|
def read_keys
|
94
104
|
return if read_env_keys
|
105
|
+
|
95
106
|
@access_key_id = File.read(Dir.home + '/.s3/key').strip
|
96
107
|
@secret_access_key = File.read(Dir.home + '/.s3/private_key').strip
|
97
108
|
rescue Errno::ENOENT
|
98
|
-
raise Errno::ENOENT, "No key or private key found in #{Dir.home}/.s3/
|
109
|
+
raise Errno::ENOENT, "No key or private key found in #{Dir.home}/.s3/ "\
|
99
110
|
"Please create files #{Dir.home}/.s3/key "\
|
100
111
|
"and #{Dir.home}/.s3/private_key"
|
101
112
|
end
|
@@ -103,6 +114,7 @@ module OnlyofficeS3Wrapper
|
|
103
114
|
# Read keys from env variables
|
104
115
|
def read_env_keys
|
105
116
|
return false unless ENV['S3_KEY'] && ENV['S3_PRIVATE_KEY']
|
117
|
+
|
106
118
|
@access_key_id = ENV['S3_KEY']
|
107
119
|
@secret_access_key = ENV['S3_PRIVATE_KEY']
|
108
120
|
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ONLYOFFICE
|
@@ -10,22 +10,22 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-01-24 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
|
@@ -50,6 +50,7 @@ extra_rdoc_files: []
|
|
50
50
|
files:
|
51
51
|
- README.md
|
52
52
|
- lib/onlyoffice_s3_wrapper.rb
|
53
|
+
- lib/onlyoffice_s3_wrapper/path_helper.rb
|
53
54
|
- lib/onlyoffice_s3_wrapper/version.rb
|
54
55
|
homepage: https://github.com/onlyoffice-testing-robot/onlyoffice_s3_wrapper
|
55
56
|
licenses:
|
@@ -70,8 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
71
|
- !ruby/object:Gem::Version
|
71
72
|
version: '0'
|
72
73
|
requirements: []
|
73
|
-
|
74
|
-
rubygems_version: 2.7.3
|
74
|
+
rubygems_version: 3.0.6
|
75
75
|
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: ONLYOFFICE Helper Gem for S3
|