Class: NoopBackup::Stores::S3
- Defined in:
- lib/noop_backup/stores/s3.rb
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#region ⇒ Object
Returns the value of attribute region.
-
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
Instance Method Summary collapse
-
#backup!(key, stream) ⇒ Object
Prefer Aws::S3::TransferManager for streaming uploads if available.
- #cleanup!(key) ⇒ Object
-
#initialize ⇒ S3
constructor
A new instance of S3.
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ S3
Returns a new instance of S3.
7 8 9 10 11 12 |
# File 'lib/noop_backup/stores/s3.rb', line 7 def initialize @bucket = ENV["AWS_S3_BUCKET"] @region = ENV["AWS_REGION"] @access_key_id = ENV["AWS_ACCESS_KEY_ID"] @secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"] end |
Instance Attribute Details
#access_key_id ⇒ Object
Returns the value of attribute access_key_id.
5 6 7 |
# File 'lib/noop_backup/stores/s3.rb', line 5 def access_key_id @access_key_id end |
#bucket ⇒ Object
Returns the value of attribute bucket.
5 6 7 |
# File 'lib/noop_backup/stores/s3.rb', line 5 def bucket @bucket end |
#region ⇒ Object
Returns the value of attribute region.
5 6 7 |
# File 'lib/noop_backup/stores/s3.rb', line 5 def region @region end |
#secret_access_key ⇒ Object
Returns the value of attribute secret_access_key.
5 6 7 |
# File 'lib/noop_backup/stores/s3.rb', line 5 def secret_access_key @secret_access_key end |
Instance Method Details
#backup!(key, stream) ⇒ Object
Prefer Aws::S3::TransferManager for streaming uploads if available. Aws::S3::Resource.upload_stream is deprecated in newer versions
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/noop_backup/stores/s3.rb', line 16 def backup!(key, stream) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) bytes = 0 validate! upload_attempted = true if defined?(Aws::S3::TransferManager) manager = Aws::S3::TransferManager.new(client: s3_client) manager.upload_stream(bucket:, key: key, part_size: 8 * 1024 * 1024, thread_count: 2) do |s3_stream| bytes = IO.copy_stream(stream, s3_stream) end else object = Aws::S3::Resource.new(client: s3_client).bucket(bucket).object(key) object.upload_stream(part_size: 8 * 1024 * 1024, thread_count: 2) do |s3_stream| bytes = IO.copy_stream(stream, s3_stream) end end duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started if config.min_size && bytes < config.min_size.to_i raise NoopBackup::DumpTooSmallError, "backup too small: #{NoopBackup.utils.human_size(bytes)} < min_size (#{NoopBackup.utils.human_size(config.min_size)})" end Result.new(success: true, store: :s3, bytes:, key:, duration:) rescue => e duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started cleanup!(key) if upload_attempted Result.new(success: false, error: e, store: :s3, bytes:, key:, duration:) end |
#cleanup!(key) ⇒ Object
62 63 64 65 66 |
# File 'lib/noop_backup/stores/s3.rb', line 62 def cleanup!(key) s3_client.delete_object(bucket:, key: key) rescue => e warn "Failed to clean up partial upload #{key}: #{e.}" end |
#validate! ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/noop_backup/stores/s3.rb', line 53 def validate! raise NoopBackup::ConfigurationError, "bucket is not configured" if bucket.to_s.empty? if access_key_id.to_s.empty? != secret_access_key.to_s.empty? raise NoopBackup::ConfigurationError, "access_key_id and secret_access_key must both be set, or both left blank to use the default AWS credential chain" end end |