Class: AWS::S3::ObjectMetadata

Inherits:
Object
  • Object
show all
Includes:
Core::Model
Defined in:
lib/aws/s3/object_metadata.rb

Overview

Returns an object that represents the metadata for an S3 object.

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(object, options = {}) ⇒ ObjectMetadata

Returns a new instance of ObjectMetadata.

Parameters:

  • object (S3Object)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :version_id (String)

    A specific version of the object to get metadata for


26
27
28
29
30
# File 'lib/aws/s3/object_metadata.rb', line 26

def initialize object, options = {}
  @object = object
  @version_id = options[:version_id]
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ String?

Proxies the method to #[].

Returns:

  • (String, nil)

    Returns the metadata for the given name.


78
79
80
81
# File 'lib/aws/s3/object_metadata.rb', line 78

def method_missing name, *args, &blk
  return super if !args.empty? || blk
  self[name]
end

Instance Attribute Details

#objectS3Object (readonly)

Returns:


33
34
35
# File 'lib/aws/s3/object_metadata.rb', line 33

def object
  @object
end

Instance Method Details

#[](name) ⇒ String?

Returns the value for the given name stored in the S3Object's metadata:

bucket.objects['myobject'].['purpose']
# returns nil if the given metadata key has not been set

Parameters:

  • name (String, Symbol)

    The name of the metadata field to get.

Returns:

  • (String, nil)

    Returns the metadata for the given name.


45
46
47
# File 'lib/aws/s3/object_metadata.rb', line 45

def [] name
  to_h[name.to_s]
end

#[]=(name, value) ⇒ String?

Note:

The name and value of each metadata field must conform to US-ASCII.

Changes the value of the given name stored in the S3Object's metadata:

object = bucket.object['myobject']
object.['purpose'] = 'research'
object.['purpose']               # => 'research'

Parameters:

  • name (String, Symbol)

    The name of the metadata field to set.

  • value (String)

    The new value of the metadata field.

Returns:

  • (String, nil)

    Returns the value that was set.


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

def []= name, value
  raise "cannot change the metadata of an object version; "+
    "use S3Object#write to create a new version with different metadata" if
    @version_id
   = to_h.dup
  [name.to_s] = value
  object.copy_from(object.key,
                   :metadata => )
  value
end

#to_hHash

Returns the user-generated metadata stored with this S3 Object.

Returns:

  • (Hash)

    Returns the user-generated metadata stored with this S3 Object.


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

def to_h
  options = {}
  options[:bucket_name] = object.bucket.name
  options[:key] = object.key
  options[:version_id] = @version_id if @version_id
  client.head_object(options).meta
end