Use the GetObject operation to download an object from a bucket and save its content to a local file.
How it works
By default, GetObject returns the current version of an object. In a versioning-enabled bucket, the response depends on the request:
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 version ID isnull.If the specified version ID belongs to a delete marker, OSS returns
405 Method Not Allowed.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with at least one object to download
The
oss:GetObjectpermission on the target object(Required when specifying a version ID) The
oss:GetObjectVersionpermission(Required when the object metadata contains
X-Oss-Server-Side-Encryption: KMS) Thekms:Decryptpermission on Key Management Service (KMS)The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with your AccessKey ID and AccessKey secret
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant the required permissions using a RAM Policy or bucket policy.
Usage notes
The example uses the public endpoint for the China (Hangzhou) region (
https://oss-cn-hangzhou.aliyuncs.com). To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For a full list of endpoints, see Regions and endpoints.The example creates an
OssClientinstance using an OSS endpoint. To create an instance using a custom domain name or Security Token Service (STS), see Initialization.
Download an object
The following example downloads an object to a local file using the GetObject operation with signature version V4.
using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Set the endpoint for the region where your bucket is located.
// Example: China (Hangzhou) uses https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Load credentials from environment variables.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the full object path, excluding the bucket name.
// Example: exampledir/exampleobject.txt
var objectName = "exampledir/exampleobject.txt";
// Specify the local file path to write the downloaded content.
var downloadFilename = "D:\\localpath\\examplefile.txt";
// Specify the version ID of the object to download.
var versionid = "yourArchiveObjectVersionid";
// Specify the region where the bucket is located.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and set the signature version to V4.
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Build the download request and specify the version ID.
var request = new GetObjectRequest(bucketName, objectName)
{
VersionId = versionid
};
// GetObject returns an OssObject containing the object content as a stream.
var obj = client.GetObject(request);
using (var requestStream = obj.Content)
{
byte[] buf = new byte[1024];
var fs = File.Open(downloadFilename, FileMode.OpenOrCreate);
var len = 0;
// Read the stream in 1,024-byte chunks and write to the local file.
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
}
fs.Close();
}
Console.WriteLine("Get object succeeded, vesionid:{0}", versionid);
}
catch (Exception ex)
{
Console.WriteLine("Get object failed. {0}", ex.Message);
}
}
}
}Replace the placeholder values before running the code:
| Placeholder | Description | Example |
|---|---|---|
examplebucket | The name of your bucket | my-bucket |
exampledir/exampleobject.txt | The full path of the object in the bucket | videos/sample.mp4 |
D:\\localpath\\examplefile.txt | The local file path to save the downloaded object | C:\\downloads\\sample.mp4 |
yourArchiveObjectVersionid | The version ID of the object to download | CAEQMxiBgICAof2D0BYiIDJhMGE3N2M1YTI1NDUy |
cn-hangzhou | The region ID where your bucket is located | ap-southeast-1 |