All Products
Search
Document Center

Object Storage Service:Download an object to memory (C# SDK V2)

Last Updated:Aug 06, 2025

This topic describes how to use the simple download method to download an object from a bucket to local memory. This method is ideal for quickly downloading objects.

Important

The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, you must use an internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see OSS regions and endpoints.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.

API

Action

Definition

GetObject

oss:GetObject

Downloads an object.

oss:GetObjectVersion

When downloading an object, if you specify the object version through versionId, this permission is required.

kms:Decrypt

When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required.

Sample code

The following sample code shows how to download an object from a bucket to local memory.

using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
var bucket = "your bucket name";  // Required. Specify the name of the destination bucket. 
var endpoint = null as string;  // Optional. Specify the domain name used to access OSS. Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
var key = "your object key"; // Required. Specify the name of the object to be downloaded. Format: folder/objectName.

// Load the default configurations of OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket for the configuration.
cfg.Region = region;   
// If an endpoint is specified, overwrite the default endpoint. 
if(endpoint != null)
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg); 

// Call the GetObjectAsync method. By default, the object is obtained in streaming mode. In this case, only the response header is downloaded, and the content is returned as a stream.
var result = await client.GetObjectAsync(new OSS.Models.GetObjectRequest()
{
    Bucket = bucket,
    Key = key,
});

// To read all the content into the memory at a time, you can use this reload method.
//var result = await client.GetObjectAsync(new OSS.Models.GetObjectRequest() {
//    Bucket = bucket,
//    Key = key,
//},System.Net.Http.HttpCompletionOption.ResponseContentRead);

// Obtain the content stream of the response.
using var body = result.Body;
// Read data.
var reader = new StreamReader(body!);
// Read all content from the stream as a string (the content is loaded into the memory).
var data = reader.ReadToEnd();

// Print the result information. 
Console.WriteLine("GetObject done");  // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // RequestId, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // Response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.

References

For the complete sample code that demonstrates how to download an object to local memory using the simple download method, see GetObject.cs.