Class: S4
- Inherits:
-
Object
- Object
- S4
- Defined in:
- lib/s4.rb
Overview
Simpler AWS S3 library
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.0.1"
- SubResources =
sub-resource names which may appear in the query string and also must be signed against.
%w( acl location logging notification partNumber policy requestPayment torrent uploadId uploads versionId versioning versions website )
- HeaderValues =
Header over-rides which may appear in the query string and also must be signed against.
%w( response-content-type response-content-language response-expires reponse-cache-control response-content-disposition response-content-encoding )
Instance Attribute Summary collapse
-
#access_key_id ⇒ Object
readonly
Returns the value of attribute access_key_id.
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#secret_access_key ⇒ Object
readonly
Returns the value of attribute secret_access_key.
Instance Method Summary collapse
-
#delete(name) ⇒ Object
Delete the object with the given name.
-
#download(name, destination = nil) ⇒ Object
Download the file with the given filename to the given destination.
-
#get(name, &block) ⇒ Object
Lower level object get which just yields the successful S3 response to the block.
-
#initialize(s3_url = ENV["S3_URL"]) ⇒ S4
constructor
A new instance of S4.
-
#list(prefix = "") ⇒ Object
List bucket contents.
- #put(io, name) ⇒ Object
-
#upload(name, destination = nil) ⇒ Object
Upload the file with the given filename to the given destination in your S3 bucket.
Constructor Details
#initialize(s3_url = ENV["S3_URL"]) ⇒ S4
Returns a new instance of S4.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/s4.rb', line 20 def initialize(s3_url = ENV["S3_URL"]) raise ArgumentError, "No S3 URL provided. You can set ENV['S3_URL'], too." if s3_url.nil? || s3_url.empty? begin url = URI(s3_url) rescue URI::InvalidURIError => e e. << " The format is s3://access_key_id:[email protected]/bucket" raise e end @access_key_id = url.user @secret_access_key = URI.unescape(url.password || "") @host = url.host @bucket = url.path[1..-1] end |
Instance Attribute Details
#access_key_id ⇒ Object (readonly)
Returns the value of attribute access_key_id.
18 19 20 |
# File 'lib/s4.rb', line 18 def access_key_id @access_key_id end |
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
18 19 20 |
# File 'lib/s4.rb', line 18 def bucket @bucket end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/s4.rb', line 18 def connection @connection end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
18 19 20 |
# File 'lib/s4.rb', line 18 def host @host end |
#secret_access_key ⇒ Object (readonly)
Returns the value of attribute secret_access_key.
18 19 20 |
# File 'lib/s4.rb', line 18 def secret_access_key @secret_access_key end |
Instance Method Details
#delete(name) ⇒ Object
Delete the object with the given name.
58 59 60 |
# File 'lib/s4.rb', line 58 def delete(name) request(uri = uri(name), Net::HTTP::Delete.new(uri.request_uri)) end |
#download(name, destination = nil) ⇒ Object
Download the file with the given filename to the given destination.
47 48 49 50 51 52 53 54 55 |
# File 'lib/s4.rb', line 47 def download(name, destination=nil) get(name) do |response| File.open(destination || File.join(Dir.pwd, File.basename(name)), "wb") do |io| response.read_body do |chunk| io.write(chunk) end end end end |
#get(name, &block) ⇒ Object
Lower level object get which just yields the successful S3 response to the block. See #download if you want to simply copy a file from S3 to local.
42 43 44 |
# File 'lib/s4.rb', line 42 def get(name, &block) request(uri(name), &block) end |
#list(prefix = "") ⇒ Object
List bucket contents
81 82 83 |
# File 'lib/s4.rb', line 81 def list(prefix = "") REXML::Document.new(request(uri("", query: "prefix=#{prefix}"))).elements.collect("//Key", &:text) end |
#put(io, name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/s4.rb', line 69 def put(io, name) uri = uri(name) req = Net::HTTP::Put.new(uri.request_uri) req.body_stream = io req.add_field "Content-Length", io.size req.add_field "Content-Type", "application/x-www-form-urlencoded" request(URI::HTTP.build(host: host, path: "/#{bucket}/#{name}"), req) end |
#upload(name, destination = nil) ⇒ Object
Upload the file with the given filename to the given destination in your S3 bucket. If no destination is given then uploads it with the same filename to the root of your bucket.
65 66 67 |
# File 'lib/s4.rb', line 65 def upload(name, destination=nil) put File.open(name, "rb"), destination || File.basename(name) end |