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

Class: AWS::S3::ObjectVersion

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

Overview

Represents a single version of an S3Object.

When you enable versioning on a S3 bucket, writing to an object will create an object version instead of replacing the existing object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, version_id, options = {}) ⇒ ObjectVersion

Returns a new instance of ObjectVersion

Parameters:

  • object (S3Object)

    The object this is a version of.

  • version_id (String)

    The unique id for this version.

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

Options Hash (options):

  • :delete_marker (Boolean)

    Is this version a delete marker?

  • :last_modified (DateTime)

    Date and time the object was last modified.



33
34
35
36
37
38
39
# File 'lib/aws/s3/object_version.rb', line 33

def initialize(object, version_id, options = {})
  @object = object
  @version_id = version_id
  @delete_marker = options[:delete_marker]
  @last_modified = options[:last_modified]
  super
end

Instance Attribute Details

#last_modifiedDateTime (readonly)

Returns timestamp of this version

Returns:

  • (DateTime)

    timestamp of this version



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

def last_modified
  @last_modified
end

#objectS3Object (readonly)

Returns the object this is a version of.

Returns:

  • (S3Object)

    the object this is a version of.



42
43
44
# File 'lib/aws/s3/object_version.rb', line 42

def object
  @object
end

#version_idString (readonly)

Returns The unique version identifier.

Returns:

  • (String)

    The unique version identifier.



52
53
54
# File 'lib/aws/s3/object_version.rb', line 52

def version_id
  @version_id
end

Instance Method Details

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

Returns true if the other object version has the same s3 object key and version id.

Returns:

  • (Boolean)

    Returns true if the other object version has the same s3 object key and version id.



139
140
141
142
143
# File 'lib/aws/s3/object_version.rb', line 139

def ==(other)
  other.kind_of?(ObjectVersion) and
    other.object == object and
    other.version_id == version_id
end

#bucketObject



47
48
49
# File 'lib/aws/s3/object_version.rb', line 47

def bucket
  object.bucket
end

#content_lengthInteger

Returns Size of the object in bytes.

Returns:

  • (Integer)

    Size of the object in bytes.



77
78
79
# File 'lib/aws/s3/object_version.rb', line 77

def content_length
  head.content_length
end

#content_typeString

Note:

S3 does not compute content-type. It reports the content-type as was reported during the file upload.

Returns the content type as reported by S3, defaults to an empty string when not provided during upload.

Returns:

  • (String)

    Returns the content type as reported by S3, defaults to an empty string when not provided during upload.

See Also:



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

def content_type
  head.content_type
end

#delete(options = {}) ⇒ nil

Deletes this object version from S3.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :mfa (String)

    The serial number and current token code of the Multi-Factor Authentication (MFA) device for the user. Format is "SERIAL TOKEN" - with a space between the serial and token.

Returns:

  • (nil)


107
108
109
110
111
# File 'lib/aws/s3/object_version.rb', line 107

def delete(options = {})
  object.delete(:version_id => @version_id,
                :mfa        => options[:mfa]
               )
end

#delete_marker?Boolean

If you delete an object in a versioned bucket, a delete marker is created.

Returns:

  • (Boolean)

    Returns true if this version is a delete marker.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/aws/s3/object_version.rb', line 123

def delete_marker?
  if @delete_marker.nil?
    begin
      # S3 responds with a 405 (method not allowed) when you try
      # to HEAD an s3 object version that is a delete marker
      ['foo']
      @delete_marker = false
    rescue Errors::MethodNotAllowed => error
      @delete_marker = true
    end
  end
  @delete_marker
end

#etagString

Returns the object's ETag

Returns:

  • (String)

    Returns the object's ETag

See Also:



72
73
74
# File 'lib/aws/s3/object_version.rb', line 72

def etag
  head.etag
end

#headObject

Returns A head object response with metadata, content_length, content_type, etag and server_side_encryption.

Returns:

  • A head object response with metadata, content_length, content_type, etag and server_side_encryption.

See Also:



66
67
68
# File 'lib/aws/s3/object_version.rb', line 66

