Class: PULS3Client
- Inherits:
-
Object
- Object
- PULS3Client
- Defined in:
- app/services/pul_s3_client.rb
Overview
A service to connect to an S3 bucket for information
Constant Summary collapse
- PRECURATION =
Mode options
"precuration"
- POSTCURATION =
"postcuration"
- PRESERVATION =
"preservation"
- EMBARGO =
"embargo"
Instance Attribute Summary collapse
-
#bucket_name ⇒ Object
readonly
Returns the value of attribute bucket_name.
-
#last_response ⇒ Object
readonly
Returns the value of attribute last_response.
-
#part_size ⇒ Object
readonly
Returns the value of attribute part_size.
Class Method Summary collapse
- .configuration ⇒ Object
- .embargo_config ⇒ Object
- .post_curation_config ⇒ Object
- .pre_curation_config ⇒ Object
- .preservation_config ⇒ Object
Instance Method Summary collapse
- #access_key_id ⇒ Object
- #client ⇒ Object
- #config ⇒ Object
- #credentials ⇒ Object
-
#initialize(mode = "precuration", bucket_name: nil) ⇒ PULS3Client
constructor
See config/s3.yml for configuration file.
- #post_curation? ⇒ Boolean
- #pre_curation? ⇒ Boolean
- #region ⇒ Object
- #secret_access_key ⇒ Object
- #upload_file(io:, target_key:, size:, md5_digest: nil) ⇒ Object
Constructor Details
#initialize(mode = "precuration", bucket_name: nil) ⇒ PULS3Client
See config/s3.yml for configuration file.
23 24 25 26 27 |
# File 'app/services/pul_s3_client.rb', line 23 def initialize(mode = "precuration", bucket_name: nil) @mode = mode @part_size = 5_368_709_120 # 5GB is the maximum part size for AWS @bucket_name = bucket_name || config.fetch(:bucket, nil) end |
Instance Attribute Details
#bucket_name ⇒ Object (readonly)
Returns the value of attribute bucket_name.
13 14 15 |
# File 'app/services/pul_s3_client.rb', line 13 def bucket_name @bucket_name end |
#last_response ⇒ Object (readonly)
Returns the value of attribute last_response.
13 14 15 |
# File 'app/services/pul_s3_client.rb', line 13 def last_response @last_response end |
#part_size ⇒ Object (readonly)
Returns the value of attribute part_size.
13 14 15 |
# File 'app/services/pul_s3_client.rb', line 13 def part_size @part_size end |
Class Method Details
.configuration ⇒ Object
30 31 32 |
# File 'app/services/pul_s3_client.rb', line 30 def configuration Rails.configuration.s3 end |
.embargo_config ⇒ Object
46 47 48 |
# File 'app/services/pul_s3_client.rb', line 46 def configuration. end |
.post_curation_config ⇒ Object
38 39 40 |
# File 'app/services/pul_s3_client.rb', line 38 def post_curation_config configuration.post_curation end |
.pre_curation_config ⇒ Object
34 35 36 |
# File 'app/services/pul_s3_client.rb', line 34 def pre_curation_config configuration.pre_curation end |
.preservation_config ⇒ Object
42 43 44 |
# File 'app/services/pul_s3_client.rb', line 42 def preservation_config configuration.preservation end |
Instance Method Details
#access_key_id ⇒ Object
77 78 79 |
# File 'app/services/pul_s3_client.rb', line 77 def access_key_id self.class.configuration["access_key_id"] end |
#client ⇒ Object
89 90 91 |
# File 'app/services/pul_s3_client.rb', line 89 def client @client ||= Aws::S3::Client.new(region:, credentials:) end |
#config ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/services/pul_s3_client.rb', line 51 def config if @mode == PRESERVATION self.class.preservation_config elsif @mode == POSTCURATION self.class.post_curation_config elsif @mode == PRECURATION self.class.pre_curation_config elsif @mode == EMBARGO self.class. else raise ArgumentError, "Invalid mode value: #{@mode}" end end |
#credentials ⇒ Object
85 86 87 |
# File 'app/services/pul_s3_client.rb', line 85 def credentials @credentials ||= Aws::Credentials.new(access_key_id, secret_access_key) end |
#post_curation? ⇒ Boolean
69 70 71 |
# File 'app/services/pul_s3_client.rb', line 69 def post_curation? @mode == POSTCURATION end |
#pre_curation? ⇒ Boolean
65 66 67 |
# File 'app/services/pul_s3_client.rb', line 65 def pre_curation? @mode == PRECURATION end |
#region ⇒ Object
73 74 75 |
# File 'app/services/pul_s3_client.rb', line 73 def region config.fetch(:region, nil) end |
#secret_access_key ⇒ Object
81 82 83 |
# File 'app/services/pul_s3_client.rb', line 81 def secret_access_key self.class.configuration["secret_access_key"] end |
#upload_file(io:, target_key:, size:, md5_digest: nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/services/pul_s3_client.rb', line 93 def upload_file(io:, target_key:, size:, md5_digest: nil) # upload file from io in a single request, may not exceed 5GB if size > part_size upload_multipart_file(target_key:, size:, io:) else md5_digest ||= md5(io:) @last_response = client.put_object(bucket: bucket_name, key: target_key, body: io, content_md5: md5_digest) end target_key rescue Aws::S3::Errors::SignatureDoesNotMatch => e Honeybadger.notify("Error Uploading file #{target_key} for object: s3://#{bucket_name}/ Signature did not match! error: #{e}") false rescue Aws::Errors::ServiceError => aws_service_error = "An error was encountered when requesting to create the AWS S3 Object in the bucket #{bucket_name} with the key #{target_key}: #{aws_service_error}" Rails.logger.error() raise aws_service_error end |