All Products
Search
Document Center

Object Storage Service:Download files using C#

Last Updated:Mar 20, 2026

Use OSS SDK for C# V2 to download objects from a versioning-enabled bucket to a local file.

Prerequisites

Before you begin, ensure that you have:

  • A versioning-enabled OSS bucket

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid credentials

  • The required permissions (see Permissions)

Usage notes

The sample code uses cn-hangzhou as the region ID and connects via the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a list of endpoints by region, see OSS regions and endpoints.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default — grant the required actions via RAM policy or bucket policy.

ActionRequired when
oss:GetObjectAlways
oss:GetObjectVersionDownloading a specific version by versionId
kms:DecryptObject metadata contains X-Oss-Server-Side-Encryption: KMS

Versioning behavior

When you call GetObject on a versioning-enabled bucket:

  • If the current version is a delete marker, OSS returns 404 Not Found.

  • If you specify a version ID, OSS returns that version. If the version ID is null, OSS returns the version whose ID is null.

  • If the specified version ID belongs to a delete marker, OSS returns 405 Method Not Allowed.

Download an object to a file

GetObjectToFileAsync streams the object directly to disk, avoiding loading the entire object into memory.

using OSS = AlibabaCloud.OSS.V2;  // Alias for OSS SDK namespace

var region = "cn-hangzhou";           // Required. Region ID of the bucket
var bucket = "your bucket name";      // Required. Bucket name
var endpoint = null as string;        // Optional. Custom endpoint (e.g., https://oss-cn-hangzhou.aliyuncs.com)
var key = "your object name";         // Required. Object key, e.g., folder/objectName
var filePath = "your file path";      // Required. Local path to save the downloaded file
var versionId = "your object versionId";  // Required. Version ID of the object to download

// Load default SDK configuration and read credentials from environment variables
// (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET)
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;

// Override the default endpoint if a custom one is specified
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

// Create an OSS client
using var client = new OSS.Client(cfg);

// Stream the object directly to the local file path
var result = await client.GetObjectToFileAsync(new OSS.Models.GetObjectRequest()
{
    Bucket = bucket,
    Key = key,
    VersionId = versionId,
}, filePath);

// Print the result
Console.WriteLine("GetObjectToFile done");
Console.WriteLine($"StatusCode: {result.StatusCode}");    // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}");      // Use for troubleshooting with Alibaba Cloud support
Console.WriteLine("Response Headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));