Objects stored in Object Storage Service (OSS) contain a key, data, and object metadata. Object metadata describes the properties of an object and includes standard HTTP headers and user-defined metadata. You can set standard HTTP headers to customize HTTP request policies, such as the object cache policy or forced download policy. You can also set user-defined metadata to identify the purpose or other properties of an object.
Important
In the Ruby software development kit (SDK), object metadata is represented by a
Hash. The keys and values are of theStringtype.Object metadata is included in the HTTP headers. The HTTP protocol specifies that request headers cannot contain complex characters. Metadata can contain only simple, visible ASCII characters and cannot contain line breaks.
The total size of all metadata cannot exceed 8 KB.
Set object metadata
You can use the following code to set object metadata when you upload an object.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is 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 this 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 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
You can use the following code to modify object metadata.
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is 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 this 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 bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# Modify the object metadata.
bucket.update_object_metas('my-object', {'year' => '2017'}) References
For more information about setting object metadata during a simple upload, see the PutObject API operation.
For more information about modifying object metadata when you copy an object, see the CopyObject API operation.