All Products
Search
Document Center

Object Storage Service:Conditional downloads (Go SDK V1)

Last Updated:Nov 28, 2025

When you download a file (object) from a bucket, you can specify conditions based on its last modified time or ETag. An ETag is an identifier for the file's content. The file is downloaded only if the specified conditions are met. If the conditions are not met, an error is returned and the download is not triggered. This method reduces network traffic and resource usage, which improves 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 and endpoints.

  • 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 Configure OSSClient instances.

  • To perform conditional download, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Conditions

OSS supports the following conditions:

Note
  • 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

How to set

IfModifiedSince

If the specified time is earlier than the actual modification time of the file, the file is transferred. Otherwise, a 304 Not Modified error is returned.

oss.IfModifiedSince

IfUnmodifiedSince

If the specified time is the same as or later than the actual modification time of the file, the file is transferred. Otherwise, a 412 Precondition Failed error is returned.

oss.IfUnmodifiedSince

IfMatch

If the specified ETag matches the ETag of the file in OSS, the file is transferred. Otherwise, a 412 Precondition Failed error is returned.

oss.IfMatch

IfNoneMatch

If the specified ETag does not match the ETag of the file in OSS, the file is transferred. Otherwise, a 304 Not Modified error is returned.

oss.IfNoneMatch

Sample code

The following code shows how to perform a 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.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Set yourBucketName to the name of the bucket.
	bucket, err := client.Bucket("yourBucketName")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Assume that the object was last modified at 18:43:02 on November 21, 2023. If the specified UTC time is earlier than this time, the IfModifiedSince condition is met and the download is triggered.
	date := time.Date(2023, time.November, 21, 10, 40, 02, 0, time.UTC)

	// The condition is not met. The file is not downloaded.
	// Set yourObjectName to the full path of the object. The full path does not include the bucket name.
	err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfUnmodifiedSince(date))
	if err == nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// The condition is met. The file is downloaded.
	err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfModifiedSince(date))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

References

  • For the complete sample code for conditional downloads, see the GitHub example.

  • For more information about the API operation for conditional downloads, see GetObject.