Module: IMW::Resources::Schemes::S3
- Defined in:
- lib/imw/resources/schemes/s3.rb
Overview
Defines methods for reading and writing data to Amazon S3 buckets.
IMW.open('s3://my_bucket/path/to/some/file.csv')
Learn more about Amazon Web Services.
Class Method Summary collapse
-
.copy(source, destination) ⇒ IMW::Resource
Copy S3 resource
source
todestination
. -
.get(source, destination) ⇒ IMW::Resource
Download
source
from S3 intodestination
. -
.put(source, destination) ⇒ IMW::Resource
Store
source
intodestination
.
Instance Method Summary collapse
-
#bucket ⇒ String
For an S3 resource, the bucket is just the hostname.
-
#cp(new_uri) ⇒ IMW::Resource
Copy this resource to the
new_uri
. -
#exist? ⇒ true, false
(also: #exists?)
Does this resource exist on S3?.
-
#on_s3? ⇒ true, false
(also: #is_s3?)
Is this resource an S3 resource?.
-
#read ⇒ String
Return the contents of this S3 object.
-
#rm ⇒ IMW::Resource
(also: #rm!)
Remove this resource from S3.
-
#s3_object ⇒ Object
The AWS::S3::S3Object corresponding to this resource.
-
#s3n_url ⇒ String
Return the S3N URL for this S3 object.
Class Method Details
.copy(source, destination) ⇒ IMW::Resource
Copy S3 resource source
to destination
.
111 112 113 114 115 116 117 118 |
# File 'lib/imw/resources/schemes/s3.rb', line 111 def self.copy source, destination source = IMW.open(source) destination = IMW.open(destination) raise IMW::PathError.new("Bucket names must be non-blank and match to 'copy'") unless source.bucket.present? && destination.bucket.present? && source.bucket == destination.bucket make_connection! AWS::S3::Object.copy(source.path, destination.path, destination.bucket) destination end |
.get(source, destination) ⇒ IMW::Resource
Download source
from S3 into destination
.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/imw/resources/schemes/s3.rb', line 95 def self.get source, destination source = IMW.open(source) destination = IMW.open(destination) make_connection! AWS::S3::Object.stream(source.path, source.bucket) do |chunk| destination.write(chunk) end destination.close destination.reopen end |
.put(source, destination) ⇒ IMW::Resource
Store source
into destination
.
81 82 83 84 85 86 87 88 |
# File 'lib/imw/resources/schemes/s3.rb', line 81 def self.put source, destination source = IMW.open(source) destintation = IMW.open(destination) raise IMW::ArgumentError.new("destination must be on S3 -- #{destination.uri} given") unless destination.on_s3? make_connection! AWS::S3::S3Object.store(destination.path, source.io, destination.bucket) destination end |
Instance Method Details
#bucket ⇒ String
For an S3 resource, the bucket is just the hostname.
16 17 18 |
# File 'lib/imw/resources/schemes/s3.rb', line 16 def bucket host end |
#cp(new_uri) ⇒ IMW::Resource
Copy this resource to the new_uri
.
32 33 34 |
# File 'lib/imw/resources/schemes/s3.rb', line 32 def cp new_uri IMW::Transforms::Transferer.new(:cp, self, new_uri).transfer! end |
#exist? ⇒ true, false Also known as: exists?
Does this resource exist on S3?
45 46 47 |
# File 'lib/imw/resources/schemes/s3.rb', line 45 def exist? s3_object.exists? end |
#on_s3? ⇒ true, false Also known as: is_s3?
Is this resource an S3 resource?
23 24 25 |
# File 'lib/imw/resources/schemes/s3.rb', line 23 def on_s3? true end |
#read ⇒ String
Return the contents of this S3 object.
72 73 74 |
# File 'lib/imw/resources/schemes/s3.rb', line 72 def read s3_object.value end |
#rm ⇒ IMW::Resource Also known as: rm!
Remove this resource from S3.
53 54 55 |
# File 'lib/imw/resources/schemes/s3.rb', line 53 def rm s3_object.delete end |
#s3_object ⇒ Object
The AWS::S3::S3Object corresponding to this resource.
37 38 39 40 |
# File 'lib/imw/resources/schemes/s3.rb', line 37 def s3_object self.class.make_connection! @s3_object ||= AWS::S3::S3Object.new(path, bucket) end |
#s3n_url ⇒ String
Return the S3N URL for this S3 object
resource = IMW.open('s3://my_bucket/path/to/some/obj')
resource.s3n_url
=> 's3n://my_bucket/path/to/some/obj'
65 66 67 |
# File 'lib/imw/resources/schemes/s3.rb', line 65 def s3n_url uri.to_s.gsub(/^s3:/, 's3n:') end |