PHP初始化
OssClient是OSS的PHP客户端,用于管理存储空间和文件等OSS资源。
新建OssClient
新建OSSClient时,需要指定Endpoint。关于Endpoint的更多信息,请参见访问域名和数据中心。
您可以通过以下多种方式新建OSSClient。
使用OSS域名新建OssClient
以下代码用于使用OSS域名新建OSSClient。
<?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;
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (OssException $e) {
print $e->getMessage();
}
使用自定义域名新建OssClient
以下代码用于使用自定义域名新建OSSClient。
使用自定义域名时,无法使用listBuckets方法。
<?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;
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
// true为开启CNAME。CNAME是指将自定义域名绑定到存储空间上。
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
} catch (OssException $e) {
print $e->getMessage();
}
使用STS新建OssClient
以下代码用于使用STS新建一个OssClient。
<?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;
// 运行本代码示例之前,请确保已使用STS服务获取的临时访问密钥设置环境变量YOUR_ACCESS_KEY_ID和YOUR_ACCESS_KEY_SECRET。
$accessKeyId = getenv("YOUR_ACCESS_KEY_ID");
$accessKeySecret = getenv("YOUR_ACCESS_KEY_SECRET");
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从STS服务获取的安全令牌(SecurityToken)。
$securityToken = "yourSecurityToken";
try {
$ossClient = new OssClient(
$accessKeyId, $accessKeySecret, $endpoint, false, $securityToken);
} catch (OssException $e) {
print $e->getMessage();
}
使用EcsRamRole新建OssClient
在云服务器ECS上,您可以通过实例RAM角色的方式访问OSS。实例RAM角色允许您将一个角色关联到云服务器实例,在实例内部基于临时凭证STS访问OSS。临时凭证由系统自动生成和更新,应用程序可以使用指定的实例元数据URL获取临时凭证。
使用EcsRamRole新建OssClient之前,您需要执行以下命令通过composer的方式安装SDK。
composer require alibabacloud/credentials
以下代码用于使用EcsRamRole新建OssClient。
<?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(
// 填写Credential类型,固定值为ecs_ram_role。
'type' => 'ecs_ram_role',
// 填写角色名称。
'role_name' => 'EcsRamRoleOssTest',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ecsRamRole);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// 以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com。其他Region请按实际情况填写。
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
} catch (OssException $e) {
print $e->getMessage();
}
使用STSAssumeRole新建OssClient
使用STSAssumeRole新建OssClient之前,您需要执行以下命令通过composer的方式安装SDK。
composer require alibabacloud/credentials
以下代码用于使用STSAssumeRole新建OssClient。
<?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(
// 填写Credential类型,固定值为ram_role_arn。
'type' => 'ram_role_arn',
// 运行本代码示例之前,请确保已使用RAM用户的访问密钥设置环境变量YOUR_ACCESS_KEY_ID和YOUR_ACCESS_KEY_SECRET。
'access_key_id' => getenv('YOUR_ACCESS_KEY_ID'),
'access_key_secret' => getenv('YOUR_ACCESS_KEY_SECRET'),
// 填写角色的ARN信息,即需要扮演的角色ID。格式为acs:ram::$accountID:role/$roleName。
'role_arn' => 'acs:ram::17464958********:role/ossststest',
// 自定义角色会话名称,用于区分不同的令牌。
'role_session_name' => 'yourRoleSessionName',
// 自定义权限策略。
'policy' => '',
));
$providerWarpper = new AlibabaCloudCredentialsWrapper($ramRoleArn);
$provider = $providerWarpper->getCredentials();
$config = array(
'provider' => $provider,
// 以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com。其他Region请按实际情况填写。
'endpoint'=> 'https://oss-cn-hangzhou.aliyuncs.com'
);
try {
$ossClient = new OssClient($config);
var_dump($ossClient);
} catch (OssException $e) {
print $e->getMessage();
}
配置OssClient
您可以通过OssClient配置代理、连接超时、最大连接数等参数。
参数 | 描述 | 方法 |
timeout | 设置Socket层传输数据的超时时间 ,默认值为5184000,单位为秒。 | $ossClient->setTimeout(60); |
connectTimeout | 设置建立连接的超时时间,默认值为10,单位为秒。 | $ossClient->setConnectTimeout(600); |
maxRetries | 请求失败后最大的重试次数。默认3次。 | $ossClient->setMaxTries(5); |
useSSL | 是否开启SSL证书校验。取值如下:
| $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;
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
// 设置建立连接的超时时间。
$ossClient->setConnectTimeout(300);
// 设置失败请求重试次数。
$ossClient->setMaxTries(5);
// 设置Socket层传输数据的超时时间。
$ossClient->setTimeout(30);
// 设置是否开启SSL证书校验。
$ossClient->setUseSSL(true);
} catch (OssException $e) {
print $e->getMessage();
}
配置代理服务器
PHP 5.3及以上版本支持配置代理服务器。
<?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;
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// 设置代理服务器地址,例如http://<用户名>:<密码>@<代理ip>:<代理端口>。
$requestProxy = "yourRequestProxy";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $requestProxy);
} catch (OssException $e) {
print $e->getMessage();
}