def head
  object.head(:version_id => @version_id)
end

#keyString

Returns The objects unique key

Returns:

  • (String)

    The objects unique key



55
56
57
# File 'lib/aws/s3/object_version.rb', line 55

def key
  object.key
end

#latest?Boolean

Returns this if this is the latest version of the object, false if the object has been written to since this version was created.

Returns:

  • (Boolean)

    Returns this if this is the latest version of the object, false if the object has been written to since this version was created.



116
117
118
# File 'lib/aws/s3/object_version.rb', line 116

def latest?
  object.versions.latest.version_id == self.version_id
end

#metadataObjectMetadata

Returns an instance of ObjectMetadata representing the metadata for this object.

Returns:

  • (ObjectMetadata)

    Returns an instance of ObjectMetadata representing the metadata for this object.

See Also:



90
91
92
# File 'lib/aws/s3/object_version.rb', line 90

def 
  object.(:version_id => @version_id)
end

#read(options = {}, &block) ⇒ Object

Reads the data from this object version.

See Also:



98
99
100
# File 'lib/aws/s3/object_version.rb', line 98

def read options = {}, &block
  object.read(options.merge(:version_id => @version_id), &block)
end

#url_for(method, options = {}) ⇒ URI::HTTP, URI::HTTPS

Generates a presigned URL for an operation on this object. This URL can be used by a regular HTTP client to perform the desired operation without credentials and without changing the permissions of the object.

Examples:

Generate a url to read an object


bucket.objects.myobject.url_for(:read)

Generate a url to delete an object


bucket.objects.myobject.url_for(:delete)

Override response headers for reading an object


object = bucket.objects.myobject
url = object.url_for(:read,
                     :response_content_type => "application/json")

Generate a url that expires in 10 minutes


bucket.objects.myobject.url_for(:read, :expires => 10*60)

Parameters:

  • method (Symbol, String)

    The HTTP verb or object method for which the returned URL will be valid. Valid values:

    • :get or :read
    • :put or :write
    • :delete
    • :head
  • options (Hash) (defaults to: {})

    Additional options for generating the URL.

Options Hash (options):

  • :expires (Object)

    Sets the expiration time of the URL; after this time S3 will return an error if the URL is used. This can be an integer (to specify the number of seconds after the current time), a string (which is parsed as a date using Time#parse), a Time, or a DateTime object. This option defaults to one hour after the current time.

  • :secure (Boolean) — default: true

    Whether to generate a secure (HTTPS) URL or a plain HTTP url.

  • :content_type (String)

    Object content type for HTTP PUT. When provided, has to be also added to the request header as a 'content-type' field

  • :content_md5 (String)

    Object MD5 hash for HTTP PUT. When provided, has to be also added to the request header as a 'content-md5' field

  • :endpoint (String)

    Sets the hostname of the endpoint.

  • :port (Integer)

    Sets the port of the endpoint (overrides config.s3_port).

  • :force_path_style (Boolean) — default: false

    Indicates whether the generated URL should place the bucket name in the path (true) or as a subdomain (false).

  • :response_content_type (String)

    Sets the Content-Type header of the response when performing an HTTP GET on the returned URL.

  • :response_content_language (String)

    Sets the Content-Language header of the response when performing an HTTP GET on the returned URL.

  • :response_expires (String)

    Sets the Expires header of the response when performing an HTTP GET on the returned URL.

  • :response_cache_control (String)

    Sets the Cache-Control header of the response when performing an HTTP GET on the returned URL.

  • :response_content_disposition (String)

    Sets the Content-Disposition header of the response when performing an HTTP GET on the returned URL.

  • :acl (String)

    The value to use for the x-amz-acl.

  • :response_content_encoding (String)

    Sets the Content-Encoding header of the response when performing an HTTP GET on the returned URL.

  • :signature_version (:v3, :v4) — default: :v3

Returns:

  • (URI::HTTP, URI::HTTPS)


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

def url_for method, options = {}
  object.url_for(method, options.merge(:version_id => version_id))
end