edit-icon download-icon

Manage objects

Last Updated: May 04, 2018

In OSS, you can manage the objects in a bucket using a series of interfaces, such as ListObjects, DeleteObject, CopyObject, and DoesObjectExist.

Note: For complete code, see GitHub.

List objects in a bucket

You can list objects in the bucket using OssClient::listObjects:

  1. <?php
  2. /**
  3. * List all directories and objects in a bucket. The listObjects interface is called recursive call according to the returned nextMarker until all objects and directories are listed.
  4. *
  5. * @param OssClient $ossClient OssClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function listAllObjects($ossClient, $bucket)
  10. {
  11. //Construct objects and virtual directories in the dir directory
  12. for ($i = 0; $i < 100; $i += 1) {
  13. $ossClient->putObject($bucket, "dir/obj" . strval($i), "hi");
  14. $ossClient->createObjectDir($bucket, "dir/obj" . strval($i));
  15. }
  16. $prefix = 'dir/';
  17. $delimiter = '/';
  18. $nextMarker = '';
  19. $maxkeys = 30;
  20. while (true) {
  21. $options = array(
  22. 'delimiter' => $delimiter,
  23. 'prefix' => $prefix,
  24. 'max-keys' => $maxkeys,
  25. 'marker' => $nextMarker,
  26. );
  27. var_dump($options);
  28. try {
  29. $listObjectInfo = $ossClient->listObjects($bucket, $options);
  30. } catch (OssException $e) {
  31. printf(__FUNCTION__ . ": FAILED\n");
  32. printf($e->getMessage() . "\n");
  33. return;
  34. }
  35. // Obtain nextMarker. The remaining portion of the object list is always retrieved starting with the object next to the last object previously retrieved through the listObjects interface.
  36. $nextMarker = $listObjectInfo->getNextMarker();
  37. $listObject = $listObjectInfo->getObjectList();
  38. $listPrefix = $listObjectInfo->getPrefixList();
  39. var_dump(count($listObject));
  40. var_dump(count($listPrefix));
  41. if ($nextMarker === '') {
  42. break;
  43. }
  44. }
  45. }

The parameters of $options in the preceding code are described as follows:

Key Description
Delimiter A character used to group object names. All the objects whose names contain the specified prefix and appear before the delimiter for the first time form a group of elements called CommonPrefixes.
Prefix The keys of returned objects must use the prefix. Note that when you make a query using the prefix, the returned key still contains the prefix.
max-keys Specify the maximum number of objects returned for one request. If this parameter is not specified, the default value 100 applies. The max-keys value cannot exceed 1,000.
Marker Set the returned results to begin from the first entry after the Marker in the alphabetical order.

Note: The prefix, delimiter, marker, and max-keys are all optional parameters.

Identify whether an object exists

You can identify whether an object exists using OssClient::doesObjectExist:

  1. <?php
  2. /**
  3. * Identify whether an object exists
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function doesObjectExist($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try{
  13. $exist = $ossClient->doesObjectExist($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. var_dump($exist);
  21. }

Create a virtual directory

OSS does not use folders. All elements are stored as objects. So, if you create a folder, an object with a size of 0 gets created instead. This object can be uploaded and downloaded, but the OSS console displays the object ended with a slash as a folder.

  1. <?php
  2. /**
  3. * Create a virtual directory
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function createObjectDir($ossClient, $bucket) {
  10. try{
  11. $ossClient->createObjectDir($bucket, "dir");
  12. } catch(OssException $e) {
  13. printf(__FUNCTION__ . ": FAILED\n");
  14. printf($e->getMessage() . "\n");
  15. return;
  16. }
  17. print(__FUNCTION__ . ": OK" . "\n");
  18. }

Delete a single object

You can use OssClient::deleteObjectto delete a single object:

  1. <?php
  2. /**
  3. * Delete an object
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function deleteObject($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try{
  13. $ossClient->deleteObject($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. }

Note: The object cannot be restored after deletion.

Delete multiple objects

You can use OssClient::deleteObjects to delete multiple objects:

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

Copy an object

You can use OssClient::copyObject to copy objects:

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

Note: You can copy an object smaller than 1 GB using OssClient::copyObject. When you copy an object larger than 1 GB, use OssClient::uploadPartCopy.

Modify object meta

Object Meta describes the attributes of objects uploaded on OSS. The attributes are of two types: HTTP standard attributes (HTTP Headers) and user-defined metainformation (User Meta). Object metadata can be configured when objects are uploaded or copied in various ways.

HTTP standard attributes, Cache-Control, Content-Disposition, Content-Encoding, Content-Language, Expires, Content-Length, Content-Type and Last-Modified.

This attribute is designed for you to enrich the descriptions of the objects. In OSS, all parameters prefixed with “x-oss-meta-“ are considered as User Meta, such as x-oss-meta-location. A single object can have multiple similar parameters, but the total size of all User Meta cannot exceed 8 KB. The User Meta information is returned when you download the object/get the object metadata.

For more information about object metadata, see Object Meta.

You can use the COPY operation to modify the metadata of an existing object. If the source object address and target object address of the COPY operation are the same, the source object metadata gets replaced directly.

  1. /**
  2. * Modify the object metadata
  3. * If the target object is identical with the source object, the object metadata is modified. This is a feature of the copyObject interface.
  4. *
  5. * @param OssClient $ossClient OSSClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function modifyMetaForObject($ossClient, $bucket)
  10. {
  11. $fromBucket = $bucket;
  12. $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";
  13. $toBucket = $bucket;
  14. $toObject = $fromObject;
  15. $copyOptions = array(
  16. OssClient::OSS_HEADERS => array(
  17. 'Expires' => '2018-10-01 08:00:00',
  18. 'Content-Disposition' => 'attachment; filename="xxxxxx"',
  19. 'x-oss-meta-location' => 'location',
  20. ),
  21. );
  22. try{
  23. $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);
  24. } catch(OssException $e) {
  25. printf(__FUNCTION__ . ": FAILED\n");
  26. printf($e->getMessage() . "\n");
  27. return;
  28. }
  29. print(__FUNCTION__ . ": OK" . "\n");
  30. }

Note:

  • You can modify the object type by modifying the object Content-Type.

  • You can control object download behaviors by modifying the object Content-Disposition.

Get the object meta information (Object Meta)

You can get the object meta information using OssClient::getObjectMeta:

  1. <?php
  2. /**
  3. * Get the object meta through the getObjectMeta interface
  4. *
  5. * @param OssClient $ossClient OssClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getObjectMeta($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try {
  13. $objectMeta = $ossClient->getObjectMeta($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. if (isset($objectMeta[strtolower('Content-Disposition')]) &&
  21. 'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]
  22. ) {
  23. print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");
  24. } else {
  25. print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");
  26. }
  27. }

Set object access permissions

The object has four access ACLs: default (default value), private (private-read-write), public-read (public-read and private-write), and public-read-write (public-read-write), which are described as follows:

Permission Description PHP SDK value
Default The object inherits the read-write permissions of the bucket. The object adopts the default ACL by default. default
Private The object is a private resource. Only the owner of the object has permission to read and write the object. Other users have no permission to operate the object. private
PublicRead The object can be read by the public. Only the owner of the object has permission to read and write the object. Other users only have permission to read the object. public-read
PublicReadWrite The object can be read and written by the public. That is, all users have permission to read and write the object. public-read-write

The object ACL takes precedence over the bucket ACL. For example, if the bucket ACL is private and the object ACL is public-read-write, the system first checks the ACL of the object when a user accesses the object. As a result, all users can access this object even if the bucket is private.

If the ACL of an object has never been set, the ACL of this object is the same as that of the bucket where the object is located.

You can set the object access permission using OssClient::putObjectAcl:

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

Get the object access permission

You can get the object access permission using OssClient::getObjectAcl:

  1. <?php
  2. /**
  3. * Get the object access permission
  4. *
  5. * @param OssClient $ossClient OssClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getObjectMeta($ossClient, $bucket)
  10. {
  11. $object = "oss-php-sdk-test/upload-test-object-name.txt";
  12. try {
  13. $objectAcl = $ossClient->getObjectAcl($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. var_dump($objectAcl);
  21. }

Restore objects of the Archive storage class

The status of archived objects changes as follows:

  1. Objects of the Archive storage class are frozen initially.
  2. After a restoring request is submitted, the server restores the objects, which turns the objects into the restoring state.
  3. It takes one minute to finish restoration by default.
  4. Users can read the objects after the restoration is done.
  5. By default, objects are frozen again one day after being restored. However, the period can be prolonged to seven days at most.

You can callOssClient::restoreObject method to restore the archived objects.

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

Tips:

Create symbolic links

A symbolic link is a special object that points to a specific object, which is similar to the shortcuts used in windows.

You can call the OssClient::putSymlink method to create a symbolic link:

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

Tips:

Getting a symbolic link requires that the user has read permission for the symbolic link.

You can call theOssClient::getSymlinkmethod to get object content that the symlink points to:

  1. <?php
  2. /**
  3. * get object content that symbolic links point to
  4. *
  5. * @param OssClient $ossClient OssClient instance
  6. * @param string $bucket Bucket name
  7. * @return null
  8. */
  9. function getSymlink($ossClient, $bucket)
  10. {
  11. $symlink = "test-symlink";
  12. try {
  13. $Symlinks = $ossClient->getSymlink($bucket, $symlink);
  14. } catch (OssException $e) {
  15. printf(__FUNCTION__ . ": FAILED\n");
  16. printf($e->getMessage() . "\n");
  17. return;
  18. }
  19. print(__FUNCTION__ . ": OK" . "\n");
  20. var_dump($Symlinks);
  21. }

Tips:

Thank you! We've received your feedback.