Download objects

Last Updated: Jun 05, 2017

Simple download of objects

The following code reads an object to a stream:

  1. // Initialize an OSSClient
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. /// <summary>
  4. // Retrieve an object from the specified bucket on OSS
  5. /// </summary>
  6. /// <param name="bucketName">Name of the bucket where the object to be retrieved is stored</param>
  7. /// <param name="key">Name of the object to be retrieved</param>
  8. /// <param name="objectToDownload">Local path to which the object will be downloaded</param>
  9. public void GetObject(string bucketName, string key, string fileToDownload)
  10. {
  11. try
  12. {
  13. var object = client.GetObject(bucketName, key);
  14. using (var requestStream = object.Content)
  15. {
  16. byte[] buf = new byte[1024];
  17. var fs = File.Open(fileToDownload, FileMode.OpenOrCreate);
  18. var len = 0;
  19. while ((len = requestStream.Read(buf, 0, 1024)) != 0)
  20. {
  21. fs.Write(buf, 0, len);
  22. }
  23. fs.Close();
  24. }
  25. Console.WriteLine("Get object succeeded");
  26. }
  27. catch (Exception ex)
  28. {
  29. Console.WriteLine("Get object failed. {0}", ex.Message);
  30. }
  31. }

Note:

  • Complete code can be found at: GitHub
  • OssObject contains various object information, including the object’s bucket, object name, metadata, and an input stream.
  • The input stream can be used to read the object content to another object or the memory. ObjectMeta contains the ETag, HTTP header, and UserMetadata that are defined when the object is uploaded.

Multipart object access

The following code uses GetObjectRequest to access an object through multipart read:

  1. // Initialize an OSSClient
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. public void GetObject(string bucketName, string key, string fileToDownload)
  4. {
  5. try
  6. {
  7. var getObjectRequest = new GetObjectRequest(bucketName, key);
  8. //Retrieve the 20th to 100th characters of the object, including the 20th character and the 100th character
  9. getObjectRequest.SetRange(20, 100);
  10. var object = client.GetObject(getObjectRequest);
  11. // Write the retrieved data to the fielToDownload object
  12. using (var requestStream = object.Content)
  13. {
  14. byte[] buf = new byte[1024];
  15. var fs = File.Open(fileToDownload, FileMode.OpenOrCreate);
  16. var len = 0;
  17. while ((len = requestStream.Read(buf, 0, 1024)) != 0)
  18. {
  19. fs.Write(buf, 0, len);
  20. }
  21. fs.Close();
  22. }
  23. Console.WriteLine("Get object succeeded");
  24. }
  25. catch (Exception ex)
  26. {
  27. Console.WriteLine("Get object failed. {0}", ex.Message);
  28. }
  29. }

Note:

  • Complete code can be found at: GitHub
  • The setRange method of the GetObjectRequest interface specifies the range of the returned object content.
  • The setRange method can be used for multipart download and resumable upload.
  • You can set the following parameters for GetObjectRequest:
Parameter Description
Range Specify the range of object transfer.
ModifiedSinceConstraint If the specified time is earlier than the actual modification time, the object will be transmitted normally. Otherwise, the system will throw the 304 Not Modified exception.
UnmodifiedSinceConstraint If the specified time in the input parameter is the same as or later than the actual modification time of the object, the object will be transmitted normally. Otherwise, the system will throw the 412 precondition failed exception
MatchingETagConstraints Pass in an ETag group. If the expected ETag that is passed in matches the object’s ETag, the object will be transmitted normally. Otherwise, the system will throw the 412 precondition failed exception
NonmatchingEtagConstraints Pass in an ETag group. If the passed ETag does not match the object’s ETag, the object will be transmitted normally. Otherwise, the system will throw the 304 Not Modified exception.
ResponseHeaderOverrides Customize some headers in the OSS response request.

Retrieve the object metadata only

The GetObjectMetadata method can be used to retrieve the ObjectMeta only instead of the body of an object. The code is as follows:

  1. // Initialize an OSSClient
  2. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  3. /// <summary>
  4. /// Retrieve the metadata of the object.
  5. /// </summary>
  6. /// <param name="bucketName">Name of the bucket</param>
  7. /// <param name="key">Name of the object on the OSS</param>
  8. public void GetObjectMetadata(string bucketName, string key)
  9. {
  10. try
  11. {
  12. var metadata = client.GetObjectMetadata(bucketName, key);
  13. Console.WriteLine("Get object meta succeeded");
  14. Console.WriteLine("Content-Type:{0}", metadata.ContentType);
  15. Console.WriteLine("Cache-Control:{0}", metadata.CacheControl);
  16. }
  17. catch (Exception ex)
  18. {
  19. Console.WriteLine("Get object meta failed. {0}", ex.Message);
  20. }
  21. }
Thank you! We've received your feedback.