Downloading Objects

Last updated: 2020-09-17 15:48:36

    Overview

    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

    SDK API Reference

    For the parameters and method descriptions of all the APIs in the SDK, see Api Documentation.

    Advanced APIs (recommended)

    Downloading an object

    Sample 1. Downloading an object

    // 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));
    };
    downloadTask.successCallback = delegate (CosResult cosResult) 
    {
        COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult result = cosResult 
          as COSXML.Transfer.COSXMLDownloadTask.DownloadTaskResult;
        Console.WriteLine(result.GetResultInfo());
        string eTag = result.eTag;
    };
    downloadTask.failCallback = delegate (CosClientException clientEx, CosServerException serverEx) 
    {
        if (clientEx != null)
        {
            Console.WriteLine("CosClientException: " + clientEx);
        }
        if (serverEx != null)
        {
            Console.WriteLine("CosServerException: " + serverEx.GetInfo());
        }
    };
    transferManager.Download(downloadTask);

    Note:

    For the complete sample, go to GitHub.

    Sample 2. Downloading multiple objects

    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);
      transferManager.Download(downloadTask);
    }

    Note:

    For the complete sample, go to GitHub.

    Simple Operations

    Downloading an object

    API description

    This API is used to download an object to the local file system.

    Sample code

    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?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help