Class: Gridium::GridiumS3
- Inherits:
-
Object
- Object
- Gridium::GridiumS3
- Defined in:
- lib/s3.rb
Instance Method Summary collapse
- #_sanitize_string(input_string) ⇒ Object
- #_validate_path(path_to_file) ⇒ Object
- #_validate_string(input_string) ⇒ Object
- #_verify_upload(s3_name, local_absolute_path) ⇒ Object
- #create_s3_name(file_name) ⇒ Object
-
#initialize(project_name, subdirectory_path = 'screenshots') ⇒ GridiumS3
constructor
A new instance of GridiumS3.
-
#save_file(local_path) ⇒ String
Save local file to S3 bucket.
Constructor Details
#initialize(project_name, subdirectory_path = 'screenshots') ⇒ GridiumS3
Returns a new instance of GridiumS3.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/s3.rb', line 5 def initialize(project_name, subdirectory_path='screenshots') Log.debug("[GRIDIUM::S3] initializing GridiumS3 with #{project_name} and #{subdirectory_path}") Aws.config.update({ credentials: Aws::Credentials.new(ENV['S3_ACCESS_KEY_ID'], ENV['S3_SECRET_ACCESS_KEY']) , region: ENV['S3_DEFAULT_REGION']}) _validate_string(project_name) _validate_string(subdirectory_path) @project_name = _sanitize_string(project_name) @subdirectory_path = _sanitize_string(subdirectory_path) @bucket = Aws::S3::Resource.new.bucket(ENV['S3_ROOT_BUCKET']) end |
Instance Method Details
#_sanitize_string(input_string) ⇒ Object
Note:
Strips whitespace, split and join to collapse contiguous white space, replace whitespace and special chars (not ‘.’, ‘/’) with an underscore
49 50 51 |
# File 'lib/s3.rb', line 49 def _sanitize_string(input_string) input_string.strip.split.join(" ").gsub(/[^\w.\/]/i, '_') end |
#_validate_path(path_to_file) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/s3.rb', line 60 def _validate_path(path_to_file) Log.debug("[GRIDIUM::S3] attempting to validate #{path_to_file} as a legitimate path") unless File.exist? path_to_file raise(ArgumentError, "[GRIDIUM::S3] this path doesn't resolve #{path_to_file}") end end |
#_validate_string(input_string) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/s3.rb', line 53 def _validate_string(input_string) Log.debug("[GRIDIUM::S3] attempting to validate #{input_string} for use as a name") if input_string.empty? || input_string.strip.empty? raise(ArgumentError, "[GRIDIUM::S3] empty and/or whitespace file names are not wanted here.") end end |
#_verify_upload(s3_name, local_absolute_path) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/s3.rb', line 67 def _verify_upload(s3_name, local_absolute_path) upload_size = @bucket.object(s3_name).content_length local_size = File.size local_absolute_path Log.debug("[GRIDIUM::S3] file upload verified: #{upload_size == local_size}. upload size is #{upload_size} and local size is #{local_size}") upload_size == local_size end |
#create_s3_name(file_name) ⇒ Object
39 40 41 42 43 |
# File 'lib/s3.rb', line 39 def create_s3_name(file_name) _validate_string(file_name) file_name = _sanitize_string(file_name) [@project_name, @subdirectory_path, file_name].join("/") end |
#save_file(local_path) ⇒ String
Save local file to S3 bucket
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/s3.rb', line 19 def save_file(local_path) Log.debug("[GRIDIUM::S3] attempting to save '#{local_path}' to s3") _validate_path(local_path) file_name = File.basename(local_path) destination_name = create_s3_name(file_name) begin @bucket.object(destination_name).upload_file(local_path) @bucket.object(destination_name).wait_until_exists _verify_upload(destination_name, local_path) # @bucket.object(s3_name).presigned_url(:get, expires_in: 3600) #uncomment this if public url ends up not working out OPREQ-83850 return @bucket.object(destination_name).public_url rescue Aws::S3::Errors::InvalidAccessKeyId Log.error("[GRIDIUM::S3] unable to save file to s3 due to Aws::S3::Errors::InvalidAccessKeyId") rescue Seahorse::Client::NetworkingError => error Log.error("[GRIDIUM::S3] unable to save file to s3 due to underlying network error: #{error}") rescue StandardError => error Log.error("[GRIDIUM::S3] unable to save file to s3 due to unexpected error: #{error}") end end |