onlyoffice_s3_wrapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bad6470557003c5cd85d5ea551afd742ac5bd2df
4
+ data.tar.gz: fe925f5ed9ce38860d13def655a17a77bd7e4331
5
+ SHA512:
6
+ metadata.gz: 19e92c37a0828842de80a7581394605b99a938bf86e80299669d977e1ed2876cfcbfb2fda7c6f8bae09914ca144e50c0cb1c5572d0982f8d631dd1d4ee2cbaf9
7
+ data.tar.gz: 6d5d36c93d978007ab7d068b7890f3e94ba5f2150f52184d4c42061eb54a502065ee2e57f40ea2db01ebf2ea32d3875144ef2e2de828a50790f3f78ecdcdf63d
data/README.md ADDED
@@ -0,0 +1,39 @@
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).
@@ -0,0 +1,3 @@
1
+ module OnlyofficeS3Wrapper
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,94 @@
1
+ require 'aws-sdk'
2
+ require 'open-uri'
3
+ require 'securerandom'
4
+ require 'onlyoffice_file_helper'
5
+ require 'onlyoffice_s3_wrapper/version'
6
+
7
+ module OnlyofficeS3Wrapper
8
+ # Class for working with amazon s3
9
+ class AmazonS3Wrapper
10
+ attr_accessor :s3, :bucket, :download_folder, :access_key_id, :secret_access_key
11
+
12
+ def initialize(bucket_name: 'nct-data-share', region: 'us-west-2')
13
+ @access_key_id = ENV['S3_KEY']
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
23
+ Aws.config = { access_key_id: @access_key_id,
24
+ secret_access_key: @secret_access_key,
25
+ region: region }
26
+ @s3 = Aws::S3::Resource.new
27
+ @bucket = @s3.bucket(bucket_name)
28
+ @download_folder = Dir.mktmpdir('amazon-s3-downloads')
29
+ end
30
+
31
+ def get_files_by_prefix(prefix = nil)
32
+ @bucket.objects(prefix: prefix).collect(&:key).reject { |file| is_folder?(file) }
33
+ end
34
+
35
+ # param [String] prefix
36
+ # return [Array] of folder names with '/' in end and filenames with fullpath (started ad prefix)
37
+ def get_elements_by_prefix(prefix = nil)
38
+ @bucket.objects(prefix: prefix).collect(&:key)
39
+ end
40
+
41
+ def is_folder?(str)
42
+ str.end_with? '/'
43
+ end
44
+
45
+ def get_object(obj_name)
46
+ @bucket.object(obj_name)
47
+ end
48
+
49
+ def download_file_by_name(file_name, download_folder = @download_folder)
50
+ OnlyofficeLoggerHelper.log("Download file with name #{file_name} to folder #{download_folder}")
51
+ OnlyofficeLoggerHelper.log('Try to find file:')
52
+ object = get_object(file_name)
53
+ download_object(object, download_folder)
54
+ end
55
+
56
+ def download_object(object, download_folder = @download_folder)
57
+ link = object.presigned_url(:get, expires_in: 3600)
58
+ OnlyofficeLoggerHelper.log("Try to download object with name #{object.key} to folder #{download_folder}")
59
+ File.open("#{download_folder}/#{File.basename(object.key)}", 'w') do |f|
60
+ IO.copy_stream(open(link), f)
61
+ end
62
+ OnlyofficeLoggerHelper.log("File with name #{object.key} successfully downloaded to folder #{download_folder}")
63
+ rescue StandardError
64
+ raise("File with name #{object.key} is not found un bucket #{@bucket.name}")
65
+ end
66
+
67
+ def upload_file(file_path, upload_folder)
68
+ upload_folder.sub!('/', '') if upload_folder[0] == '/'
69
+ upload_folder.chop! if is_folder?(upload_folder)
70
+ @bucket.object("#{upload_folder}/#{File.basename(file_path)}").upload_file(file_path)
71
+ end
72
+
73
+ def make_public(file_path)
74
+ @bucket.object(file_path).acl.put(acl: 'public-read')
75
+ permission = @bucket.object(file_path).acl.grants.last.permission
76
+ [@bucket.object(file_path).public_url.to_s, permission]
77
+ end
78
+
79
+ def get_permission_by_link(file_path)
80
+ @bucket.object(file_path).acl
81
+ end
82
+
83
+ def upload_file_and_make_public(file_path, upload_folder)
84
+ upload_file(file_path, upload_folder)
85
+ make_public("#{upload_folder}/#{File.basename(file_path)}")
86
+ @bucket.object("#{upload_folder}/#{File.basename(file_path)}").public_url
87
+ end
88
+
89
+ def delete_file(file_path)
90
+ file_path.sub!('/', '') if file_path[0] == '/'
91
+ get_object(file_path).delete
92
+ end
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onlyoffice_s3_wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ONLYOFFICE
8
+ - Pavel Lobashov
9
+ - Dmitry Rotaty
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2017-10-31 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: aws-sdk
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: onlyoffice_file_helper
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.1'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.1'
43
+ description: ONLYOFFICE Helper Gem for S3. Used in QA
44
+ email:
45
+ - shockwavenn@gmail.com
46
+ - flamine@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - README.md
52
+ - lib/onlyoffice_s3_wrapper.rb
53
+ - lib/onlyoffice_s3_wrapper/version.rb
54
+ homepage: https://github.com/onlyoffice-testing-robot/onlyoffice_s3_wrapper
55
+ licenses:
56
+ - AGPL-3.0
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.6.13
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: ONLYOFFICE Helper Gem for S3
78
+ test_files: []