Class: BFS::Bucket::S3

Inherits:
Abstract
  • Object
show all
Defined in:
lib/bfs/bucket/s3.rb

Overview

S3 buckets are operating on s3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **opts) ⇒ S3

Initializes a new S3 bucket

Parameters:

  • name (String)

    the bucket name

  • opts (Hash)

    options

Options Hash (**opts):

  • :region (String)

    default region

  • :prefix (String)

    custom namespace within the bucket

  • :sse (String)

    default server-side-encryption setting

  • :credentials (Aws::Credentials)

    credentials object

  • :access_key_id (String)

    custom AWS access key ID

  • :secret_access_key (String)

    custom AWS secret access key

  • :profile_name (String)

    custom AWS profile name (for shared credentials)

  • :acl (Symbol)

    canned ACL

  • :storage_class (String)

    storage class

  • :client (Aws::S3::Client)

    custom client, uses default_client by default



23
24
25
26
27
28
29
30
31
32
# File 'lib/bfs/bucket/s3.rb', line 23

def initialize(name, **opts)
  super(**opts)

  @name = name
  @sse = opts[:sse] || opts[:server_side_encryption]
  @prefix = opts[:prefix]
  @acl = opts[:acl].to_sym if opts[:acl]
  @storage_class = opts[:storage_class]
  @client = opts[:client] || init_client(**opts)
end

Instance Attribute Details

#aclObject (readonly)

Returns the value of attribute acl.



8
9
10
# File 'lib/bfs/bucket/s3.rb', line 8

def acl
  @acl
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/bfs/bucket/s3.rb', line 8

def name
  @name
end

#sseObject (readonly)

Returns the value of attribute sse.



8
9
10
# File 'lib/bfs/bucket/s3.rb', line 8

def sse
  @sse
end

#storage_classObject (readonly)

Returns the value of attribute storage_class.



8
9
10
# File 'lib/bfs/bucket/s3.rb', line 8

def storage_class
  @storage_class
end

Instance Method Details

#cp(src, dst, **opts) ⇒ Object

Copies a file.



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bfs/bucket/s3.rb', line 123

def cp(src, dst, **opts)
  src = full_path(src)
  dst = full_path(dst)
  opts = opts.merge(
    bucket: name,
    copy_source: "/#{name}/#{src}",
    key: dst,
  )
  @client.copy_object(**opts)
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NotFound
  raise BFS::FileNotFound, trim_prefix(src)
end

#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object

Creates a new file and opens it for writing

Parameters:

  • path (String)
  • opts (Hash)

    options

Options Hash (**opts):

  • :encoding (String)

    Custom encoding.

  • :acl (String)

    custom ACL override

  • :server_side_encryption (String)

    SSE override

  • :storage_class (String)

    storage class override



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bfs/bucket/s3.rb', line 72

def create(path, encoding: self.encoding, perm: self.perm, **opts, &block)
  path = full_path(path)
  opts = opts.merge(
    bucket: name,
    key: path,
  )
  opts[:acl] ||= @acl if @acl
  opts[:server_side_encryption] ||= @sse if @sse
  opts[:storage_class] ||= @storage_class if @storage_class

  BFS::Writer.new(path, encoding: encoding, perm: perm) do |t|
    File.open(t, encoding: encoding) do |file|
      @client.put_object(opts.merge(body: file))
    end
  end.perform(&block)
end

#glob(pattern = '**/*', **opts) ⇒ Object

Iterates over the contents of a bucket using a glob pattern



44
45
46
47
48
49
50
51
# File 'lib/bfs/bucket/s3.rb', line 44

def glob(pattern = '**/*', **opts)
  Enumerator.new do |acc|
    walk(pattern, **opts) do |path, obj|
      info = BFS::FileInfo.new(path: path, size: obj.size, mtime: obj.last_modified)
      acc << info
    end
  end
end

#info(path, **opts) ⇒ Object

Info returns the object info



54
55
56
57
58
59
60
61
62
63
# File 'lib/bfs/bucket/s3.rb', line 54

def info(path, **opts)
  path = norm_path(path)
  opts = opts.merge(bucket: name, key: full_path(path))
  info = @client.head_object(**opts)
  raise BFS::FileNotFound, path unless info

  BFS::FileInfo.new path: path, size: info.content_length, mtime: info.last_modified, content_type: info.content_type, metadata: norm_meta(info.)
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NotFound
  raise BFS::FileNotFound, path
end

#ls(pattern = '**/*', **opts) ⇒ Object

Lists the contents of a bucket using a glob pattern



35
36
37
38
39
40
41
# File 'lib/bfs/bucket/s3.rb', line 35

def ls(pattern = '**/*', **opts)
  Enumerator.new do |acc|
    walk(pattern, **opts) do |path, _|
      acc << path
    end
  end
end

#open(path, encoding: self.encoding, tempdir: nil, **opts, &block) ⇒ Object

Opens an existing file for reading

Parameters:

  • path (String)
  • opts (Hash)

    options

Options Hash (**opts):

  • :encoding (String)

    Custom encoding.

  • :tempdir (String)

    Custom temp dir.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bfs/bucket/s3.rb', line 94

def open(path, encoding: self.encoding, tempdir: nil, **opts, &block)
  path = full_path(path)
  temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding)
  temp.close

  opts = opts.merge(
    response_target: temp.path,
    bucket: name,
    key: path,
  )
  @client.get_object(**opts)

  File.open(temp.path, encoding: encoding, &block)
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NotFound
  raise BFS::FileNotFound, trim_prefix(path)
end

#rm(path, **opts) ⇒ Object

Deletes a file.



112
113
114
115
116
117
118
119
120
# File 'lib/bfs/bucket/s3.rb', line 112

def rm(path, **opts)
  path = full_path(path)
  opts = opts.merge(
    bucket: name,
    key: path,
  )
  @client.delete_object(**opts)
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NoSuchBucket, Aws::S3::Errors::NotFound # rubocop:disable Lint/SuppressedException
end