Quick start

Last Updated: Nov 01, 2017

Before we start, we have to be familiar with Basic OSS concepts, such as bucket, object, endpoint, AccessKeyID, and AccessKeySecret.

This document describes how to quickly use the OSS PHP SDK to perform common operations, such as bucket creation, object upload, and object download.

Common classes

Class Explanation
OSS\OSSClient OSS client class. An OSSClient instance can be used to call the interface.
OSS\Core\OSSException Exception class of OSS. You must pay attention to this exception when you use the OSSClient.

Basic operations

Create a bucket

You can create a bucket with the following code:

  1. <?php
  2. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  3. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  4. $endpoint = "<Domain that you select to access an OSS data center, such as "http://oss-cn-hangzhou.aliyuncs.com">";
  5. $bucket = "<Name of the bucket in use. Pay attention to naming conventions>";
  6. try {
  7. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  8. $ossClient->createBucket($bucket);
  9. } catch (OssException $e) {
  10. print $e->getMessage();
  11. }

Note:

Upload an object

Objects are the most basic data units on OSS. You can assume objects as files. The following code uploads an object:

  1. <?php
  2. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  3. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  4. $endpoint = "<Domain that you select to access an OSS data center, such as "http://oss-cn-hangzhou.aliyuncs.com">";
  5. $bucket= " <Name of the bucket in use. Pay attention to naming conventions>";
  6. $object = " <Name of the object in use. Pay attention to naming conventions>";
  7. $content = "Hi, OSS.";
  8. try {
  9. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  10. $ossClient->putObject($bucket, $object, $content);
  11. } catch (OssException $e) {
  12. print $e->getMessage();
  13. }

Note:

Download an object

You can read the content of an uploaded object. The following code retrieves the textual content of an object:

  1. <?php
  2. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  3. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  4. $endpoint = "<Domain you select to access an OSS data center, such as "http://oss-cn-hangzhou.aliyuncs.com">";
  5. $bucket= " <Name of the bucket in use. Pay attention to naming conventions>";
  6. $object = " <Name of the object in use. Pay attention to naming conventions>";
  7. try {
  8. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  9. $content = $ossClient->getObject($bucket, $object);
  10. print("object content: " . $content);
  11. } catch (OssException $e) {
  12. print $e->getMessage();
  13. }

Note: For more information on object downloading, see Download objects.

List objects

After the upload object operation is completed, you may want to view the objects contained in the bucket. The following code demonstrates how to list objects in the bucket:

  1. <?php
  2. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  3. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  4. $endpoint = "<Domain that you select to access an OSS data center, such as "http://oss-cn-hangzhou.aliyuncs.com">";
  5. $bucket= " <Name of the bucket in use. Pay attention to naming conventions>";
  6. try {
  7. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  8. $listObjectInfo = $ossClient->listObjects($bucket);
  9. $objectList = $listObjectInfo->getObjectList();
  10. if (!empty($objectList)) {
  11. foreach ($objectList as $objectInfo) {
  12. print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "\n");
  13. }
  14. }
  15. } catch (OssException $e) {
  16. print $e->getMessage();
  17. }

Note:

  • The preceding code lists 100 objects by default.
  • For richer listing features, see Manage objects.

Delete an object

The following code deletes a specified object in a specified bucket:

  1. <?php
  2. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  3. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  4. $endpoint = "<Domain that you select to access an OSS data center, such as "http://oss-cn-hangzhou.aliyuncs.com">";
  5. $bucket= " <Name of the bucket in use. Pay attention to naming conventions>";
  6. $object = " <Name of the object in use. Pay attention to naming conventions>";
  7. try {
  8. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  9. $ossClient->deleteObject($bucket, $object);
  10. } catch (OssException $e) {
  11. print $e->getMessage();
  12. }

Handle returned results

The OSSClient provides the following two types of returned data from interfaces:

  • Put and Delete interfaces: The PUT and DELETE operations are successful if “null” is returned by the interfaces without OSSException.

  • Get and List interfaces: The GET and LIST operations are successful if the expected data is returned successfully without OSSException.

For example,

  1. <?php
  2. $bucketListInfo = $ossClient->listBuckets();
  3. $bucketList = $bucketListInfo->getBucketList();
  4. foreach($bucketList as $bucket) {
  5. print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
  6. }

In the preceding code, $bucketListInfo belongs to the OSS\Model\BucketListInfo data type.

Thank you! We've received your feedback.