This document provides an overview of APIs and SDK sample codes related to object download.
API | Operation | Description |
---|---|---|
GET Object | Downloading an object | Downloads an object to the local file system |
For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.
// Initialize TransferConfig
TransferConfig transferConfig = new TransferConfig();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
String bucket = "examplebucket-1250000000"; // Bucket name in the format: BucketName-APPID
String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e. the object key
string localDir = System.IO.Path.GetTempPath();// Local file directory
string localFileName = "my-local-temp-file"; // Specify the name of the file to be saved locally
// Download an object
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
localDir, localFileName);
downloadTask.progressCallback = delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
};
try {
COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = await
transferManager.DownloadAsync(downloadTask);
Console.WriteLine(result.GetResultInfo());
string eTag = result.eTag;
} catch (Exception e) {
Console.WriteLine("CosException: " + e);
}
Note:
For the complete sample, go to GitHub.
TransferConfig transferConfig = new TransferConfig();
// Initialize TransferManager
TransferManager transferManager = new TransferManager(cosXml, transferConfig);
string bucket = "examplebucket-1250000000"; // Bucket name in the format: BucketName-APPID
string localDir = System.IO.Path.GetTempPath();// Local file directory
for (int i = 0; i < 5; i++) {
// Download a set of objects
string cosPath = "exampleobject" + i; // The location identifier of an object in the bucket, i.e. the object key
string localFileName = "my-local-temp-file"; // Specify the name of the file to be saved locally
COSXMLDownloadTask downloadTask = new COSXMLDownloadTask(bucket, cosPath,
localDir, localFileName);
await transferManager.DownloadAsync(downloadTask);
}
Note:
For the complete sample, go to GitHub.
This API is used to download an object to the local file system.
try
{
string bucket = "examplebucket-1250000000"; // Bucket name in the format: BucketName-APPID
string key = "exampleobject"; // Object key
string localDir = System.IO.Path.GetTempPath();// Local file directory
string localFileName = "my-local-temp-file"; // Specify the name of the file to be saved locally
GetObjectRequest request = new GetObjectRequest(bucket, key, localDir, localFileName);
// Set progress callback
request.SetCosProgressCallback(delegate (long completed, long total)
{
Console.WriteLine(String.Format("progress = {0:##.##}%", completed * 100.0 / total));
});
// Execute the request
GetObjectResult result = cosXml.GetObject(request);
// Request succeeded
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
// Request failed
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
// Request failed
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
Note:
For the complete sample, go to GitHub.
Was this page helpful?