Class: Aws::S3::Object
- Inherits:
-
Object
- Object
- Aws::S3::Object
- Defined in:
- lib/aws-sdk-resources/services/s3/object.rb
Instance Method Summary collapse
-
#copy_from(source, options = {}) ⇒ Object
Copies another object to this object.
-
#copy_to(target, options = {}) ⇒ Object
Copies this object to another object.
-
#download_file(destination, options = {}) ⇒ Boolean
Downloads a file in S3 to a path on disk.
-
#move_to(target, options = {}) ⇒ void
Copies and deletes the current object.
-
#presigned_post(options = {}) ⇒ PresignedPost
Creates a PresignedPost that makes it easy to upload a file from a web browser direct to Amazon S3 using an HTML post form with a file field.
-
#presigned_url(http_method, params = {}) ⇒ String
Generates a pre-signed URL for this object.
-
#public_url(options = {}) ⇒ String
Returns the public (un-signed) URL for this object.
-
#upload_file(source, options = {}) ⇒ Boolean
Uploads a file from disk to the current object in S3.
Instance Method Details
#copy_from(source, options = {}) ⇒ Object
Copies another object to this object. Use ‘multipart_copy: true` for large objects. This is required for objects that exceed 5GB.
61 62 63 64 65 66 67 68 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 61 def copy_from(source, = {}) if Hash === source && source[:copy_source] # for backwards compatibility @client.copy_object(source.merge(bucket: bucket_name, key: key)) else ObjectCopier.new(self, ).copy_from(source, ) end end |
#copy_to(target, options = {}) ⇒ Object
If you need to copy to a bucket in a different region, use #copy_from.
Copies this object to another object. Use ‘multipart_copy: true` for large objects. This is required for objects that exceed 5GB.
102 103 104 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 102 def copy_to(target, = {}) ObjectCopier.new(self, ).copy_to(target, ) end |
#download_file(destination, options = {}) ⇒ Boolean
Downloads a file in S3 to a path on disk.
# small files (< 5MB) are downloaded in a single API call
obj.download_file('/path/to/file')
Files larger than 5MB are downloaded using multipart method
# large files are split into parts
# and the parts are downloaded in parallel
obj.download_file('/path/to/very_large_file')
282 283 284 285 286 287 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 282 def download_file(destination, = {}) downloader = FileDownloader.new(client: client) downloader.download( destination, .merge(bucket: bucket_name, key: key)) true end |
#move_to(target, options = {}) ⇒ void
This method returns an undefined value.
Copies and deletes the current object. The object will only be deleted if the copy operation succeeds.
113 114 115 116 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 113 def move_to(target, = {}) copy_to(target, ) delete end |
#presigned_post(options = {}) ⇒ PresignedPost
Creates a PresignedPost that makes it easy to upload a file from a web browser direct to Amazon S3 using an HTML post form with a file field.
See the PresignedPost documentation for more information.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 127 def presigned_post( = {}) PresignedPost.new( client.config.credentials, client.config.region, bucket_name, { key: key, url: bucket.url, }.merge() ) end |
#presigned_url(http_method, params = {}) ⇒ String
Generates a pre-signed URL for this object.
187 188 189 190 191 192 193 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 187 def presigned_url(http_method, params = {}) presigner = Presigner.new(client: client) presigner.presigned_url("#{http_method.downcase}_object", params.merge( bucket: bucket_name, key: key, )) end |
#public_url(options = {}) ⇒ String
Returns the public (un-signed) URL for this object.
s3.bucket('bucket-name').object('obj-key').public_url
#=> "https://bucket-name.s3.amazonaws.com/obj-key"
To use virtual hosted bucket url (disables https):
s3.bucket('my.bucket.com').object('key').public_url(virtual_host: true)
#=> "http://my.bucket.com/key"
210 211 212 213 214 215 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 210 def public_url( = {}) url = URI.parse(bucket.url()) url.path += '/' unless url.path[-1] == '/' url.path += key.gsub(/[^\/]+/) { |s| Seahorse::Util.uri_escape(s) } url.to_s end |
#upload_file(source, options = {}) ⇒ Boolean
Uploads a file from disk to the current object in S3.
# small files are uploaded in a single API call
obj.upload_file('/path/to/file')
Files larger than ‘:multipart_threshold` are uploaded using the Amazon S3 multipart upload APIs.
# large files are automatically split into parts
# and the parts are uploaded in parallel
obj.upload_file('/path/to/very_large_file')
247 248 249 250 251 252 253 254 |
# File 'lib/aws-sdk-resources/services/s3/object.rb', line 247 def upload_file(source, = {}) = .dup uploader = FileUploader.new( multipart_threshold: .delete(:multipart_threshold), client: client) uploader.upload(source, .merge(bucket: bucket_name, key: key)) true end |