Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aws|storage] Add bucket lifecycle / object expiration requests.
- Loading branch information
Showing
6 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Fog | ||
module Parsers | ||
module Storage | ||
module AWS | ||
|
||
class GetBucketLifecycle < Fog::Parsers::Base | ||
|
||
def reset | ||
@rule = {} | ||
@response = { 'Rules' => [] } | ||
end | ||
|
||
def end_element(name) | ||
case name | ||
when 'ID', 'Prefix' | ||
@rule[name] = value | ||
when 'Status' | ||
@rule['Enabled'] = value == 'Enabled' | ||
when 'Days' | ||
@rule[name] = value.to_i | ||
when 'Rule' | ||
@response['Rules'] << @rule | ||
@rule = {} | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Fog | ||
module Storage | ||
class AWS | ||
class Real | ||
|
||
# Delete lifecycle configuration for a bucket | ||
# | ||
# ==== Parameters | ||
# * bucket_name<~String> - name of bucket to delete lifecycle configuration from | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * status<~Integer> - 204 | ||
# | ||
# ==== See Also | ||
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketDELETElifecycle.html | ||
|
||
def delete_bucket_lifecycle(bucket_name) | ||
request({ | ||
:expects => 204, | ||
:headers => {}, | ||
:host => "#{bucket_name}.#{@host}", | ||
:method => 'DELETE', | ||
:query => {'lifecycle' => nil} | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Fog | ||
module Storage | ||
class AWS | ||
class Real | ||
|
||
require 'fog/aws/parsers/storage/get_bucket_lifecycle' | ||
|
||
# Get bucket lifecycle configuration | ||
# | ||
# ==== Parameters | ||
# * bucket_name<~String> - name of bucket to get lifecycle configuration for | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'Rules'<~Array> - object expire rules | ||
# * 'ID'<~String> - Unique identifier for the rule | ||
# * 'Prefix'<~String> - Prefix identifying one or more objects to which the rule applies | ||
# * 'Enabled'<~Boolean> - if rule is currently being applied | ||
# * 'Days'<~Integer> - lifetime, in days, of the objects that are subject to the rule | ||
# | ||
# ==== See Also | ||
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketGETlifecycle.html | ||
|
||
def get_bucket_lifecycle(bucket_name) | ||
request({ | ||
:expects => 200, | ||
:headers => {}, | ||
:host => "#{bucket_name}.#{@host}", | ||
:idempotent => true, | ||
:method => 'GET', | ||
:parser => Fog::Parsers::Storage::AWS::GetBucketLifecycle.new, | ||
:query => {'lifecycle' => nil} | ||
}) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Fog | ||
module Storage | ||
class AWS | ||
class Real | ||
|
||
# Change lifecycle configuration for an S3 bucket | ||
# | ||
# ==== Parameters | ||
# * bucket_name<~String> - name of bucket to set lifecycle configuration for | ||
# * lifecycle<~Hash>: | ||
# * 'Rules'<~Array> - object expire rules | ||
# * 'ID'<~String> - Unique identifier for the rule | ||
# * 'Prefix'<~String> - Prefix identifying one or more objects to which the rule applies | ||
# * 'Enabled'<~Boolean> - if rule is currently being applied | ||
# * 'Days'<~Integer> - lifetime, in days, of the objects that are subject to the rule | ||
# ==== See Also | ||
# http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html | ||
|
||
def put_bucket_lifecycle(bucket_name, lifecycle) | ||
builder = Nokogiri::XML::Builder.new do | ||
LifecycleConfiguration { | ||
lifecycle['Rules'].each do |rule| | ||
Rule { | ||
ID rule['ID'] | ||
Prefix rule['Prefix'] | ||
Status rule['Enabled'] ? 'Enabled' : 'Disabled' | ||
Expiration { Days rule['Days'] } | ||
} | ||
end | ||
} | ||
end | ||
|
||
body = builder.to_xml | ||
|
||
request({ | ||
:body => body, | ||
:expects => 200, | ||
:headers => {'Content-MD5' => Base64.encode64(Digest::MD5.digest(body)).chomp!, | ||
'Content-Type' => 'application/xml'}, | ||
:host => "#{bucket_name}.#{@host}", | ||
:method => 'PUT', | ||
:query => {'lifecycle' => nil} | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters