Class: ArchiveStorage::Adapters::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/adapters/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ S3

Returns a new instance of S3.



14
15
16
# File 'lib/archive_storage/adapters/s3.rb', line 14

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/archive_storage/adapters/s3.rb', line 12

def config
  @config
end

Instance Method Details

#clientObject



108
109
110
111
112
113
114
# File 'lib/archive_storage/adapters/s3.rb', line 108

def client
  @client ||= begin
    require "aws-sdk-s3"

    Aws::S3::Client.new(client_options)
  end
end

#copy_from(source_adapter, source_key, target_key) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/archive_storage/adapters/s3.rb', line 58

def copy_from(source_adapter, source_key, target_key)
   = source_adapter.head(source_key)

  Tempfile.create("archive-storage-copy") do |tempfile|
    tempfile.binmode
    source_adapter.download_to(source_key, tempfile.path)
    upload_path(target_key, tempfile.path, content_type: .content_type)
  end
end

#delete(key) ⇒ Object



90
91
92
93
# File 'lib/archive_storage/adapters/s3.rb', line 90

def delete(key)
  client.delete_object(bucket: config.bucket, key: key)
  true
end

#download_to(key, path) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/archive_storage/adapters/s3.rb', line 48

def download_to(key, path)
  ::File.open(path, "wb") do |file|
    client.get_object(bucket: config.bucket, key: key) do |chunk|
      file.write(chunk)
    end
  end
rescue not_found_errors => error
  raise NotFoundError, error.message
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/archive_storage/adapters/s3.rb', line 83

def exists?(key)
  head(key)
  true
rescue NotFoundError
  false
end

#head(key) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/archive_storage/adapters/s3.rb', line 68

def head(key)
  response = client.head_object(bucket: config.bucket, key: key)

  Metadata.new(
    byte_size: response.content_length,
    content_type: response.content_type,
    etag: clean_etag(response.etag),
    checksum: response_checksum(response),
    checksum_algorithm: response_checksum_algorithm(response),
    metadata: response. || {}
  )
rescue not_found_errors => error
  raise NotFoundError, error.message
end

#read(key) ⇒ Object



42
43
44
45
46
# File 'lib/archive_storage/adapters/s3.rb', line 42

def read(key)
  client.get_object(bucket: config.bucket, key: key).body.read
rescue not_found_errors => error
  raise NotFoundError, error.message
end

#upload(key, file, content_type: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/archive_storage/adapters/s3.rb', line 18

def upload(key, file, content_type: nil)
  body, close_body = upload_body(file)

  client.put_object(
    bucket: config.bucket,
    key: key,
    body: body,
    content_type: content_type || detect_content_type(file)
  )
ensure
  body.close if close_body && body.respond_to?(:close)
end

#upload_path(key, path, content_type: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/archive_storage/adapters/s3.rb', line 31

def upload_path(key, path, content_type: nil)
  ::File.open(path, "rb") do |file|
    client.put_object(
      bucket: config.bucket,
      key: key,
      body: file,
      content_type: content_type
    )
  end
end

#url(key, expires_in: 3600, public: nil, **_options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/archive_storage/adapters/s3.rb', line 95

def url(key, expires_in: 3600, public: nil, **_options)
  if public || config.public?
    public_url(key)
  else
    presigner.presigned_url(
      :get_object,
      bucket: config.bucket,
      key: key,
      expires_in: expires_in
    )
  end
end