For more information about Object Meta, see Object Meta in OSS Developer Guide. For the complete code of Object Meta, see GitHub.

Configure Object Meta

You can configure the Object Meta when uploading an objct. The following table lists the Object Meta you can configure:

Parameter Description
CacheControl Speicifies the cache action of a webpage when an object is downloaded.
ContentDisposition // Specify the name of the object when it is downloaded.
ContentEncoding Specifies the encoding format when an object is downloaded.
Expires Specifies the cache expiration time (GMT is used).
ServerSideEncryption Specifies the server-side encryption algorithm when the OSS creates an object. Valid value: AES256.
ObjectACL Specifies the ACL for an object created by OSS.
Meta Speicifies the custom Object Meta, with X-Oss-Meta- as the prefix.

Run the following code to configure Object Meta:

package main

import (
	"fmt"
	"os"
	"time"
	"strings"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Obtain the bucket.
	bucket, err := client.Bucket("<yourBucketName>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Configure Object Meta: The expiration time is 2049-01-10, 23:00:00 in the GMT format. The ACL is public read. The custom Object Meta is MyProp (the value is MyPropVal).
	expires := time.Date(2049, time.January, 10, 23, 0, 0, 0, time.UTC)
	options := []oss.Option{
		oss.Expires(expires),
		oss.ObjectACL(oss.ACLPublicRead),
		oss.Meta("MyProp", "MyPropVal"),
	}

	// Use the data stream to upload.
	err = bucket.PutObject("<yourObjectName>", strings.NewReader("MyObjectValue"), options...)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Obtain Object Meta.
	props, err := bucket.GetObjectDetailedMeta("<yourObjectName>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Object Meta:", props)
}

Modify Object Meta

Run the following code to modify one or more Object Meta at a time:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"
	objectName := "<yourObjectName>"

	// Obtain the bucket.
	bucket, err := client.Bucket(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Modify one Object Meta at a time.
	err = bucket.SetObjectMeta(objectName, oss.Meta("MyMeta", "MyMetaValue1"))
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Modify multiple Object Metas at one time.
	options := []oss.Option{
		oss.Meta("MyMeta", "MyMetaValue2"),
		oss.Meta("MyObjectLocation", "HangZhou"),
	}
	err = bucket.SetObjectMeta(objectName, options...)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Obtain Object Meta.
	props, err := bucket.GetObjectDetailedMeta(objectName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Object Meta:", props)
}

Obtain Object Meta

Run the following code to obtain Object Meta:

package main

import (
	"fmt"
	"os"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Create an OSSClient instance.
	client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	bucketName := "<yourBucketName>"
	objectName := "<yourObjectName>"

	// Obtain the bucket.
	bucket, err := client.Bucket(bucketName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Obtain Object Meta.
	props, err := bucket.GetObjectDetailedMeta(objectName)
	if err ! = nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("Object Meta:", props)
}