Class: Saviour::S3Storage
- Inherits:
-
Object
- Object
- Saviour::S3Storage
- Defined in:
- lib/saviour/s3_storage.rb
Constant Summary collapse
- MissingPublicUrlPrefix =
Class.new(StandardError)
- KeyTooLarge =
Class.new(StandardError)
Instance Method Summary collapse
- #cp(source_path, destination_path) ⇒ Object
- #delete(path) ⇒ Object
- #exists?(path) ⇒ Boolean
-
#initialize(conf = {}) ⇒ S3Storage
constructor
A new instance of S3Storage.
- #mv(source_path, destination_path) ⇒ Object
- #public_url(path) ⇒ Object
- #read(path) ⇒ Object
- #read_to_file(path, dest_file) ⇒ Object
- #write(file_or_contents, path) ⇒ Object
- #write_from_file(file, path) ⇒ Object
Constructor Details
#initialize(conf = {}) ⇒ S3Storage
Returns a new instance of S3Storage.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/saviour/s3_storage.rb', line 13 def initialize(conf = {}) @bucket = conf.delete(:bucket) @public_url_prefix = conf.delete(:public_url_prefix) = conf.delete(:aws_client_opts) @conf = conf = conf.delete(:create_options) { {} } conf.fetch(:aws_access_key_id) { raise(ArgumentError, "aws_access_key_id is required") } conf.fetch(:aws_secret_access_key) { raise(ArgumentError, "aws_secret_access_key is required") } @region = conf[:region] || raise(ArgumentError, "region is required") end |
Instance Method Details
#cp(source_path, destination_path) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/saviour/s3_storage.rb', line 93 def cp(source_path, destination_path) source_path = sanitize_leading_slash(source_path) destination_path = sanitize_leading_slash(destination_path) client.copy_object( .merge( copy_source: "/#{@bucket}/#{source_path}", bucket: @bucket, key: destination_path ) ) rescue Aws::S3::Errors::NoSuchKey raise FileNotPresent, "Trying to cp an unexisting path: #{source_path}" end |
#delete(path) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/saviour/s3_storage.rb', line 66 def delete(path) path = sanitize_leading_slash(path) client.delete_object( bucket: @bucket, key: path ) end |
#exists?(path) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/saviour/s3_storage.rb', line 75 def exists?(path) path = sanitize_leading_slash(path) !!client.head_object( bucket: @bucket, key: path ) rescue Aws::S3::Errors::NotFound false end |
#mv(source_path, destination_path) ⇒ Object
108 109 110 111 |
# File 'lib/saviour/s3_storage.rb', line 108 def mv(source_path, destination_path) cp(source_path, destination_path) delete(source_path) end |
#public_url(path) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/saviour/s3_storage.rb', line 86 def public_url(path) raise(MissingPublicUrlPrefix, "You must provide a `public_url_prefix`") unless public_url_prefix path = sanitize_leading_slash(path) ::File.join(public_url_prefix, path) end |
#read(path) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/saviour/s3_storage.rb', line 58 def read(path) path = sanitize_leading_slash(path) client.get_object(bucket: @bucket, key: path).body.read rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey raise FileNotPresent, "Trying to read an unexisting path: #{path}" end |
#read_to_file(path, dest_file) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/saviour/s3_storage.rb', line 46 def read_to_file(path, dest_file) path = sanitize_leading_slash(path) dest_file.binmode dest_file.rewind dest_file.truncate(0) client.get_object({ bucket: @bucket, key: path }, target: dest_file) rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey raise FileNotPresent, "Trying to read an unexisting path: #{path}" end |
#write(file_or_contents, path) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/saviour/s3_storage.rb', line 24 def write(file_or_contents, path) path = sanitize_leading_slash(path) # http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html if path.bytesize > 1024 raise(KeyTooLarge, "The key in S3 must be at max 1024 bytes, this key is too big: #{path}") end mime_type = Marcel::MimeType.for file_or_contents # TODO: Use multipart api client.put_object(.merge( body: file_or_contents, bucket: @bucket, key: path, content_type: mime_type )) end |
#write_from_file(file, path) ⇒ Object
40 41 42 43 44 |
# File 'lib/saviour/s3_storage.rb', line 40 def write_from_file(file, path) file.rewind write(file, path) end |