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

Returns a new instance of BucketTagCollection.

Parameters:

  • bucket (Bucket)
  • options (Hash) (defaults to: {})


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)

Returns:



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

def bucket
  @bucket
end

Instance Method Details

#[](key) ⇒ String?

Returns the tag for the given key. If there Returns nil if the key does not exist.

Parameters:

  • key (String)

Returns:

  • (String, nil)

    Returns the tag for the given key. If there Returns nil if the key does not exist.



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

def [] key
  self.to_h[key]
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (String)
  • value (String)


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.

Examples:


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

Returns:

  • (nil)


83
84
85
86
# File 'lib/aws/s3/bucket_tag_collection.rb', line 83

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

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

Returns true if the tags for this bucket match the passed hash.

Parameters:

  • other (Hash)

Returns:

  • (Boolean)

    Returns true if the tags for this bucket match the passed hash.



99
100
101
# File 'lib/aws/s3/bucket_tag_collection.rb', line 99

def eql? other
  self.to_h == other
end

#set(tags) ⇒ nil

Parameters:

  • tags (Hash<String,String>)

    A hash of tag keys and values.

Returns:

  • (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

Returns:

  • (Hash)


89
90
91
92
93
# File 'lib/aws/s3/bucket_tag_collection.rb', line 89

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