このトピックでは、オブジェクトメタの管理方法について説明します。 詳しくは、「OSS 開発者ガイド」の「オブジェクトメタ」をご参照ください。

HTTP ヘッダーの設定

OSS はユーザー定義の HTTP ヘッダーをサポートしています。 HTTP ヘッダーを設定するには、以下のコードを実行します。

# -*- coding: utf-8 -*-
import oss2

# It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

bucket.put_object('<yourObjectName>', '{"age": 1}', headers={'Content-Type': 'application/json; charset=utf-8'})
			

HTTP ヘッダーの詳細については、 「RFC2616」をご参照ください。

ユーザー定義のオブジェクトメタの構成

オブジェクト記述にはユーザー定義のオブジェクトメタを定義できます。 ユーザー定義のオブジェクトメタを設定するには、以下のコードを実行します。

# -*- coding: utf-8 -*-

import oss2

# It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

bucket.put_object('', 'a novel', headers={'x-oss-meta-author': 'O. Henry', 'Content-Type': 'application/json; charset=utf-8'})
Parameters with the x-oss-meta- prefix are used as the headers of user-defined Object Meta. 詳しくは、「PutObject」をご参照ください。

オブジェクトメタの変更

オブジェクトメタを変更するには、以下のコードを実行します。

# -*- coding: utf-8 -*-
import oss2

# It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operation and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

bucket.update_object_meta('<yourObjectName>', {'x-oss-meta-author': 'O. Henry'})
# User-defined Object Meta is cleared each time the bucket.update_object_meta is called and must be configured again.
bucket.update_object_meta('<yourObjectName>', {'x-oss-meta-price': '100 dollar'})
			

Run the following code to change Object Meta, such as Content-Type:

bucket.update_object_meta('<yourObjectName>', {'x-oss-meta-author': 'O. Henry'})
# User-defined Object Meta is cleared each time the bucket.update_object_meta is called and must be configured again.
bucket.update_object_meta('<yourObjectName>', {'Content-Type': 'text/plain'})
			

オブジェクトメタの取得

OSS Python SDK が提供する API を使用してオブジェクトメタを取得します。

方法 説明 利点
get_object_meta オブジェクトの ETag、Size(オブジェクトサイズ)、および LastModified(オブジェクトが最後に変更された時刻)を取得します。 簡単でスピーディー
head_object オブジェクトメタの取得 なし

オブジェクトメタを取得するには、以下のコードを実行します。

# -*- coding: utf-8 -*-

import oss2

# We recommend that you do not log on with the AccessKey of an Alibaba Cloud account because the Alibaba Cloud account has permissions on all APIs in OSS. Instead, we recommend that you log on as an authorized RAM user to access APIs or perform routine operation and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the endpoint China East 1 (Hangzhou). In actual scenarios, specify your required endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Obtain part of Object Meta.
simplifiedmeta = bucket.get_object_meta("<yourObjectName>")
print(simplifiedmeta.headers['Last-Modified']) 
print(simplifiedmeta.headers['Content-Length']) 
print(simplifiedmeta.headers['ETag']) 

# Obtain all Object Meta.
objectmeta = bucket.head_object("<yourObjectName>")
print(objectmeta.headers['Content-Type']) 
print(objectmeta.headers['Last-Modified']) 
print(objectmeta.headers['x-oss-object-type'])