All Products
Search
Document Center

Object Storage Service:Manage object metadata (Ruby SDK)

Last Updated:Mar 20, 2026

Each object in Object Storage Service (OSS) consists of a key, data, and metadata. Object metadata describes the object's properties and comes in two forms:

  • Standard HTTP headers: Control HTTP request behavior, such as caching and download handling.

  • User-defined metadata: Store custom key-value pairs to describe the object's purpose or attributes.

In the Ruby SDK, object metadata is represented as a Hash where both keys and values are String types.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account with OSS access

  • The OSS Ruby SDK installed

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables configured

Set metadata during upload

All three upload methods accept a :metas parameter as a Hash of string key-value pairs.

Metadata is transmitted in HTTP headers, so values must contain only simple, visible ASCII characters and no line breaks. The total size of all metadata for an object cannot exceed 8 KB.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region where the bucket is located.
  # Example: if the bucket is in the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')

# Set object metadata during a simple upload.
bucket.put_object(
  'my-object-1',
  :file => 'local-file',
  :metas => {'year' => '2016', 'people' => 'mary'})

# Set object metadata during an append upload.
bucket.append_object(
  'my-object-2', 0,
  :file => 'local-file',
  :metas => {'year' => '2016', 'people' => 'mary'})

# Set object metadata during a resumable upload.
bucket.resumable_upload(
  'my-object',
  'local-file',
  :metas => {'year' => '2016', 'people' => 'mary'})

Modify object metadata

Use update_object_metas to modify an object's metadata.

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region where the bucket is located.
  # Example: if the bucket is in the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')

# Modify the object metadata.
bucket.update_object_metas('my-object', {'year' => '2017'})

What's next

  • For details on the underlying API used during simple upload, see PutObject.

  • To modify metadata by copying an object, see CopyObject.