Class: Attachie::S3Driver
- Inherits:
-
Object
- Object
- Attachie::S3Driver
- Defined in:
- lib/attachie/s3_driver.rb
Instance Attribute Summary collapse
-
#s3_client ⇒ Object
Returns the value of attribute s3_client.
-
#s3_resource ⇒ Object
Returns the value of attribute s3_resource.
Instance Method Summary collapse
- #delete(name, bucket) ⇒ Object
- #download(name, bucket, path) ⇒ Object
- #exists?(name, bucket) ⇒ Boolean
- #info(name, bucket) ⇒ Object
-
#initialize(s3_client) ⇒ S3Driver
constructor
A new instance of S3Driver.
- #list(bucket, prefix: nil) ⇒ Object
- #presigned_post(name, bucket, options = {}) ⇒ Object
- #store(name, data_or_io, bucket, options = {}) ⇒ Object
- #store_multipart(name, bucket, options = {}, &block) ⇒ Object
- #temp_url(name, bucket, options = {}) ⇒ Object
- #value(name, bucket) ⇒ Object
Constructor Details
#initialize(s3_client) ⇒ S3Driver
Returns a new instance of S3Driver.
57 58 59 60 |
# File 'lib/attachie/s3_driver.rb', line 57 def initialize(s3_client) self.s3_client = s3_client self.s3_resource = Aws::S3::Resource.new(client: s3_client) end |
Instance Attribute Details
#s3_client ⇒ Object
Returns the value of attribute s3_client.
55 56 57 |
# File 'lib/attachie/s3_driver.rb', line 55 def s3_client @s3_client end |
#s3_resource ⇒ Object
Returns the value of attribute s3_resource.
55 56 57 |
# File 'lib/attachie/s3_driver.rb', line 55 def s3_resource @s3_resource end |
Instance Method Details
#delete(name, bucket) ⇒ Object
127 128 129 130 131 |
# File 'lib/attachie/s3_driver.rb', line 127 def delete(name, bucket) s3_resource.bucket(bucket).object(name).delete true end |
#download(name, bucket, path) ⇒ Object
121 122 123 124 125 |
# File 'lib/attachie/s3_driver.rb', line 121 def download(name, bucket, path) s3_resource.bucket(bucket).object(name).download_file(path) rescue Aws::S3::Errors::NotFound => e raise ItemNotFound, e. end |
#exists?(name, bucket) ⇒ Boolean
133 134 135 |
# File 'lib/attachie/s3_driver.rb', line 133 def exists?(name, bucket) s3_resource.bucket(bucket).object(name).exists? end |
#info(name, bucket) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/attachie/s3_driver.rb', line 79 def info(name, bucket) object = s3_resource.bucket(bucket).object(name) { content_length: object.content_length, last_modified: object.last_modified, content_type: object.content_type } rescue Aws::S3::Errors::NotFound => e raise ItemNotFound, e. end |
#list(bucket, prefix: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/attachie/s3_driver.rb', line 68 def list(bucket, prefix: nil) return enum_for(:list, bucket, prefix: prefix) unless block_given? = {} [:prefix] = prefix if prefix s3_resource.bucket(bucket).objects().each do |object| yield object.key end end |
#presigned_post(name, bucket, options = {}) ⇒ Object
62 63 64 65 66 |
# File 'lib/attachie/s3_driver.rb', line 62 def presigned_post(name, bucket, = {}) res = s3_resource.bucket(bucket).object(name).presigned_post() return { fields: res.fields, headers: {}, method: "post", url: res.url } end |
#store(name, data_or_io, bucket, options = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/attachie/s3_driver.rb', line 91 def store(name, data_or_io, bucket, = {}) opts = .dup mime_type = MIME::Types.of(name).first opts[:content_type] ||= mime_type.content_type if mime_type opts[:content_type] ||= "application/octet-stream" opts[:body] = data_or_io s3_resource.bucket(bucket).object(name).put(opts) end |
#store_multipart(name, bucket, options = {}, &block) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/attachie/s3_driver.rb', line 104 def store_multipart(name, bucket, = {}, &block) opts = .dup mime_type = MIME::Types.of(name).first opts[:content_type] ||= mime_type.content_type if mime_type opts[:content_type] ||= "application/octet-stream" S3MultipartUpload.new(s3_client, name, bucket, opts, &block) end |
#temp_url(name, bucket, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/attachie/s3_driver.rb', line 137 def temp_url(name, bucket, = {}) opts = .dup opts[:expires_in] = opts.delete(:expires_in).to_i if opts.key?(:expires_in) method = opts.delete(:method) || :get s3_resource.bucket(bucket).object(name).presigned_url(method, opts) end |
#value(name, bucket) ⇒ Object
115 116 117 118 119 |
# File 'lib/attachie/s3_driver.rb', line 115 def value(name, bucket) s3_resource.bucket(bucket).object(name).get.body.read.force_encoding(Encoding::BINARY) rescue Aws::S3::Errors::NotFound => e raise ItemNotFound, e. end |