edit-icon download-icon

Download objects

Last Updated: Nov 07, 2017

OSS PHP SDK provides a variety of object downloading interfaces. You can download an object from OSS by using any of the following methods:

  • Download an object to a local file
  • Download an object to the memory
  • Multipart download
  • Download with specified conditions

Note: For complete code, see GitHub.

Download an object to a local file

The following code downloads an OSS object to a local file:

  1. <?php
  2. /**
  3. * get_object_to_local_file
  4. *
  5. * Get the object
  6. * Download the object to a specified file
  7. *
  8. * @param OssClient $ossClient OSSClient instance
  9. * @param string $bucket Bucket name
  10. * @return null
  11. */
  12. function getObjectToLocalFile($ossClient, $bucket)
  13. {
  14. $object = "oss-php-sdk-test/download-test-object-name.txt";
  15. $localfile = "download-test-object-name.txt";
  16. $options = array(
  17. OssClient::OSS_FILE_DOWNLOAD => $localfile,
  18. );
  19. try{
  20. $ossClient->getObject($bucket, $object, $options);
  21. } catch(OssException $e) {
  22. printf(__FUNCTION__ . ": FAILED\n");
  23. printf($e->getMessage() . "\n");
  24. return;
  25. }
  26. print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");
  27. }

Download an object to the memory

The following code downloads an object to the memory:

  1. <?php
  2. /**
  3. * Get the content of the object
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getObject($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try{
  13. $content = $ossClient->getObject($bucket, $object);
  14. } catch(OssException $e) {
  15. printf(__FUNCTION__ . ": FAILED\n");
  16. printf($e->getMessage() . "\n");
  17. return;
  18. }
  19. print(__FUNCTION__ . ": OK" . "\n");
  20. }

Download within a specified range

If the OSS object is large and only a portion of data is required, you can use the download by range feature to download data within a specified range. If the specified download range is 0 - 100, the data of the 0th to the 100th bytes is returned, including the 100th, that means, it has 101 bytes of total data, [0, 100]. If the specified range is invalid, the whole object is downloaded.

The following code downloads the content of [1, 100] of an object to the memory:

  1. <?php
  2. /**
  3. * Get the content of the object
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getObject($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try{
  13. $options = array(OssClient::OSS_RANGE => '0-100');
  14. $content = $ossClient->getObject($bucket, $object, $options);
  15. } catch(OssException $e) {
  16. printf(__FUNCTION__ . ": FAILED\n");
  17. printf($e->getMessage() . "\n");
  18. return;
  19. }
  20. print(__FUNCTION__ . ": OK" . "\n");
  21. }

Note:

  • The subject for by-range download can also be objects.

  • The download range is a closed interval [start, end], with both ends inclusive.

Download with specified conditions

You can specify one or more conditions for downloading an object. The object is downloaded only when the conditions are met. Otherwise, an error is reported and the object is not downloaded. Available conditions include:

Parameter Description PHP SDK value
If-Modified-Since If the specified time is earlier than the actual modification time, the object is transmitted normally. Otherwise, an error is returned. OssClient::OSS_IF_MODIFIED_SINCE
If-Unmodified-Since If the specified time in the input parameter is the same as or later than the actual modification time of the object, the object is transmitted normally. Otherwise, an error is returned. OssClient::OSS_IF_UNMODIFIED_SINCE
If-Match If the expected ETag that is passed in matches the object’s ETag, the object is transmitted normally. Otherwise, an error is returned. OssClient::OSS_IF_MATCH
If-None-Match If the ETag that is passed in does not match the object’s ETag, the object is transmitted normally. Otherwise, an error is returned. OssClient::OSS_IF_NONE_MATCH

Note:

  • If the time specified by If-Modified-Since does not match the actual modification time, the system directly returns the object and the 200 OK message.

  • If-Modified-Since and If-Unmodified-Since can coexist, and If-Match and If-None-Match can also coexist.

  • If the request contains If-Unmodified-Since and If-Unmodified-Since does not match the actual modification time, or the request contains If-Match and If-Match does not match the Etag of the object, the system returns the 412 Precondition Failed error.

  • If the request contains If-Modified-Since and If-Modified-Since does not match the actual modification time, or the request contains If-None-Match and If-None-Match does not match the Etag of the object, the system returns the 304 Not Modified error.

  1. <?php
  2. /**
  3. * If the object gets modified after the specified time, the object gets downloaded
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getObject($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try{
  13. $options = array(
  14. OssClient::OSS_HEADERS => array(
  15. OssClient::OSS_IF_MODIFIED_SINCE => "Fri, 13 Nov 2015 14:47:53 GMT"),
  16. );
  17. $content = $ossClient->getObject($bucket, $object, $options);
  18. } catch(OssException $e) {
  19. printf(__FUNCTION__ . ": FAILED\n");
  20. printf($e->getMessage() . "\n");
  21. return;
  22. }
  23. print(__FUNCTION__ . ": OK" . "\n");
  24. }

Note:

  • You can get the object ETag value through OssClient::getObjectMeta.

  • The subject for download with specified conditions can also be objects.

Thank you! We've received your feedback.