mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Add s3 bucket tagging support
This commit is contained in:
parent
052768c95e
commit
e7a6e55143
5 changed files with 148 additions and 0 deletions
37
lib/fog/aws/parsers/storage/get_bucket_tagging.rb
Normal file
37
lib/fog/aws/parsers/storage/get_bucket_tagging.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
module Fog
|
||||
module Parsers
|
||||
module Storage
|
||||
module AWS
|
||||
|
||||
class GetBucketTagging < Fog::Parsers::Base
|
||||
|
||||
def reset
|
||||
@in_tag = {}
|
||||
@response = {'BucketTagging' => {}}
|
||||
end
|
||||
|
||||
def start_element(name, *args)
|
||||
super
|
||||
if name == 'Tag'
|
||||
@in_tag = {}
|
||||
end
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
case name
|
||||
when 'Tag'
|
||||
@response['BucketTagging'].merge!(@in_tag)
|
||||
@in_tag = {}
|
||||
when 'Key'
|
||||
@in_tag[value] = nil
|
||||
when 'Value'
|
||||
@in_tag = {@in_tag.keys.first => value}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
29
lib/fog/aws/requests/storage/delete_bucket_tagging.rb
Normal file
29
lib/fog/aws/requests/storage/delete_bucket_tagging.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
class Real
|
||||
|
||||
# Delete tagging for a bucket
|
||||
#
|
||||
# @param bucket_name [String] name of bucket to delete tagging from
|
||||
#
|
||||
# @return [Excon::Response] response:
|
||||
# * status [Integer] - 204
|
||||
#
|
||||
# @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketDELETEtagging.html
|
||||
|
||||
def delete_bucket_tagging(bucket_name)
|
||||
request({
|
||||
:expects => 204,
|
||||
:headers => {},
|
||||
:bucket_name => bucket_name,
|
||||
:method => 'DELETE',
|
||||
:query => {'tagging' => nil}
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
37
lib/fog/aws/requests/storage/get_bucket_tagging.rb
Normal file
37
lib/fog/aws/requests/storage/get_bucket_tagging.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
class Real
|
||||
|
||||
require 'fog/aws/parsers/storage/get_bucket_tagging'
|
||||
|
||||
# Get tags for an S3 bucket
|
||||
#
|
||||
# @param bucket_name [String] name of bucket to get tags for
|
||||
#
|
||||
# @return [Excon::Response] response:
|
||||
# * body [Hash]:
|
||||
# * BucketTagging [Hash]:
|
||||
# * Key [String] - tag key
|
||||
# * Value [String] - tag value
|
||||
# @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETtagging.html
|
||||
|
||||
def get_bucket_tagging(bucket_name)
|
||||
unless bucket_name
|
||||
raise ArgumentError.new('bucket_name is required')
|
||||
end
|
||||
request({
|
||||
:expects => 200,
|
||||
:headers => {},
|
||||
:bucket_name => bucket_name,
|
||||
:idempotent => true,
|
||||
:method => 'GET',
|
||||
:parser => Fog::Parsers::Storage::AWS::GetBucketTagging.new,
|
||||
:query => {'tagging' => nil}
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
41
lib/fog/aws/requests/storage/put_bucket_tagging.rb
Normal file
41
lib/fog/aws/requests/storage/put_bucket_tagging.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
module Fog
|
||||
module Storage
|
||||
class AWS
|
||||
class Real
|
||||
|
||||
# Change tag set for an S3 bucket
|
||||
#
|
||||
# @param bucket_name [String] name of bucket to modify
|
||||
# @param tags [Hash]:
|
||||
# * Key [String]: tag key
|
||||
# * Value [String]: tag value
|
||||
#
|
||||
# @see http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTtagging.html
|
||||
|
||||
def put_bucket_tagging(bucket_name, tags)
|
||||
tagging = tags.map do |k,v|
|
||||
"<Tag><Key>#{k}</Key><Value>#{v}</Value></Tag>"
|
||||
end.join("\n")
|
||||
data =
|
||||
<<-DATA
|
||||
<Tagging xmlns="http://doc.s3.amazonaws.com/2006-03-01" >
|
||||
<TagSet>
|
||||
#{tagging}
|
||||
</TagSet>
|
||||
</Tagging>
|
||||
DATA
|
||||
|
||||
request({
|
||||
:body => data,
|
||||
:expects => 204,
|
||||
:headers => {'Content-MD5' => Base64.encode64(Digest::MD5.digest(data)).chomp!, 'Content-Type' => 'application/xml'},
|
||||
:bucket_name => bucket_name,
|
||||
:method => 'PUT',
|
||||
:query => {'tagging' => nil}
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -34,6 +34,7 @@ module Fog
|
|||
response-content-type
|
||||
response-expires
|
||||
restore
|
||||
tagging
|
||||
torrent
|
||||
uploadId
|
||||
uploads
|
||||
|
@ -65,6 +66,7 @@ module Fog
|
|||
request :delete_bucket_website
|
||||
request :delete_object
|
||||
request :delete_multiple_objects
|
||||
request :delete_bucket_tagging
|
||||
request :get_bucket
|
||||
request :get_bucket_acl
|
||||
request :get_bucket_cors
|
||||
|
@ -73,6 +75,7 @@ module Fog
|
|||
request :get_bucket_logging
|
||||
request :get_bucket_object_versions
|
||||
request :get_bucket_policy
|
||||
request :get_bucket_tagging
|
||||
request :get_bucket_versioning
|
||||
request :get_bucket_website
|
||||
request :get_object
|
||||
|
@ -95,6 +98,7 @@ module Fog
|
|||
request :put_bucket_lifecycle
|
||||
request :put_bucket_logging
|
||||
request :put_bucket_policy
|
||||
request :put_bucket_tagging
|
||||
request :put_bucket_versioning
|
||||
request :put_bucket_website
|
||||
request :put_object
|
||||
|
|
Loading…
Reference in a new issue