Module: Conduit::Storage::Aws::ClassMethods

Defined in:
lib/conduit/storage/aws.rb

Instance Method Summary collapse

Instance Method Details

#bucketObject

Bucket we want to work with

Configurable in: config/initializers/conduit.rb



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/conduit/storage/aws.rb', line 47

def bucket
  @bucket ||= begin
    bucket_name = config[:bucket]
    bucket = s3.bucket(bucket_name)

    if bucket.exists?
      bucket
    else
      s3.buckets.create(bucket)
      s3.bucket(bucket)
    end
  end
end

#configureObject

Configure Aws::S3 with credentials if provided, else, assume IAM will provide them.



21
22
23
24
25
26
27
28
29
# File 'lib/conduit/storage/aws.rb', line 21

def configure
  if %i[aws_access_key_id aws_access_secret].all? { |key| config.key?(key) }
    credentials = ::Aws::Credentials.new(config[:aws_access_key_id],
                                       config[:aws_access_secret])
    ::Aws.config.update(credentials: credentials)
  else
    ::Aws.config
  end
end

#delete(key) ⇒ Object

Delete a file from Aws::S3

e.g.

> Conduit::Storage.delete(‘/path/to/file’)



86
87
88
89
90
# File 'lib/conduit/storage/aws.rb', line 86

def delete(key)
  bucket.object(key).delete
rescue ::Aws::S3::Errors::NoSuchKey
  nil
end

#read(key) ⇒ Object

Read a file from Aws::S3

e.g.

> Conduit::Storage.read(‘/path/to/file’)



75
76
77
78
79
# File 'lib/conduit/storage/aws.rb', line 75

def read(key)
  bucket.object(key).get.body.read
rescue ::Aws::S3::Errors::NoSuchKey
  nil
end

#s3Object

Primary connection object to Aws::S3

Configurable in: config/initializers/conduit.rb TODO: Update how conduit gets the credentials for s3



38
39
40
# File 'lib/conduit/storage/aws.rb', line 38

def s3
  @s3 ||= ::Aws::S3::Resource.new
end

#write(key, content) ⇒ Object

Write a file to Aws::S3

e.g.

> Conduit::Storage.write(‘/path/to/file’, ‘foo’)



66
67
68
# File 'lib/conduit/storage/aws.rb', line 66

def write(key, content)
  bucket.object(key).put(body: content)
end