Client.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. namespace Qcloud\Cos;
  3. use GuzzleHttp\Client as HttpClient;
  4. use GuzzleHttp\HandlerStack;
  5. use Psr\Http\Message\RequestInterface;
  6. use Psr\Http\Message\ResponseInterface;
  7. use GuzzleHttp\Command\Guzzle\Description;
  8. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  9. use GuzzleHttp\Command\Guzzle\Deserializer;
  10. use GuzzleHttp\Command\CommandInterface;
  11. use GuzzleHttp\Exception\ConnectException;
  12. use GuzzleHttp\Middleware;
  13. use GuzzleHttp\Psr7;
  14. /**
  15. * @method object AbortMultipartUpload(array $args) 舍弃一个分块上传且删除已上传的分片块
  16. * @method object CreateBucket(array $args) 创建存储桶(Bucket)
  17. * @method object CompleteMultipartUpload(array $args) 完成整个分块上传
  18. * @method object CreateMultipartUpload(array $args) 初始化分块上传
  19. * @method object CopyObject(array $args) 复制对象
  20. * @method object DeleteBucket(array $args) 删除存储桶 (Bucket)
  21. * @method object DeleteBucketCors(array $args) 删除跨域访问配置信息
  22. * @method object DeleteBucketTagging(array $args) 删除存储桶标签信息
  23. * @method object DeleteBucketInventory(array $args) 删除存储桶标清单任务
  24. * @method object DeleteObject(array $args) 删除 COS 上单个对象
  25. * @method object DeleteObjects(array $args) 批量删除 COS 对象
  26. * @method object DeleteBucketWebsite(array $args) 删除存储桶(Bucket)的website
  27. * @method object DeleteBucketLifecycle(array $args) 删除存储桶(Bucket)的生命周期配置
  28. * @method object DeleteBucketReplication(array $args) 删除跨区域复制配置
  29. * @method object PutObjectTagging(array $args) 配置对象标签
  30. * @method object GetObjectTagging(array $args) 获取对象标签信息
  31. * @method object DeleteObjectTagging(array $args) 删除对象标签
  32. * @method object GetObject(array $args) 下载对象
  33. * @method object GetObjectAcl(array $args) 获取 COS 对象的访问权限信息(Access Control List, ACL)
  34. * @method object GetBucketAcl(array $args) 获取存储桶(Bucket)的访问权限信息(Access Control List, ACL)
  35. * @method object GetBucketCors(array $args) 查询存储桶(Bucket)跨域访问配置信息
  36. * @method object GetBucketDomain(array $args) 查询存储桶(Bucket)Domain配置信息
  37. * @method object GetBucketAccelerate(array $args) 查询存储桶(Bucket)Accelerate配置信息
  38. * @method object GetBucketWebsite(array $args) 查询存储桶(Bucket)Website配置信息
  39. * @method object GetBucketLifecycle(array $args) 查询存储桶(Bucket)的生命周期配置
  40. * @method object GetBucketVersioning(array $args) 获取存储桶(Bucket)版本控制信息
  41. * @method object GetBucketReplication(array $args) 获取存储桶(Bucket)跨区域复制配置信息
  42. * @method object GetBucketLocation(array $args) 获取存储桶(Bucket)所在的地域信息
  43. * @method object GetBucketNotification(array $args) 获取存储桶(Bucket)Notification信息
  44. * @method object GetBucketLogging(array $args) 获取存储桶(Bucket)日志信息
  45. * @method object GetBucketInventory(array $args) 获取存储桶(Bucket)清单信息
  46. * @method object GetBucketTagging(array $args) 获取存储桶(Bucket)标签信息
  47. * @method object UploadPart(array $args) 分块上传
  48. * @method object PutObject(array $args) 上传对象
  49. * @method object AppendObject(array $args) 追加对象
  50. * @method object PutObjectAcl(array $args) 设置 COS 对象的访问权限信息(Access Control List, ACL)
  51. * @method object PutBucketAcl(array $args) 设置存储桶(Bucket)的访问权限(Access Control List, ACL)
  52. * @method object PutBucketCors(array $args) 设置存储桶(Bucket)的跨域配置信息
  53. * @method object PutBucketDomain(array $args) 设置存储桶(Bucket)的Domain信息
  54. * @method object PutBucketLifecycle(array $args) 设置存储桶(Bucket)生命周期配置
  55. * @method object PutBucketVersioning(array $args) 存储桶(Bucket)版本控制
  56. * @method object PutBucketAccelerate(array $args) 配置存储桶(Bucket)Accelerate
  57. * @method object PutBucketWebsite(array $args) 配置存储桶(Bucket)website
  58. * @method object PutBucketReplication(array $args) 配置存储桶(Bucket)跨区域复制
  59. * @method object PutBucketNotification(array $args) 设置存储桶(Bucket)的回调设置
  60. * @method object PutBucketTagging(array $args) 配置存储桶(Bucket)标签
  61. * @method object PutBucketLogging(array $args) 开启存储桶(Bucket)日志服务
  62. * @method object PutBucketInventory(array $args) 配置存储桶(Bucket)清单
  63. * @method object RestoreObject(array $args) 回热归档对象
  64. * @method object ListParts(array $args) 查询存储桶(Bucket)中正在进行中的分块上传对象
  65. * @method object ListObjects(array $args) 查询存储桶(Bucket)下的部分或者全部对象
  66. * @method object ListBuckets 获取所属账户的所有存储空间列表
  67. * @method object ListObjectVersions(array $args) 获取多版本对象
  68. * @method object ListMultipartUploads(array $args) 获取已上传分块列表
  69. * @method object ListBucketInventoryConfigurations(array $args) 获取清单列表
  70. * @method object HeadObject(array $args) 获取对象的meta信息
  71. * @method object HeadBucket(array $args) 存储桶(Bucket)是否存在
  72. * @method object UploadPartCopy(array $args) 分块copy
  73. * @method object SelectObjectContent(array $args) 检索对象内容
  74. * @method object PutBucketIntelligentTiering(array $args) 存储桶(Bucket)开启智能分层
  75. * @method object GetBucketIntelligentTiering(array $args) 查询存储桶(Bucket)智能分层
  76. * @method object ImageInfo(array $args) 万象-获取图片基本信息
  77. * @method object ImageExif(array $args) 万象-获取图片EXIF信息
  78. * @method object ImageAve(array $args) 万象-获取图片主色调信息
  79. * @method object ImageProcess(array $args) 万象-云上数据处理
  80. * @method object Qrcode(array $args) 万象-二维码下载时识别
  81. * @method object QrcodeGenerate(array $args) 万象-二维码生成
  82. * @method object DetectLabel(array $args) 万象-图片标签
  83. * @method object PutBucketImageStyle(array $args) 万象-增加样式
  84. * @method object GetBucketImageStyle(array $args) 万象-查询样式
  85. * @method object DeleteBucketImageStyle(array $args) 万象-删除样式
  86. * @method object PutBucketGuetzli(array $args) 万象-开通Guetzli压缩
  87. * @method object GetBucketGuetzli(array $args) 万象-查询Guetzli状态
  88. * @method object DeleteBucketGuetzli(array $args) 万象-关闭Guetzli压缩
  89. * @method object GetObjectSensitiveContentRecognition(array $args) 图片审核
  90. * @method object DetectText(array $args) 文本审核
  91. * @method object GetSnapshot(array $args) 媒体截图
  92. * @method object PutBucketReferer(array $args) 添加防盗链
  93. * @method object GetBucketReferer(array $args) 获取防盗链规则
  94. * @method object GetMediaInfo(array $args) 获取媒体信息
  95. * @method object CreateMediaTranscodeJobs(array $args) 媒体转码
  96. * @method object CreateMediaJobs(array $args) 媒体任务
  97. * @method object DescribeMediaJob(array $args) 查询指定的媒体任务
  98. * @method object DescribeMediaJobs(array $args) 拉取拉取符合条件的媒体任务
  99. * @method object CreateMediaSnapshotJobs(array $args) 媒体截图
  100. * @method object CreateMediaConcatJobs(array $args) 媒体拼接
  101. * @method object DetectAudio(array $args) 音频审核
  102. * @method object GetDetectAudioResult(array $args) 主动获取音频审核结果
  103. * @method object GetDetectTextResult(array $args) 主动获取文本文件审核结果
  104. * @method object DetectVideo(array $args) 视频审核
  105. * @method object GetDetectVideoResult(array $args) 主动获取视频审核结果
  106. * @method object DetectDocument(array $args) 文档审核
  107. * @method object GetDetectDocumentResult(array $args) 主动获取文档审核结果
  108. * @method object CreateDocProcessJobs(array $args) 提交文档转码任务
  109. * @method object DescribeDocProcessQueues(array $args) 查询文档转码队列
  110. * @method object DescribeDocProcessJob(array $args) 查询文档转码任务
  111. * @method object GetDescribeDocProcessJobs(array $args) 拉取符合条件的文档转码任务
  112. * @method object DetectImage(array $args) 图片审核
  113. * @method object DetectImages(array $args) 图片审核-批量
  114. * @method object DetectVirus(array $args) 云查毒
  115. * @method object GetDetectVirusResult(array $args) 查询病毒检测任务结果
  116. * @method object GetDetectImageResult(array $args) 主动获取图片审核结果
  117. * @method object CreateMediaVoiceSeparateJobs(array $args) 提交人声分离任务
  118. * @method object DescribeMediaVoiceSeparateJob(array $args) 查询指定的人声分离任务
  119. * @method object DetectWebpage(array $args) 提交网页审核任务
  120. * @method object GetDetectWebpageResult(array $args) 查询网页审核任务结果
  121. * @see \Qcloud\Cos\Service::getService()
  122. */
  123. class Client extends GuzzleClient {
  124. const VERSION = '2.5.1';
  125. public $httpClient;
  126. private $api;
  127. private $desc;
  128. private $action;
  129. private $operation;
  130. private $cosConfig;
  131. private $signature;
  132. private $rawCosConfig;
  133. public function __construct(array $cosConfig) {
  134. $this->rawCosConfig = $cosConfig;
  135. $this->cosConfig['schema'] = isset($cosConfig['schema']) ? $cosConfig['schema'] : 'http';
  136. $this->cosConfig['region'] = isset($cosConfig['region']) ? region_map($cosConfig['region']) : null;
  137. $this->cosConfig['appId'] = isset($cosConfig['credentials']['appId']) ? $cosConfig['credentials']['appId'] : null;
  138. $this->cosConfig['secretId'] = isset($cosConfig['credentials']['secretId']) ? trim($cosConfig['credentials']['secretId']) : '';
  139. $this->cosConfig['secretKey'] = isset($cosConfig['credentials']['secretKey']) ? trim($cosConfig['credentials']['secretKey']) : '';
  140. $this->cosConfig['anonymous'] = isset($cosConfig['credentials']['anonymous']) ? $cosConfig['credentials']['anonymous'] : false;
  141. $this->cosConfig['token'] = isset($cosConfig['credentials']['token']) ? trim($cosConfig['credentials']['token']) : null;
  142. $this->cosConfig['timeout'] = isset($cosConfig['timeout']) ? $cosConfig['timeout'] : 3600;
  143. $this->cosConfig['connect_timeout'] = isset($cosConfig['connect_timeout']) ? $cosConfig['connect_timeout'] : 3600;
  144. $this->cosConfig['ip'] = isset($cosConfig['ip']) ? $cosConfig['ip'] : null;
  145. $this->cosConfig['port'] = isset($cosConfig['port']) ? $cosConfig['port'] : null;
  146. $this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : null;
  147. $this->cosConfig['domain'] = isset($cosConfig['domain']) ? $cosConfig['domain'] : null;
  148. $this->cosConfig['proxy'] = isset($cosConfig['proxy']) ? $cosConfig['proxy'] : null;
  149. $this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1;
  150. $this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION;
  151. $this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false;
  152. $this->cosConfig['signHost'] = isset($cosConfig['signHost']) ? $cosConfig['signHost'] : true;
  153. $this->cosConfig['allow_redirects'] = isset($cosConfig['allow_redirects']) ? $cosConfig['allow_redirects'] : false;
  154. $this->cosConfig['allow_accelerate'] = isset($cosConfig['allow_accelerate']) ? $cosConfig['allow_accelerate'] : false;
  155. // check config
  156. $this->inputCheck();
  157. $service = Service::getService();
  158. $handler = HandlerStack::create();
  159. $handler->push(Middleware::retry($this->retryDecide(), $this->retryDelay()));
  160. $handler->push(Middleware::mapRequest(function (RequestInterface $request) {
  161. return $request->withHeader('User-Agent', $this->cosConfig['userAgent']);
  162. }));
  163. if ($this->cosConfig['anonymous'] != true) {
  164. $handler->push($this::handleSignature($this->cosConfig['secretId'], $this->cosConfig['secretKey'], $this->cosConfig['signHost']));
  165. }
  166. if ($this->cosConfig['token'] != null) {
  167. $handler->push(Middleware::mapRequest(function (RequestInterface $request) {
  168. return $request->withHeader('x-cos-security-token', $this->cosConfig['token']);
  169. }));
  170. }
  171. $handler->push($this::handleErrors());
  172. $this->signature = new Signature($this->cosConfig['secretId'], $this->cosConfig['secretKey'], $this->cosConfig, $this->cosConfig['token']);
  173. $area = $this->cosConfig['allow_accelerate'] ? 'accelerate' : $this->cosConfig['region'];
  174. $this->httpClient = new HttpClient([
  175. 'base_uri' => "{$this->cosConfig['schema']}://cos.{$area}.myqcloud.com/",
  176. 'timeout' => $this->cosConfig['timeout'],
  177. 'handler' => $handler,
  178. 'proxy' => $this->cosConfig['proxy'],
  179. 'allow_redirects' => $this->cosConfig['allow_redirects']
  180. ]);
  181. $this->desc = new Description($service);
  182. $this->api = (array) $this->desc->getOperations();
  183. parent::__construct($this->httpClient, $this->desc, [$this,
  184. 'commandToRequestTransformer'], [$this, 'responseToResultTransformer'],
  185. null);
  186. }
  187. public function inputCheck() {
  188. $message = null;
  189. //检查Region
  190. if (empty($this->cosConfig['region']) &&
  191. empty($this->cosConfig['domain']) &&
  192. empty($this->cosConfig['endpoint']) &&
  193. empty($this->cosConfig['ip'])) {
  194. $message = 'Region is empty';
  195. }
  196. //检查Secret
  197. if (empty($this->cosConfig['secretId']) || empty($this->cosConfig['secretKey'])) {
  198. $message = 'Secret is empty';
  199. }
  200. if ($message !== null) {
  201. $e = new Exception\CosException($message);
  202. $e->setExceptionCode('Invalid Argument');
  203. throw $e;
  204. }
  205. }
  206. public function retryDecide() {
  207. return function (
  208. $retries,
  209. RequestInterface $request,
  210. ResponseInterface $response = null,
  211. \Exception $exception = null
  212. ) {
  213. if ($retries >= $this->cosConfig['retry']) {
  214. return false;
  215. }
  216. if ($response != null && $response->getStatusCode() >= 400 ) {
  217. return true;
  218. }
  219. if ($exception instanceof Exception\ServiceResponseException) {
  220. if ($exception->getStatusCode() >= 400) {
  221. return true;
  222. }
  223. }
  224. if ($exception instanceof ConnectException) {
  225. return true;
  226. }
  227. return false;
  228. };
  229. }
  230. public function retryDelay() {
  231. return function ($numberOfRetries) {
  232. return 1000 * $numberOfRetries;
  233. };
  234. }
  235. public function commandToRequestTransformer(CommandInterface $command)
  236. {
  237. $this->action = $command->GetName();
  238. $this->operation = $this->api[$this->action];
  239. $transformer = new CommandToRequestTransformer($this->cosConfig, $this->operation);
  240. $seri = new Serializer($this->desc);
  241. $request = $seri($command);
  242. $request = $transformer->bucketStyleTransformer($command, $request);
  243. $request = $transformer->uploadBodyTransformer($command, $request);
  244. $request = $transformer->metadataTransformer($command, $request);
  245. $request = $transformer->queryStringTransformer($command, $request);
  246. $request = $transformer->md5Transformer($command, $request);
  247. $request = $transformer->specialParamTransformer($command, $request);
  248. $request = $transformer->ciParamTransformer($command, $request);
  249. $request = $transformer->cosDomain2CiTransformer($command, $request);
  250. return $request;
  251. }
  252. public function responseToResultTransformer(ResponseInterface $response, RequestInterface $request, CommandInterface $command)
  253. {
  254. $transformer = new ResultTransformer($this->cosConfig, $this->operation);
  255. $transformer->writeDataToLocal($command, $request, $response);
  256. $deseri = new Deserializer($this->desc, true);
  257. $result = $deseri($response, $request, $command);
  258. $result = $transformer->metaDataTransformer($command, $response, $result);
  259. $result = $transformer->extraHeadersTransformer($command, $request, $result);
  260. $result = $transformer->selectContentTransformer($command, $result);
  261. $result = $transformer->ciContentInfoTransformer($command, $result);
  262. return $result;
  263. }
  264. public function __destruct() {
  265. }
  266. public function __call($method, array $args) {
  267. try {
  268. $rt = parent::__call(ucfirst($method), $args);
  269. return $rt;
  270. } catch (\Exception $e) {
  271. $previous = $e->getPrevious();
  272. if ($previous !== null) {
  273. throw $previous;
  274. } else {
  275. throw $e;
  276. }
  277. }
  278. }
  279. public function getApi() {
  280. return $this->api;
  281. }
  282. private function getCosConfig() {
  283. return $this->cosConfig;
  284. }
  285. private function createPresignedUrl(RequestInterface $request, $expires) {
  286. return $this->signature->createPresignedUrl($request, $expires);
  287. }
  288. public function getPresignedUrl($method, $args, $expires = "+30 minutes") {
  289. $command = $this->getCommand($method, $args);
  290. $request = $this->commandToRequestTransformer($command);
  291. return $this->createPresignedUrl($request, $expires);
  292. }
  293. public function getObjectUrl($bucket, $key, $expires = "+30 minutes", array $args = array()) {
  294. $command = $this->getCommand('GetObject', $args + array('Bucket' => $bucket, 'Key' => $key));
  295. $request = $this->commandToRequestTransformer($command);
  296. return $this->createPresignedUrl($request, $expires)->__toString();
  297. }
  298. public function getObjectUrlWithoutSign($bucket, $key, array $args = array()) {
  299. $command = $this->getCommand('GetObject', $args + array('Bucket' => $bucket, 'Key' => $key));
  300. $request = $this->commandToRequestTransformer($command);
  301. return $request->getUri()-> __toString();
  302. }
  303. public function upload($bucket, $key, $body, $options = array()) {
  304. $body = Psr7\Utils::streamFor($body);
  305. $options['Retry'] = $this->cosConfig['retry'];
  306. $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE;
  307. if ($body->getSize() < $options['PartSize']) {
  308. $rt = $this->putObject(array(
  309. 'Bucket' => $bucket,
  310. 'Key' => $key,
  311. 'Body' => $body,
  312. ) + $options);
  313. }
  314. else {
  315. $multipartUpload = new MultipartUpload($this, $body, array(
  316. 'Bucket' => $bucket,
  317. 'Key' => $key,
  318. ) + $options);
  319. $rt = $multipartUpload->performUploading();
  320. }
  321. return $rt;
  322. }
  323. public function download($bucket, $key, $saveAs, $options = array()) {
  324. $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : RangeDownload::DEFAULT_PART_SIZE;
  325. $contentLength = 0;
  326. $versionId = isset($options['VersionId']) ? $options['VersionId'] : '';
  327. $rt = $this->headObject(array(
  328. 'Bucket'=>$bucket,
  329. 'Key'=>$key,
  330. 'VersionId'=>$versionId,
  331. )
  332. );
  333. $contentLength = $rt['ContentLength'];
  334. $resumableJson = [
  335. 'LastModified' => $rt['LastModified'],
  336. 'ContentLength' => $rt['ContentLength'],
  337. 'ETag' => $rt['ETag'],
  338. 'Crc64ecma' => $rt['Crc64ecma']
  339. ];
  340. $options['ResumableJson'] = $resumableJson;
  341. if ($contentLength < $options['PartSize']) {
  342. $rt = $this->getObject(array(
  343. 'Bucket' => $bucket,
  344. 'Key' => $key,
  345. 'SaveAs' => $saveAs,
  346. ) + $options);
  347. } else {
  348. $rangeDownload = new RangeDownload($this, $contentLength, $saveAs, array(
  349. 'Bucket' => $bucket,
  350. 'Key' => $key,
  351. ) + $options);
  352. $rt = $rangeDownload->performDownloading();
  353. }
  354. return $rt;
  355. }
  356. public function resumeUpload($bucket, $key, $body, $uploadId, $options = array()) {
  357. $body = Psr7\Utils::streamFor($body);
  358. $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE;
  359. $multipartUpload = new MultipartUpload($this, $body, array(
  360. 'Bucket' => $bucket,
  361. 'Key' => $key,
  362. 'UploadId' => $uploadId,
  363. ) + $options);
  364. $rt = $multipartUpload->resumeUploading();
  365. return $rt;
  366. }
  367. public function copy($bucket, $key, $copySource, $options = array()) {
  368. $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : Copy::DEFAULT_PART_SIZE;
  369. // set copysource client
  370. $sourceConfig = $this->rawCosConfig;
  371. $sourceConfig['region'] = $copySource['Region'];
  372. $cosSourceClient = new Client($sourceConfig);
  373. $copySource['VersionId'] = isset($copySource['VersionId']) ? $copySource['VersionId'] : '';
  374. $rt = $cosSourceClient->headObject(
  375. array('Bucket'=>$copySource['Bucket'],
  376. 'Key'=>$copySource['Key'],
  377. 'VersionId'=>$copySource['VersionId'],
  378. )
  379. );
  380. $contentLength = $rt['ContentLength'];
  381. // sample copy
  382. if ($contentLength < $options['PartSize']) {
  383. $rt = $this->copyObject(array(
  384. 'Bucket' => $bucket,
  385. 'Key' => $key,
  386. 'CopySource' => "{$copySource['Bucket']}.cos.{$copySource['Region']}.myqcloud.com/". urlencode("{$copySource['Key']}")."?versionId={$copySource['VersionId']}",
  387. ) + $options
  388. );
  389. return $rt;
  390. }
  391. // multi part copy
  392. $copySource['ContentLength'] = $contentLength;
  393. $copy = new Copy($this, $copySource, array(
  394. 'Bucket' => $bucket,
  395. 'Key' => $key
  396. ) + $options
  397. );
  398. return $copy->copy();
  399. }
  400. public function doesBucketExist($bucket, array $options = array())
  401. {
  402. try {
  403. $this->HeadBucket(array(
  404. 'Bucket' => $bucket));
  405. return true;
  406. } catch (\Exception $e){
  407. return false;
  408. }
  409. }
  410. public function doesObjectExist($bucket, $key, array $options = array())
  411. {
  412. try {
  413. $this->HeadObject(array(
  414. 'Bucket' => $bucket,
  415. 'Key' => $key));
  416. return true;
  417. } catch (\Exception $e){
  418. return false;
  419. }
  420. }
  421. public static function explodeKey($key) {
  422. // Remove a leading slash if one is found
  423. $split_key = explode('/', $key && $key[0] == '/' ? substr($key, 1) : $key);
  424. // Remove empty element
  425. $split_key = array_filter($split_key, function($var) {
  426. return !($var == '' || $var == null);
  427. });
  428. $final_key = implode("/", $split_key);
  429. if (substr($key, -1) == '/') {
  430. $final_key = $final_key . '/';
  431. }
  432. return $final_key;
  433. }
  434. public static function handleSignature($secretId, $secretKey, $signHost) {
  435. return function (callable $handler) use ($secretId, $secretKey, $signHost) {
  436. return new SignatureMiddleware($handler, $secretId, $secretKey, $signHost);
  437. };
  438. }
  439. public static function handleErrors() {
  440. return function (callable $handler) {
  441. return new ExceptionMiddleware($handler);
  442. };
  443. }
  444. }