All Products
Search
Document Center

Object Storage Service:use Ruby to manage object metadata

Last Updated:Jan 18, 2024

Objects that are stored in Object Storage Service (OSS) consist of keys, data, and metadata. Object metadata describes object attributes. Object metadata includes standard HTTP headers and user metadata. You can create custom HTTP request policies such as object cache policies and forced object download policies by configuring standard HTTP headers. You can configure user metadata for an object to identify the purposes or attributes of the object.

Usage notes

  • In OSS SDK for Ruby, the metadata of an object is represented by a Hash. Other keys and values are of the String type.

  • Object metadata is transferred as HTTP headers in requests and responses. According to the HTTP protocol, HTTP headers cannot contain complex characters. Therefore, object metadata can contain only visible ASCII characters. Metadata cannot contain line feeds.

  • The metadata of an object cannot exceed 8 KB in size.

Configure object metadata

The following code provides an example on how to configure object metadata when you upload an object:

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # Obtain access credentials from environment variables. Before you run the code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

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

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

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

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

Modify object metadata

The following code provides an example on how to modify object metadata:

require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
  # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
   # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the name of the bucket. Example: examplebucket. 
bucket = client.get_bucket('examplebucket')
# Modify the object metadata. 
bucket.update_object_metas('my-object', {'year' => '2017'})            

References

  • For more information about the API operation that you can call to configure object metadata during a simple upload, see PutObject.

  • For more information about the API operation that you can call to modify object metadata when you copy an object, see CopyObject