Bir paketten tek bir nesne indirirken, nesnenin son değiştirilme zamanına veya ETag (nesne içeriğinin tanımlayıcısı) değerine göre indirme koşulları belirtebilirsiniz. Belirtilen indirme koşulları karşılanırsa nesne indirilir. Koşullar karşılanmazsa sistem bir hata döndürür ve nesneyi indirmez. Koşullu indirme kullanarak ağ trafiğini ve kaynak tüketimini azaltabilir, indirme verimliliğini artırabilirsiniz.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı göstermektedir. Özel alan adı kullanımı veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırma (Go SDK V1).
Koşullu indirme işlemi için
oss:GetObjectizni gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Koşullar
Aşağıdaki tabloda kullanılabilir nesne indirme koşulları açıklanmaktadır.
If-Modified-Since ve If-Unmodified-Since birlikte kullanılabilir. If-Match ve If-None-Match de birlikte kullanılabilir.
ETag değerini almak için ossClient.getObjectMetadata yöntemini çağırın.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Örnekler
Aşağıdaki örnek kod, koşullu indirme işleminin nasıl yapılacağını göstermektedir:
package main
import (
"fmt"
"os"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the name of your bucket.
bucket, err := client.Bucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// For example, an object was last modified at 18:43:02, on November 21, 2023. If the specified time is earlier than the last modified time, the object meets the If-Modified-Since condition and the object is downloaded.
date := time.Date(2023, time.November, 21, 10, 40, 02, 0, time.UTC)
// The object is not downloaded if it does not meet the specified conditions.
// Specify the full path of the object. Do not include the bucket name in the full path.
err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfUnmodifiedSince(date))
if err == nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// The object is downloaded if it meets the specified conditions.
err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfModifiedSince(date))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}