Class: AWS::S3::BucketTagCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/s3/bucket_tag_collection.rb

Overview

Manages tags for a single S3 Bucket.

Examples:

Setting a tag.


bucket.tags['key'] = 'value'

Getting a tag.


bucket.tags['key']
#=> 'value'

Getting all tags


bucket.tags.to_h
#=> { 'key' => 'value', ... }

Removing all tags


bucket.tags.clear

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, options = {}) ⇒ BucketTagCollection



43
44
45
46
# File 'lib/aws/s3/bucket_tag_collection.rb', line 43

def initialize bucket, options = {}
  @bucket = bucket
  super
end

Instance Attribute Details

#bucketBucket (readonly)



49
50
51
# File 'lib/aws/s3/bucket_tag_collection.rb', line 49

def bucket
  @bucket
end

Instance Method Details

#[](key) ⇒ String?



54
55
56
# File 'lib/aws/s3/bucket_tag_collection.rb', line 54

def [] key
  self.to_h[key]
end

#[]=(key, value) ⇒ Object



60
61
62
# File 'lib/aws/s3/bucket_tag_collection.rb', line 60

def []= key, value
  self.set(self.to_h.merge(key => value))
end

#clearnil

Removes all tags from the bucket.

bucket.tags.clear
bucket.tags.to_h #=> {}


81
82
83
84
# File 'lib/aws/s3/bucket_tag_collection.rb', line 81

def clear
  client.delete_bucket_tagging(:bucket_name => bucket.name)
  nil
end

#eql?(other) ⇒ Boolean Also known as: ==



97
98
99
# File 'lib/aws/s3/bucket_tag_collection.rb', line 97

def eql? other
  self.to_h == other
end

#set(tags) ⇒ nil



66
67
68
69
70
71
72
73
# File 'lib/aws/s3/bucket_tag_collection.rb', line 66

def set tags
  if tags.nil? or tags.empty?
    self.clear
  else
    client.put_bucket_tagging(:bucket_name => bucket.name, :tags => tags)
  end
  nil
end

#to_hHash Also known as: to_hash



87
88
89
90
91
# File 'lib/aws/s3/bucket_tag_collection.rb', line 87

def to_h
  client.get_bucket_tagging(:bucket_name => bucket.name).data[:tags]
rescue AWS::S3::Errors::NoSuchTagSet
  {}
end