An OSSClient serves as the Object Storage Service (OSS) client for PHP to manage OSS resources such as buckets and objects.
Create an OSSClient instance
To create an OSSClient instance, you must specify an endpoint. For more information about endpoints, see Regions and endpoints.
You can use one of the following methods to create an OSSClient instance:
Use an OSS endpoint to create an OSSClient instance
The following sample code provides an example on how to create an OSSClient instance by using an OSS endpoint:
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
print $e->getMessage();
}
Use a custom domain name to create an OSSClient instance
The following sample code provides an example on how to create an OSSClient instance by using a custom domain name.
The listBuckets method is unavailable when a custom domain name is used.
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
// true indicates that CNAME is enabled. If CNAME is enabled, a custom domain name is mapped to a bucket.
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
} catch (OssException $e) {
print $e->getMessage();
}
Use STS to create an OSSClient instance
The following sample code provides an example on how to create an OSSClient instance by using Security Token Service (STS):
<?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;
// Before you run the sample code, make sure that the YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET environment variables are configured by using the temporary access credentials that you obtained from STS.
$accessKeyId = getenv("YOUR_ACCESS_KEY_ID");
$accessKeySecret = getenv("YOUR_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the security token obtained from STS.
$securityToken = "yourSecurityToken";
try {
$ossClient = new OssClient(
$accessKeyId, $accessKeySecret, $endpoint, false, $securityToken);
} catch (OssException $e) {
print $e->getMessage();
}
Use EcsRamRole to create an OSSClient instance
You can access OSS from an Elastic Compute Service (ECS) instance by using a RAM role attached to the instance. You can attach a RAM role to an ECS instance to access OSS from the instance by using temporary access credentials that are obtained from STS. The temporary access credentials are automatically generated and updated. Applications can obtain the temporary access credentials by using the instance metadata URL.
Before you use EcsRamRole to create an OSSClient instance, run the following command to install OSS SDK for PHP by using Composer:
composer require alibabacloud/credentials
The following sample code provides an example on how to create an OSSClient instance by using EcsRamRole:
<?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\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Core\OssException;
use OSS\OssClient;
class AlibabaCloudCredentialsWrapper implements CredentialsProvider{
/**
* @var \OSS\Credentials\Credentials
*/
private $wrapper;
public function __construct($wrapper){
$this->wrapper = $wrapper;
}
public function getCredentials(){
$ak = $this->wrapper->getAccessKeyId();
$sk = $this->wrapper->getAccessKeySecret();
$token = $this->wrapper->getSecurityToken();
return new StaticCredentialsProvider($ak, $sk, $token);
}
}
$ecsRamRole = new Credential(array(
// Specify the type of the temporary access credentials. Set the value to ecs_ram_role.
'type' => 'ecs_ram_role',
// Specify the role name.
'role_name' => 'EcsRamRoleOssTest',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ecsRamRole);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
Use STSAssumeRole to create an OSSClient instance
Before you use STSAssumeRole to create an OSSClient instance, run the following command to install OSS SDK for PHP by using Composer:
composer require alibabacloud/credentials
The following sample code provides an example on how to create an OSSClient instance by using STSAssumeRole:
<?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\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
use OSS\Core\OssException;
use OSS\OssClient;
class AlibabaCloudCredentialsWrapper implements CredentialsProvider{
/**
* @var \OSS\Credentials\Credentials
*/
private $wrapper;
public function __construct($wrapper){
$this->wrapper = $wrapper;
}
public function getCredentials(){
$ak = $this->wrapper->getAccessKeyId();
$sk = $this->wrapper->getAccessKeySecret();
$token = $this->wrapper->getSecurityToken();
return new StaticCredentialsProvider($ak, $sk, $token);
}
}
$ramRoleArn = new Credential(array(
// Specify the type of the temporary access credentials. Set the value to ram_role_arn.
'type' => 'ram_role_arn',
// Before you run the sample code, make sure that the YOUR_ACCESS_KEY_ID and YOUR_ACCESS_KEY_SECRET environment variables are configured by using the AccessKey pair of the RAM user.
'access_key_id' => getenv('YOUR_ACCESS_KEY_ID'),
'access_key_secret' => getenv('YOUR_ACCESS_KEY_SECRET'),
// Specify the Alibaba Cloud Resource Name (ARN) of the RAM role, which is the ID of the role to be assumed. Format: acs:ram::$accountID:role/$roleName.
'role_arn' => 'acs:ram::17464958********:role/ossststest',
// Specify a custom session name for the role to distinguish different tokens.
'role_session_name' => 'yourRoleSessionName',
// Specify a custom policy.
'policy' => '',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ramRoleArn);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
var_dump($ossClient);
} catch (OssException $e) {
print $e->getMessage();
}
Configure OSSClient
You can use OSSClient to configure parameters, such as the proxy, connection timeout, and maximum number of connections.
Parameter | Description | Example |
timeout | The timeout period for data transmission at the Socket layer. Unit: seconds. Default value: 5184000. | $ossClient->setTimeout(60); |
connectTimeout | The timeout period to establish a connection. Unit: seconds. Default value: 10. | $ossClient->setConnectTimeout(600); |
maxRetries | The maximum number of retries when a request error occurs. Default value: 3. | $ossClient->setMaxTries(5); |
useSSL | Specifies whether to enable SSL-based authentication. Default value: false. Valid values:
| $ossClient->setUseSSL(true); |
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// Specify the timeout period to establish a connection.
$ossClient->setConnectTimeout(300);
// Specify the maximum number of retries when a request error occurs.
$ossClient->setMaxTries(5);
// Specify the timeout period for data transmission at the Socket layer.
$ossClient->setTimeout(30);
// Specify whether to enable SSL-based authentication.
$ossClient->setUseSSL(true);
} catch (OssException $e) {
print $e->getMessage();
}
Configure a proxy server
You can configure a proxy server for OSS SDK for PHP 5.3 or later.
<?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;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Specify the IP address of the proxy server. Example: http://<username>:<password>@<proxyip>:<proxyport>.
$requestProxy = "yourRequestProxy";
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $requestProxy);
} catch (OssException $e) {
print $e->getMessage();
}