Class: OnlyofficeS3Wrapper::AmazonS3Wrapper
- Inherits:
-
Object
- Object
- OnlyofficeS3Wrapper::AmazonS3Wrapper
- Includes:
- PathHelper
- Defined in:
- lib/onlyoffice_s3_wrapper.rb
Overview
Class for working with amazon s3
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
Returns default content type for uploaded files.
'binary/octet-stream'
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
writeonly
- String
-
Amazon key.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#download_folder ⇒ Object
Returns the value of attribute download_folder.
-
#secret_access_key ⇒ Object
writeonly
- String
-
Amazon secret key.
Instance Method Summary collapse
-
#delete_file(file_path) ⇒ nil
Delete file by name.
-
#download_file_by_name(file_name, download_location = nil) ⇒ String
Full path to file.
-
#download_object(object, download_folder = @download_folder) ⇒ String
Download object.
-
#files_from_folder(folder = nil) ⇒ Array<Object>
Get list of files from specific folder Will not use folder name as prefix, will always try to look at folder with exact specified name.
-
#folder?(str) ⇒ True, False
Is string path to folder.
-
#get_elements_by_prefix(prefix = nil) ⇒ Object
param [String] prefix return [Array] of folder names with ‘/’ in end and filenames with fullpath (started ad prefix).
-
#get_files_by_prefix(prefix = nil, field: :key) ⇒ Array<Object>
Get files by prefix.
-
#get_object(obj_name) ⇒ Object
Get object by name.
-
#get_permission_by_link(file_path) ⇒ Aws::S3::ObjectAcl
Get permissions for file.
-
#initialize(bucket_name: 'nct-data-share', region: 'us-west-2') ⇒ AmazonS3Wrapper
constructor
A new instance of AmazonS3Wrapper.
-
#make_public(file_path) ⇒ Array<String, String>
Make file public.
-
#read_keys(key_location = "#{Dir.home}/.s3") ⇒ nil
Get S3 key and S3 private key.
-
#upload_file(file_path, upload_folder, content_type = DEFAULT_CONTENT_TYPE) ⇒ nil
Upload file.
-
#upload_file_and_make_public(file_path, upload_folder = nil, content_type = DEFAULT_CONTENT_TYPE) ⇒ String
Upload file/folder and make public.
Methods included from PathHelper
Constructor Details
#initialize(bucket_name: 'nct-data-share', region: 'us-west-2') ⇒ AmazonS3Wrapper
Returns a new instance of AmazonS3Wrapper.
25 26 27 28 29 30 31 32 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 25 def initialize(bucket_name: 'nct-data-share', region: 'us-west-2') read_keys Aws.config = { access_key_id: @access_key_id, secret_access_key: @secret_access_key, region: region } @bucket = Aws::S3::Resource.new.bucket(bucket_name) @download_folder = Dir.mktmpdir('amazon-s3-downloads') end |
Instance Attribute Details
#access_key_id=(value) ⇒ Object (writeonly)
- String
-
Amazon key
21 22 23 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 21 def access_key_id=(value) @access_key_id = value end |
#bucket ⇒ Object
Returns the value of attribute bucket.
19 20 21 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 19 def bucket @bucket end |
#download_folder ⇒ Object
Returns the value of attribute download_folder.
19 20 21 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 19 def download_folder @download_folder end |
#secret_access_key=(value) ⇒ Object (writeonly)
- String
-
Amazon secret key
23 24 25 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 23 def secret_access_key=(value) @secret_access_key = value end |
Instance Method Details
#delete_file(file_path) ⇒ nil
Delete file by name
150 151 152 153 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 150 def delete_file(file_path) file_path = file_path.sub('/', '') if file_path[0] == '/' get_object(file_path).delete end |
#download_file_by_name(file_name, download_location = nil) ⇒ String
Returns full path to file.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 79 def download_file_by_name(file_name, download_location = nil) object = get_object(file_name) temp_location = download_object(object, @download_folder) OnlyofficeLoggerHelper.log("Temp downloaded file: #{temp_location}") return temp_location unless download_location download_location = "#{download_location}/#{File.basename(file_name)}" if File.directory?(download_location) FileUtils.mv(temp_location, download_location) download_location end |
#download_object(object, download_folder = @download_folder) ⇒ String
Download object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 95 def download_object(object, download_folder = @download_folder) file_name = "#{download_folder}/#{File.basename(object.key)}" link = object.presigned_url(:get, expires_in: 3600) File.open(file_name, 'w') do |f| IO.copy_stream(URI.parse(link).open, f) end file_name rescue StandardError => e raise("File #{file_name} download failed with: #{e}") end |
#files_from_folder(folder = nil) ⇒ Array<Object>
Get list of files from specific folder Will not use folder name as prefix, will always try to look at folder with exact specified name
49 50 51 52 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 49 def files_from_folder(folder = nil) folder = "#{folder}/" unless folder.end_with?('/') get_files_by_prefix(folder) end |
#folder?(str) ⇒ True, False
Is string path to folder
64 65 66 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 64 def folder?(str) str.end_with? '/' end |
#get_elements_by_prefix(prefix = nil) ⇒ Object
param [String] prefix return [Array] of folder names with ‘/’ in end and filenames with fullpath (started ad prefix)
57 58 59 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 57 def get_elements_by_prefix(prefix = nil) @bucket.objects(prefix: prefix).collect(&:key) end |
#get_files_by_prefix(prefix = nil, field: :key) ⇒ Array<Object>
Get files by prefix
38 39 40 41 42 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 38 def get_files_by_prefix(prefix = nil, field: :key) @bucket.objects(prefix: prefix) .collect(&field) .reject { |file| folder?(file) } end |
#get_object(obj_name) ⇒ Object
Get object by name
71 72 73 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 71 def get_object(obj_name) @bucket.object(obj_name) end |
#get_permission_by_link(file_path) ⇒ Aws::S3::ObjectAcl
Get permissions for file
129 130 131 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 129 def (file_path) @bucket.object(file_path).acl end |
#make_public(file_path) ⇒ Array<String, String>
Make file public
120 121 122 123 124 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 120 def make_public(file_path) @bucket.object(file_path).acl.put(acl: 'public-read') = @bucket.object(file_path).acl.grants.last. [@bucket.object(file_path).public_url.to_s, ] end |
#read_keys(key_location = "#{Dir.home}/.s3") ⇒ nil
Get S3 key and S3 private key
158 159 160 161 162 163 164 165 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 158 def read_keys(key_location = "#{Dir.home}/.s3") @access_key_id = File.read("#{key_location}/key").strip @secret_access_key = File.read("#{key_location}/private_key").strip rescue Errno::ENOENT raise Errno::ENOENT, "No key or private key found in #{key_location} " \ "Please create files #{key_location}/key " \ "and #{key_location}/private_key" end |
#upload_file(file_path, upload_folder, content_type = DEFAULT_CONTENT_TYPE) ⇒ nil
Upload file
111 112 113 114 115 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 111 def upload_file(file_path, upload_folder, content_type = DEFAULT_CONTENT_TYPE) path = bucket_file_path(File.basename(file_path), upload_folder) @bucket.object(path).upload_file(file_path, content_type: content_type) end |
#upload_file_and_make_public(file_path, upload_folder = nil, content_type = DEFAULT_CONTENT_TYPE) ⇒ String
Upload file/folder and make public
138 139 140 141 142 143 144 145 |
# File 'lib/onlyoffice_s3_wrapper.rb', line 138 def upload_file_and_make_public(file_path, upload_folder = nil, content_type = DEFAULT_CONTENT_TYPE) upload_file(file_path, upload_folder, content_type) make_public(bucket_file_path(File.basename(file_path), upload_folder)) @bucket.object(bucket_file_path(File.basename(file_path), upload_folder)).public_url end |