Class: Stashify::Directory::AWS::S3

Inherits:
Stashify::Directory show all
Defined in:
lib/stashify/directory/aws/s3.rb

Overview

An implementation for interacting with AWS S3 buckets as if they had directories with “/” as a path separator. In addition to a path, it also needs a Aws::S3::Bucket object representing the bucket the file resides within.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, path:) ⇒ S3

Returns a new instance of S3.



16
17
18
19
# File 'lib/stashify/directory/aws/s3.rb', line 16

def initialize(bucket:, path:)
  @bucket = bucket
  super(path: path)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



14
15
16
# File 'lib/stashify/directory/aws/s3.rb', line 14

def bucket
  @bucket
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/stashify/directory/aws/s3.rb', line 14

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
# File 'lib/stashify/directory/aws/s3.rb', line 51

def ==(other)
  self.class == other.class && @bucket == other.bucket && path == other.path
end

#directory(name) ⇒ Object



39
40
41
# File 'lib/stashify/directory/aws/s3.rb', line 39

def directory(name)
  Stashify::Directory::AWS::S3.new(bucket: @bucket, path: path_of(name))
end

#directory?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/stashify/directory/aws/s3.rb', line 35

def directory?(name)
  @bucket.objects(prefix: path_of(name, "")).count.positive?
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/stashify/directory/aws/s3.rb', line 43

def exists?(name)
  @bucket.object(::File.join(path, name)).exists?
end

#file(name) ⇒ Object



47
48
49
# File 'lib/stashify/directory/aws/s3.rb', line 47

def file(name)
  Stashify::File::AWS::S3.new(bucket: @bucket, path: ::File.join(path, name))
end

#filesObject



28
29
30
31
32
33
# File 'lib/stashify/directory/aws/s3.rb', line 28

def files
  @bucket.objects.map do |object|
    key = object.key
    file(::File.basename(key)) if key =~ %r{^#{Regexp.escape(path)}/([^/]*)(/.*)?$}
  end.compact
end

#parentObject



21
22
23
24
25
26
# File 'lib/stashify/directory/aws/s3.rb', line 21

def parent
  Stashify::Directory::AWS::S3.new(
    bucket: @bucket,
    path: ::File.dirname(path),
  )
end