When you download a single object from a bucket, you can specify download conditions based on the last modified time or the ETag (the identifier of the object content) of the object. If the specified download conditions are met, the object is downloaded. If the specified download conditions are not met, an error is returned and the object is not downloaded. You can use conditional download to reduce network transmission and resource consumption and improve download efficiency.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To perform conditional download, you must have the
oss:GetObject
permission. For more information, see Attach a custom policy to a RAM user.
Conditions
The following table describes the available object download conditions.
Both If-Modified-Since and If-Unmodified-Since can exist at the same time as object download conditions. Both If-Match and If-None-Match can exist at the same time as object download conditions.
You can obtain the ETag by using ossClient.getObjectMeta.
Parameter | Description | Configuration method |
If-Modified-Since | If the specified time is earlier than the time when an object was last modified, the object can be downloaded. Otherwise, 304 Not Modified is returned. | oss.IfModifiedSince |
If-Unmodified-Since | If the specified time is later than or equal to the time when an object was last modified, the object can be downloaded. Otherwise, 412 Precondition Failed is returned. | oss.IfUnmodifiedSince |
If-Match | If the specified ETag matches that of an object, the object can be downloaded. Otherwise, 412 Precondition Failed is returned. | oss.IfMatch |
If-None-Match | If the specified ETag does not match that of an object, the object can be downloaded. Otherwise, 304 Not Modified is returned. | oss.IfNoneMatch |
Examples
The following sample code provides an example on how to perform conditional download:
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)
}
}