Class: Uc3DmpS3::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/uc3-dmp-s3/client.rb

Overview

A module to interact with the RDS DB. Expects the following ENV variables to be set:

DATABASE_HOST:  The host URL
DATABASE_PORT:  The port to use
DATABASE_NAME:  The name of the database

and the following should be passed into the :connect method:

RDS_USERNAME:   The RDS username
RDS_PASSWORD:   The RDS password

Constant Summary collapse

NARRATIVE_KEY_PREFIX =
'narratives/'
RESOURCE_KEY_PREFIX =
'external_resources/'
MSG_S3_FAILURE =
'Unable to save the object at this time'

Class Method Summary collapse

Class Method Details

.get_narrative(key:) ⇒ Object

Fetch the narrative file from the S3 bucket



66
67
68
69
70
71
72
73
# File 'lib/uc3-dmp-s3/client.rb', line 66

def get_narrative(key:)
  return nil unless key.is_a?(String) && !key.strip.empty? && !ENV['S3_BUCKET'].nil?

  obj = _get_object(key: key.start_with?(NARRATIVE_KEY_PREFIX) ? key : "#{NARRATIVE_KEY_PREFIX}#{key}")
  Base64.encode64(obj)
rescue Aws::Errors::ServiceError => e
  raise ClientError, "Unable to fetch PDF narrative from S3 bucket (key: #{key}) - #{e.message}"
end

.get_resource_file(key:) ⇒ Object

Fetch the narrative file from the S3 bucket



43
44
45
46
47
48
49
# File 'lib/uc3-dmp-s3/client.rb', line 43

def get_resource_file(key:)
  return nil unless key.is_a?(String) && !key.strip.empty? && !ENV['S3_BUCKET'].nil?

  obj = _get_object(key: key.start_with?(RESOURCE_KEY_PREFIX) ? key : "#{RESOURCE_KEY_PREFIX}#{key}")
rescue Aws::Errors::ServiceError => e
  raise ClientError, "Unable to fetch External Resource file from S3 bucket (key: #{key}) - #{e.message}"
end

.put_narrative(document:, dmp_id: nil, base64: false) ⇒ Object

Put the narrative file into the S3 bucket



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/uc3-dmp-s3/client.rb', line 52

def put_narrative(document:, dmp_id: nil, base64: false)
  return nil if !document.is_a?(String) || document.strip.empty? || ENV['S3_BUCKET'].nil?

  key = "#{NARRATIVE_KEY_PREFIX}#{SecureRandom.hex(8)}.pdf"
  tg = "DMP_ID=#{CGI.escape(dmp_id)}" unless dmp_id.nil?
  body = base64 ? Base64.decode64(document) : document

  _put_object(key:, tags: tg, payload: body)
rescue Aws::Errors::ServiceError => e
  msg = "Unable to write PDF narrative to S3 bucket (dmp_id: #{dmp_id})"
  raise ClientError, "#{msg} - #{e.message}"
end

.put_resource_file(file:, explorer_details:) ⇒ Object

Put the narrative file into the S3 bucket



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/uc3-dmp-s3/client.rb', line 28

def put_resource_file(file:, explorer_details:)
  return nil if File.nil? || !explorer_details.is_a?(Hash) || ENV['S3_BUCKET'].nil? ||
                explorer_details.fetch('file_metadata', {})['filename'].nil?

  filename = "#{explorer_details['ID']}_#{explorer_details['file_metadata']['filename']}"
  key = "#{RESOURCE_KEY_PREFIX}#{filename}"
  tg = "RESOURCE_TYPE=#{explorer_details['RESOURCE_TYPE']}&ID=#{explorer_details['ID']}"

  _put_object(key:, tags: tg, payload: file)
rescue Aws::Errors::ServiceError => e
  msg = "Unable to write External Resource file to S3 bucket (#{tg})"
  raise ClientError, "#{msg} - #{e.message}"
end