This topic describes how to use OSS PHP SDK to perform routine operations such as bucket creation, object uploads, and object downloads.

Create a bucket

Run the following code to create a bucket:

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Bucket name
$bucket = "<yourBucketName>";

try {
	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	$ossClient->createBucket($bucket);
} catch (OssException $e) {
	print $e->getMessage();
}

For more information about bucket naming rules, see naming conventions in Basic concepts. For more information about how to create a bucket, see Manage a bucket.

For more information about endpoints, see Regions and endpoints.

Upload objects

Run the following code to upload a file to OSS:

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Bucket name
$bucket= " <yourBucketName>";
// Object name
$object = " <yourObjectName>";
$content = "Hi, OSS.";

try {
	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
	print $e->getMessage();
}

For more information, see Upload objects.

Download objects

Run the following code to download an object:

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Bucket name
$bucket= "<yourBucketName>";
// Object name
$object = "<yourObjectName>";

try {
	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	$content = $ossClient->getObject($bucket, $object);
    print("object content: " . $content);
} catch (OssException $e) {
	print $e->getMessage();
}

For more information, see Download objects.

List objects

Run the following code to list objects in a specified bucket. You can list a maximum of 100 objects by default.

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Bucket name
$bucket= "<yourBucketName>";

try {
	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	
	$listObjectInfo = $ossClient->listObjects($bucket);
	$objectList = $listObjectInfo->getObjectList();
	if (! empty($objectList)) {
		foreach ($objectList as $objectInfo) {
		print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "\n");
		}
	}
} catch (OssException $e) {
	print $e->getMessage();
}

For more information, see List objects in Manage objects.

Delete objects

Run the following code to delete a specified object:

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}

use OSS\OssClient;
use OSS\Core\OssException;

// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Bucket name
$bucket= "<yourBucketName>";
// Object name
$object = "<yourObjectName>";

try {
	$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
	$ossClient->deleteObject($bucket, $object);
} catch (OssException $e) {
	print $e->getMessage();
}

For more information, see Delete objects in Manage objects.