Download files using C#
This topic describes how to download objects from a versioned bucket by using OSS SDK for C#.
Usage notes
The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information, 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 policies or Bucket Policy.
|
API |
Action |
Description |
|
GetObject |
|
Downloads an object. |
|
|
When downloading an object, if you specify the object version through versionId, this permission is required. |
|
|
|
When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required. |
When you call the GetObject operation to download an object in a bucket, the following three scenarios apply:
-
If the current version of the object is a delete marker, Object Storage Service (OSS) returns 404 Not Found.
-
If the version ID of the object is specified in the request, OSS returns the specified version of the object. If the version ID is set to null in the request, OSS returns the version whose ID is null.
-
If the version ID specified in the request is the version ID of a delete marker, OSS returns 405 Method Not Allowed.
Sample code
Below is the sample code for downloading objects.
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 of the bucket. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
var bucket = "your bucket name"; // Required. Specify the name of the destination bucket.
var endpoint = null as string; // Optional. Specify the endpoint used to access OSS. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var key = "your object name"; // Required. Specify the name of the object to download. Format: folder/objectName.
var filePath = "your file path"; // Required. Specify the path to save the downloaded file to the local disk.
var versionId = "your object versionId"; // Required. Specify the x-oss-version-id of the object to download.
// Load the default configuration of OSS SDK, which automatically reads credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set environment variables to obtain credentials for authentication (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Specify the region in which the bucket is located.
cfg.Region = region;
// Overrides default endpoint if specified.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);
// Call the GetObjectToFileAsync method to directly download the OSS object in streaming mode to the local disk, avoiding excessive memory usage for large files.
var result = await client.GetObjectToFileAsync(new OSS.Models.GetObjectRequest()
{
Bucket = bucket,
Key = key,
VersionId = versionId,
}, filePath);
// Print the upload result.
Console.WriteLine("GetObjectToFile done"); // Indicates that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // RequestId, used for Alibaba Cloud troubleshooting.
Console.WriteLine("Response Headers:"); // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.