若您想在下载对象时,设置下载对象的限制条件(对象 ETag 或对象修改时间),您可使用TOS Go SDK提供的限定条件下载功能,当对象信息满足您指定条件时才下载对象。本文介绍如何通过限定条件下载对象。
tos:GetObject
权限,具体操作,请参见权限配置指南。tos:GetObjectVersion
权限,具体操作,请参见权限配置指南。下载对象时可以指定下载对象条件,当满足条件时进行下载对象。限定条件如下:
参数 | 描述 |
---|---|
If-Match | 只有当传入的 |
If-None-Match | 只有当传入的 |
If-Modified-Since | 只有在对象自指定时间以来被修改过时才返回该对象,否则返回304 Not Modified。 |
If-Unmodified-Since | 只有在对象自指定时间以来未被修改过时才返回该对象,否则返回412 Precondition Failed。 |
package main import ( "context" "fmt" "io/ioutil" "net/http" "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) // 指定日期作为下载条件 date := time.Date(2022, time.October, 24, 0, 0, 0, 0, time.UTC) getoutput, err := client.GetObjectV2(ctx, &tos.GetObjectV2Input{ Bucket: bucketName, Key: objectKey, IfModifiedSince: date, }) if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { // IfModifiedSince 不满足时,返回 304 if serverErr.StatusCode == http.StatusNotModified { fmt.Println("Object not modify, Request ID:", serverErr.RequestID) } 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) } defer getoutput.Content.Close() // 满足限定条件时,读取对象 body, err := ioutil.ReadAll(getoutput.Content) checkErr(err) fmt.Println("GetObject Content:", body) getoutput, err = client.GetObjectV2(ctx, &tos.GetObjectV2Input{ Bucket: bucketName, Key: objectKey, IfUnmodifiedSince: date, }) if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { // IfUnmodifiedSince 不满足时,返回 412 if serverErr.StatusCode == http.StatusPreconditionFailed { fmt.Println("Object modify, Request ID:", serverErr.RequestID) } 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) } defer getoutput.Content.Close() // 满足限定条件时,读取对象 body, err = ioutil.ReadAll(getoutput.Content) checkErr(err) fmt.Println("GetObject Content:", body) }
关于下载对象的 API 文档,请参见 GetObject。