<?php
namespace OSS;
use OSS\Core\MimeTypes;
use OSS\Core\OssException;
use OSS\Http\RequestCore;
use OSS\Http\RequestCore_Exception;
use OSS\Http\ResponseCore;
use OSS\Model\CorsConfig;
use OSS\Model\CnameConfig;
use OSS\Model\LoggingConfig;
use OSS\Model\LiveChannelConfig;
use OSS\Model\LiveChannelInfo;
use OSS\Model\LiveChannelListInfo;
use OSS\Model\StorageCapacityConfig;
use OSS\Result\AclResult;
use OSS\Result\BodyResult;
use OSS\Result\GetCorsResult;
use OSS\Result\GetLifecycleResult;
use OSS\Result\GetLoggingResult;
use OSS\Result\GetRefererResult;
use OSS\Result\GetWebsiteResult;
use OSS\Result\GetCnameResult;
use OSS\Result\GetLocationResult;
use OSS\Result\HeaderResult;
use OSS\Result\InitiateMultipartUploadResult;
use OSS\Result\ListBucketsResult;
use OSS\Result\ListMultipartUploadResult;
use OSS\Model\ListMultipartUploadInfo;
use OSS\Result\ListObjectsResult;
use OSS\Result\ListPartsResult;
use OSS\Result\PutSetDeleteResult;
use OSS\Result\DeleteObjectsResult;
use OSS\Result\CopyObjectResult;
use OSS\Result\CallbackResult;
use OSS\Result\ExistResult;
use OSS\Result\PutLiveChannelResult;
use OSS\Result\GetLiveChannelHistoryResult;
use OSS\Result\GetLiveChannelInfoResult;
use OSS\Result\GetLiveChannelStatusResult;
use OSS\Result\ListLiveChannelResult;
use OSS\Result\GetStorageCapacityResult;
use OSS\Result\AppendResult;
use OSS\Model\ObjectListInfo;
use OSS\Result\UploadPartResult;
use OSS\Model\BucketListInfo;
use OSS\Model\LifecycleConfig;
use OSS\Model\RefererConfig;
use OSS\Model\WebsiteConfig;
use OSS\Core\OssUtil;
use OSS\Model\ListPartsInfo;
use OSS\Result\SymlinkResult;
/**
* Class OssClient
*
* Object Storage Service(OSS) 的客户端类,封装了用户通过OSS API对OSS服务的各种操作,
* 用户通过OssClient实例可以进行Bucket,Object,MultipartUpload, ACL等操作,具体
* 的接口规则可以参考官方OSS API文档
*/
class OssClient
{
/**
* 构造函数
*
* 构造函数有几种情况:
* 1. 一般的时候初始化使用 $ossClient = new OssClient($id, $key, $endpoint)
* 2. 如果使用CNAME的,比如使用的是www.testoss.com,在控制台上做了CNAME的绑定,
* 初始化使用 $ossClient = new OssClient($id, $key, $endpoint, true)
* 3. 如果使用了阿里云SecurityTokenService(STS),获得了AccessKeyID, AccessKeySecret, Token
* 初始化使用 $ossClient = new OssClient($id, $key, $endpoint, false, $token)
* 4. 如果用户使用的endpoint是ip
* 初始化使用 $ossClient = new OssClient($id, $key, “1.2.3.4:8900”)
*
* @param string $accessKeyId 从OSS获得的AccessKeyId
* @param string $accessKeySecret 从OSS获得的AccessKeySecret
* @param string $endpoint 您选定的OSS数据中心访问域名,例如oss-cn-hangzhou.aliyuncs.com
* @param boolean $isCName 是否对Bucket做了域名绑定,并且Endpoint参数填写的是自己的域名
* @param string $securityToken
* @param string $requestProxy 添加代理支持
* @throws OssException
*/
public function __construct($accessKeyId, $accessKeySecret, $endpoint, $isCName = false, $securityToken = NULL, $requestProxy = NULL)
{
$accessKeyId = trim($accessKeyId);
$accessKeySecret = trim($accessKeySecret);
$endpoint = trim(trim($endpoint), "/");
if (empty($accessKeyId)) {
throw new OssException("access key id is empty");
}
if (empty($accessKeySecret)) {
throw new OssException("access key secret is empty");
}
if (empty($endpoint)) {
throw new OssException("endpoint is empty");
}
$this->hostname = $this->checkEndpoint($endpoint, $isCName);
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->securityToken = $securityToken;
$this->requestProxy = $requestProxy;
self::checkEnv();
}
/**
* 列举用户所有的Bucket[GetService], Endpoint类型为cname不能进行此操作
*
* @param array $options
* @throws OssException
* @return BucketListInfo
*/
public function listBuckets($options = NULL)
{
if ($this->hostType === self::OSS_HOST_TYPE_CNAME) {
throw new OssException("operation is not permitted with CName host");
}
$this->precheckOptions($options);
$options[self::OSS_BUCKET] = '';
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$response = $this->auth($options);
$result = new ListBucketsResult($response);
return $result->getData();
}
/**
* 创建bucket,默认创建的bucket的ACL是OssClient::OSS_ACL_TYPE_PRIVATE
*
* @param string $bucket
* @param string $acl
* @param array $options
* @param string $storageType
* @return null
*/
public function createBucket($bucket, $acl = self::OSS_ACL_TYPE_PRIVATE, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_PUT;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_HEADERS] = array(self::OSS_ACL => $acl);
if (isset($options[self::OSS_STORAGE])) {
$this->precheckStorage($options[self::OSS_STORAGE]);
$options[self::OSS_CONTENT] = OssUtil::createBucketXmlBody($options[self::OSS_STORAGE]);
unset($options[self::OSS_STORAGE]);
}
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* 删除bucket
* 如果Bucket不为空(Bucket中有Object,或者有分块上传的碎片),则Bucket无法删除,
* 必须删除Bucket中的所有Object以及碎片后,Bucket才能成功删除。
*
* @param string $bucket
* @param array $options
* @return null
*/
public function deleteBucket($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_DELETE;
$options[self::OSS_OBJECT] = '/';
$response = $this->auth($options);
$result = new PutSetDeleteResult($response);
return $result->getData();
}
/**
* 判断bucket是否存在
*
* @param string $bucket
* @return bool
* @throws OssException
*/
public function doesBucketExist($bucket)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_SUB_RESOURCE] = 'acl';
$response = $this->auth($options);
$result = new ExistResult($response);
return $result->getData();
}
/**
* 获取bucket所属的数据中心位置信息
*
* @param string $bucket
* @param array $options
* @throws OssException
* @return string
*/
public function getBucketLocation($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$options[self::OSS_BUCKET] = $bucket;
$options[self::OSS_METHOD] = self::OSS_HTTP_GET;
$options[self::OSS_OBJECT] = '/';
$options[self::OSS_SUB_RESOURCE] = 'location';
$response = $this->auth($options);
$result = new GetLocationResult($response);
return $result->getData();
}
/**
* 获取Bucket的Meta信息
*
* @param string $bucket
* @param array $options 具体参考SDK文档
* @return array
*/
public function getBucketMeta($bucket, $options = NULL)
{
$this->precheckCommon($bucket, NULL, $options, false);
$opt