You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::S3::ObjectMetadata

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



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

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?

Deprecated.

In order to safely update an S3 object's metadata, you should use S3Object#copy_from. This operation does not preserve the ACL, storage class (standard vs. reduced redundancy) or server side encryption settings. Using this method on anything other than vanilla S3 objects risks clobbering other metadata values set on the object.

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.



72
73
74
75
76
77
78
79
80
81
# File 'lib/aws/s3/object_metadata.rb', line 72

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.



92
93
94
95
96
97
98
# File 'lib/aws/s3/object_metadata.rb', line 92

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