对象元信息是对象的属性描述,包括 HTTP 标准属性(HTTP Header)和用户自定义元数据(User Meta)两种。
通过 TOS Go SDK 可以添加和修改已经上传对象的元数据信息。TOS 支持携带以下 7 个请求头来修改对象元数据,在下载对象或者 HEAD 对象时,这些值会设置到 HTTP 头域中返回给客户端。请求头如下所示:
名称 | 说明 |
---|---|
Content-Type | 对象类型 |
Cache-Control | 指定该对象被下载时网页的缓存行为 |
Expires | RFC2616中定义的缓存失效时间 |
Content-Disposition | 对象被下载时的名称 |
Content-Encoding | 对象被下载时的内容编码类型 |
Content-Language | 对象被下载时的内容语言格式 |
x-tos-meta-* | 使用 SDK 设置 Meta 字段时设置的元数据信息。当查询此对象时,元数据将会在返回消息的 Header 中 |
package main import ( "context" "fmt" "strings" "time" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" // 将文件上传到 example_dir 目录下的 example.txt 文件 objectKey = "example_dir/example.txt" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 将字符串 “Hello TOS” 上传到指定 example_dir 目录下的 example.txt body := strings.NewReader("Hello TOS") output, err := client.PutObjectV2(ctx, &tos.PutObjectV2Input{ PutObjectBasicInput: tos.PutObjectBasicInput{ Bucket: bucketName, Key: objectKey, // 用户自定义元数据信息 Meta: map[string]string{"key": "value"}, // 设置对象类型 ContentType: "text/html", // 设置对象下载时内容语言格式 ContentLanguage: "zh-cn", // 设置缓存策略 CacheControl: "no-store", // 设置缓存过期时间 Expires: time.Now().UTC().Add(time.Hour), // 设置下载内容下载时的名称 ContentDisposition: "example.png", // 设置下载时编码类型 ContentEncoding: "deflate", }, Content: body, }) checkErr(err) fmt.Println("PutObjectV2 Request ID:", output.RequestID) }
package main import ( "context" "fmt" "strings" "time" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" // 将文件上传到 example_dir 目录下的 example.txt 文件 objectKey = "example_dir/example.txt" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 在创建分片时设置元数据信息 output, err := client.CreateMultipartUploadV2(ctx, &tos.CreateMultipartUploadV2Input{ Bucket: bucketName, Key: objectKey, // 用户自定义元数据信息 Meta: map[string]string{"key": "value"}, // 设置对象类型 ContentType: "text/html", // 设置对象下载时内容语言格式 ContentLanguage: "zh-cn", // 设置缓存策略 CacheControl: "no-store", // 设置缓存过期时间 Expires: time.Now().UTC().Add(time.Hour), // 设置下载内容下载时的名称 ContentDisposition: "example.png", // 设置下载时编码类型 ContentEncoding: "deflate", }) checkErr(err) fmt.Println("CreateMultipartUploadV2 Request ID:", output.RequestID) fmt.Println("CreateMultipartUploadV2 Upload ID:", output.UploadID) // 上传分片数据 part, err := client.UploadPartV2(ctx, &tos.UploadPartV2Input{ UploadPartBasicInput: tos.UploadPartBasicInput{ Bucket: bucketName, Key: objectKey, UploadID: output.UploadID, PartNumber: 1, }, Content: strings.NewReader("your body data"), }) checkErr(err) fmt.Println("UploadPartV2 Request ID:", part.RequestID) // 完成上传 completeMultipartOutput, err := client.CompleteMultipartUploadV2(ctx, &tos.CompleteMultipartUploadV2Input{ Bucket: bucketName, Key: objectKey, UploadID: output.UploadID, Parts: []tos.UploadedPartV2{{ PartNumber: part.PartNumber, ETag: part.ETag, }}, }) checkErr(err) fmt.Println("CompleteMultipartUploadV2 Request ID:", completeMultipartOutput.RequestID) }
package main import ( "context" "fmt" "time" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" // 存储桶中的对象名 objectKey = "example_dir/example.txt" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 设置对象元数据信息 output, err := client.SetObjectMeta(ctx, &tos.SetObjectMetaInput{ Bucket: bucketName, Key: objectKey, // 用户自定义元数据信息 Meta: map[string]string{"key": "value"}, // 设置对象类型 ContentType: "text/html", // 设置对象下载时内容语言格式 ContentLanguage: "zh-cn", // 设置缓存策略 CacheControl: "no-store", // 设置缓存过期时间 Expires: time.Now().UTC().Add(time.Hour), // 设置下载内容下载时的名称 ContentDisposition: "example.png", // 设置下载时编码类型 ContentEncoding: "deflate", }) checkErr(err) fmt.Println("SetObjectMeta Request ID:", output.RequestID) }
注意
获取对象元数据前,您必须具有 tos:GetObject
权限,具体操作请参见 IAM 策略概述。
package main import ( "context" "fmt" "net/http" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else if clientErr, ok := err.(*tos.TosClientError); ok { fmt.Println("Error:", clientErr.Error()) fmt.Println("Client Cause Err:", clientErr.Cause.Error()) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" // 存储桶中的对象名 objectKey = "example_dir/example.txt" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) output, err := client.HeadObjectV2(ctx, &tos.HeadObjectV2Input{ Bucket: bucketName, Key: objectKey, }) if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { // 判断对象是否存在 if serverErr.StatusCode == http.StatusNotFound { fmt.Println("Object not found.") } else { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) panic(err) } } else { panic(err) } } fmt.Println("HeadObjectV2 Request ID:", output.RequestID) // 查看内容语言格式 fmt.Println("HeadObjectV2 Response ContentLanguage:", output.ContentLanguage) // 查看下载时的名称 fmt.Println("HeadObjectV2 Response ContentDisposition:", output.ContentDisposition) // 查看编码类型 fmt.Println("HeadObjectV2 Response ContentEncoding:", output.ContentEncoding) // 查看缓存策略 fmt.Println("HeadObjectV2 Response CacheControl:", output.CacheControl) // 查看对象类型 fmt.Println("HeadObjectV2 Response ContentType:", output.ContentType) // 查看缓存过期时间 fmt.Println("HeadObjectV2 Response Expires:", output.Expires) }
关于设置对象元数据的 API 文档,请参见 SetObjectMeta。