Class: BFS::Bucket::S3
- Inherits:
-
Abstract
- Object
- Abstract
- BFS::Bucket::S3
- Defined in:
- lib/bfs/bucket/s3.rb
Overview
S3 buckets are operating on s3
Instance Attribute Summary collapse
-
#acl ⇒ Object
readonly
Returns the value of attribute acl.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sse ⇒ Object
readonly
Returns the value of attribute sse.
-
#storage_class ⇒ Object
readonly
Returns the value of attribute storage_class.
Instance Method Summary collapse
-
#cp(src, dst, **opts) ⇒ Object
Copies a file.
-
#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object
Creates a new file and opens it for writing.
-
#glob(pattern = '**/*', **opts) ⇒ Object
Iterates over the contents of a bucket using a glob pattern.
-
#info(path, **opts) ⇒ Object
Info returns the object info.
-
#initialize(name, **opts) ⇒ S3
constructor
Initializes a new S3 bucket.
-
#ls(pattern = '**/*', **opts) ⇒ Object
Lists the contents of a bucket using a glob pattern.
-
#open(path, encoding: self.encoding, tempdir: nil, **opts, &block) ⇒ Object
Opens an existing file for reading.
-
#rm(path, **opts) ⇒ Object
Deletes a file.
Constructor Details
#initialize(name, **opts) ⇒ S3
Initializes a new S3 bucket
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
#acl ⇒ Object (readonly)
Returns the value of attribute acl.
8 9 10 |
# File 'lib/bfs/bucket/s3.rb', line 8 def acl @acl end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/bfs/bucket/s3.rb', line 8 def name @name end |
#sse ⇒ Object (readonly)
Returns the value of attribute sse.
8 9 10 |
# File 'lib/bfs/bucket/s3.rb', line 8 def sse @sse end |
#storage_class ⇒ Object (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
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: (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
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 |