オブジェクトメタには、HTTP ヘッダーとユーザー定義のオブジェクトメタが含まれます。 詳しくは、「OSS 開発者ガイド」の「オブジェクトメタ」をご参照ください。

HTTP ヘッダーの設定

  • HTTP ヘッダーの設定

    HTTP ヘッダーを設定するには、以下のコードを実行します。

    // This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // 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.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    String content = "Hello OSS";
    
    // Create Object Meta. You can configure HTTP headers with Object Meta.
    ObjectMetadata meta = new ObjectMetadata();
    
    String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(content.getBytes()));
    // Start object content verification with MD5 values. After the verification is started, the MD5 value calculated by OSS is compared with that of an object. If the two values are different, an error occurs.
    meta.setContentMD5(md5);
    // Specify the type of content you want to upload. The content type determines the form and format in which your browser reads an object. The object extension is used as the content type if the content type is not specified. If no object extension exists, the default value application/octet-stream is used.
    meta.setContentType("text/plain");
    // Specify the name of the content when you download an object.
    meta.setContentDisposition("attachment; filename=\"DownloadFilename\"");
    // Configure the length for an object. If the actual object length is greater than the configured length, only the configured length will be uploaded. If the configured length is greater than the actual object length, the entire object will be uploaded.
    meta.setContentLength(content.length());
    // Configure the cache action of a webpage when an object is downloaded.
    meta.setCacheControl("Download Action");
    // Configure the expiration time (GMT is used).
    meta.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
    // Configure the encoding format when an object is downloaded.
    meta.setContentEncoding("utf-8");
    // Configure the HTTP header.
    meta.setHeader("<yourHeader>", "<yourHeaderValue>");
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // Upload the object.
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()), meta);
    
    // Close your OSSClient.
    ossClient.shutdown();
    

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

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

    オブジェクト記述にはユーザー定義のオブジェクトメタを定義できます。

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

    // This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // 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.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    String content = "Hello OSS";
    
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // Create Object Meta.
    ObjectMetadata meta = new ObjectMetadata();
    
    // Set property to property-value for user-defined Object Meta. We recommend that you use Base64 encoding method.
    meta.addUserMetadata("property", "property-value");
    
    // Upload the object.
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()), meta);
    
    // Obtain Object Meta.
    ossClient.getObjectMetadata("<yourBucketName>", "<yourObjectName>");
    
    // Close your OSSClient.
    ossClient.shutdown();
    

    オブジェクトがダウンロードされると、そのオブジェクトメタもダウンロードされます。 1つのオブジェクトに複数のオブジェクトメタを含めることができ、合計最大サイズは 8 KB 未満です。

オブジェクトメタの変更

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

// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 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.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String sourceBucketName = "<yourSourceBucketName>";
String sourceObjectName = "<yourSourceObjectName>";
String destinationBucketName = "<yourDestinationBucketName>";
String destinationObjectName = "<yourDestinationObjectName>";

// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);

// Replace the source object with the target object. Call ossClient.copyObject to modify Object Meta.
CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);

ObjectMetadata meta = new ObjectMetadata();
// Specify the type of content you want to upload. The content type determines the form and format that your browser uses to read an object. The object extension is used for the content type if the content type is not specified. If no object extension exists, the default value application/octet-stream is used.
meta.setContentType("text/plain");
// Specify the name of the content when you download an object.
meta.setContentDisposition("Download File Name");
// Configure the cache action of a webpage when an object is downloaded.
meta.setCacheControl("Download Action");
// Configure the expiration time (GMT is used).
meta.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
// Configure the encoding type when the object is downloaded.
meta.setContentEncoding("utf-8");
// Configure the HTTP header.
meta.setHeader("<yourHeader>", "<yourHeaderValue>");
// Set property to property-value for user-defined Object Meta.
meta.addUserMetadata("property", "property-value");
request.setNewObjectMetadata(meta);

//Modify Object Meta.
ossClient.copyObject(request);

// Close your OSSClient.
ossClient.shutdown();

オブジェクトメタの取得

次の表は、オブジェクトメタを取得するために使用できるメソッドの一覧です。

方法 説明 利点
ossClient.getSimplifiedObjectMeta オブジェクトの ETag、サイズ、および LastModified(オブジェクトが最後に変更された時刻)を取得します。 より軽量で高速
ossClient.getObjectMetadata すべてのメタデータを取得します。 なし

次のコードを実行してオブジェクトメタを取得します。

// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all 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.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

// Obtain partial Object Meta.
SimplifiedObjectMeta objectMeta = ossClient.getSimplifiedObjectMeta("<yourBucketName>", "<yourObjectName>");
System.out.println(objectMeta.getSize());
System.out.println(objectMeta.getETag());
System.out.println(objectMeta.getLastModified());

// Obtain all Object Meta.
ObjectMetadata metadata = ossClient.getObjectMetadata("<yourBucketName>", "<yourObjectName>");
System.out.println(metadata.getContentType());
System.out.println(metadata.getLastModified());
System.out.println(metadata.getExpirationTime());

// Close your OSSClient.
ossClient.shutdown();