Class: PULS3Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode = "precuration", bucket_name: nil) ⇒ PULS3Client

See config/s3.yml for configuration file.

Examples:

S3Client.new(“precuration”)

S3Client.new(bucket_name: “example-bucket-two”)

Parameters:

  • mode (String) (defaults to: "precuration")

    See constant options above This value controls the AWS S3 bucket used to access the files.

  • "optional (String)

    bucket name to override the bucket name defined by the mode“



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_nameObject (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_responseObject (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_sizeObject (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

.configurationObject



30
31
32
# File 'app/services/pul_s3_client.rb', line 30

def configuration
  Rails.configuration.s3
end

.embargo_configObject



46
47
48
# File 'app/services/pul_s3_client.rb', line 46

def embargo_config
  configuration.embargo
end

.post_curation_configObject



38
39
40
# File 'app/services/pul_s3_client.rb', line 38

def post_curation_config
  configuration.post_curation
end

.pre_curation_configObject



34
35
36
# File 'app/services/pul_s3_client.rb', line 34

def pre_curation_config
  configuration.pre_curation
end

.preservation_configObject



42
43
44
# File 'app/services/pul_s3_client.rb', line 42

def preservation_config
  configuration.preservation
end

Instance Method Details

#access_key_idObject



77
78
79
# File 'app/services/pul_s3_client.rb', line 77

def access_key_id
  self.class.configuration["access_key_id"]
end

#clientObject



89
90
91
# File 'app/services/pul_s3_client.rb', line 89

def client
  @client ||= Aws::S3::Client.new(region:, credentials:)
end

#configObject



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.embargo_config
  else
    raise ArgumentError, "Invalid mode value: #{@mode}"
  end
end

#credentialsObject



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

Returns:

  • (Boolean)


69
70
71
# File 'app/services/pul_s3_client.rb', line 69

def post_curation?
  @mode == POSTCURATION
end

#pre_curation?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/services/pul_s3_client.rb', line 65

def pre_curation?
  @mode == PRECURATION
end

#regionObject



73
74
75
# File 'app/services/pul_s3_client.rb', line 73

def region
  config.fetch(:region, nil)
end

#secret_access_keyObject



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
  message = "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(message)
  raise aws_service_error
end