123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824 |
- <?php
- require 'vendor/autoload.php';
- $cosClient = new Qcloud\Cos\Client(array(
- 'region' => 'COS_REGION', #地域,如ap-guangzhou,ap-beijing-1
- 'credentials' => array(
- 'secretId' => 'COS_KEY',
- 'secretKey' => 'COS_SECRET',
- ),
- ));
- // 若初始化 Client 时未填写 appId,则 bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
- $bucket = 'test2-1252448703';
- $key = 'a.txt';
- $local_path = "E:/a.txt";
- # 上传文件
- ## putObject(上传接口,最大支持上传5G文件)
- ### 上传内存中的字符串
- try {
- $result = $cosClient->putObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => 'Hello World!'
- ));
- print_r($result);
- # 可以直接通过$result读出返回结果
- echo ($result['ETag']);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 上传文件流
- try {
- $result = $cosClient->putObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => fopen($local_path, 'rb')
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 设置header和meta
- try {
- $result = $cosClient->putObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => fopen($local_path, 'rb'),
- 'ACL' => 'string',
- 'CacheControl' => 'string',
- 'ContentDisposition' => 'string',
- 'ContentEncoding' => 'string',
- 'ContentLanguage' => 'string',
- 'ContentLength' => integer,
- 'cONTENTType' => 'string',
- 'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
- 'GrantFullControl' => 'string',
- 'GrantRead' => 'string',
- 'GrantWrite' => 'string',
- 'Metadata' => array(
- 'string' => 'string',
- ),
- 'StorageClass' => 'string'
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## Upload(高级上传接口,默认使用分块上传最大支持50T)
- ### 上传内存中的字符串
- try {
- $result = $cosClient->upload(
- $bucket = $bucket,
- $key = $key,
- $body = 'Hello World!'
- );
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 上传文件流
- try {
- $result = $cosClient->upload(
- $bucket = $bucket,
- $key = $key,
- $body = fopen($local_path, 'rb')
- );
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 设置header和meta
- try {
- $result = $cosClient->upload(
- $bucket = $bucket,
- $key = $key,
- $body = fopen($local_path, 'rb'),
- $options = array(
- 'ACL' => 'string',
- 'CacheControl' => 'string',
- 'ContentDisposition' => 'string',
- 'ContentEncoding' => 'string',
- 'ContentLanguage' => 'string',
- 'ContentLength' => integer,
- 'ContentType' => 'string',
- 'Expires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
- 'GrantFullControl' => 'string',
- 'GrantRead' => 'string',
- 'GrantWrite' => 'string',
- 'Metadata' => array(
- 'string' => 'string',
- ),
- 'StorageClass' => 'string'
- )
- );
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## 预签名上传createPresignedUrl
- ## 获取带有签名的url
- ### 简单上传预签名
- try {
- #此处可以替换为其他上传接口
- $command = $cosClient->getCommand('putObject', array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => '', //Body可以任意
- ));
- $signedUrl = $command->createPresignedUrl('+10 minutes');
- echo ($signedUrl);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 分块上传预签名
- try {
- #此处可以替换为其他上传接口
- $command = $cosClient->getCommand('uploadPart', array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'UploadId' => '',
- 'PartNumber' => '1',
- 'Body' => '', //Body可以任意
- ));
- $signedUrl = $command->createPresignedUrl('+10 minutes');
- echo ($signedUrl);
- } catch (\Exception $e) {
- echo($e);
- }
- ### 获取签名
- try {
- #此处可以替换为其他上传接口
- $command = $cosClient->getCommand('putObject', array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Body' => '', //Body可以任意
- ));
- $signedUrl = $command->createAuthorization('+10 minutes');
- echo ($signedUrl);
- } catch (\Exception $e) {
- echo($e);
- }
- # 下载文件
- ## getObject(下载文件)
- ### 下载到内存
- try {
- $result = $cosClient->getObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key
- ));
- echo $result['Body'];
- } catch (\Exception $e) {
- echo($e);
- }
- ### 下载到本地
- try {
- $result = $cosClient->getObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'SaveAs' => $local_path
- ));
- } catch (\Exception $e) {
- echo($e);
- }
- ### 指定下载范围
- /*
- * Range 字段格式为 'bytes=a-b'
- */
- try {
- $result = $cosClient->getObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Range' => 'bytes=0-10',
- 'SaveAs' => $local_path
- ));
- } catch (\Exception $e) {
- echo($e);
- }
- ### 设置返回header
- try {
- $result = $cosClient->getObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'ResponseCacheControl' => 'string',
- 'ResponseContentDisposition' => 'string',
- 'ResponseContentEncoding' => 'string',
- 'ResponseContentLanguage' => 'string',
- 'ResponseContentType' => 'string',
- 'ResponseExpires' => 'mixed type: string (date format)|int (unix timestamp)|\DateTime',
- 'SaveAs' => $local_path
- ));
- } catch (\Exception $e) {
- echo($e);
- }
- ## getObjectUrl(获取文件UrL)
- try {
- $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
- echo $signedUrl;
- } catch (\Exception $e) {
- echo($e);
- }
- # 删除object
- ## deleteObject
- try {
- $result = $cosClient->deleteObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'VersionId' => 'string'
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 删除多个object
- ## deleteObjects
- try {
- $result = $cosClient->deleteObjects(array(
- 'Bucket' => 'string',
- 'Objects' => array(
- array(
- 'Key' => $key,
- 'VersionId' => 'string',
- ),
- // ... repeated
- ),
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 获取object信息
- ## headObject
- /*
- * 可代替isObjectExist接口,查询object是否存在
- */
- try {
- $result = $cosClient->headObject(array(
- 'Bucket' => $bucket,
- 'Key' => '11',
- 'VersionId' => '111',
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 获取bucket列表
- ## listBuckets
- try {
- $result = $cosClient->listBuckets();
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 创建bucket
- ## createBucket
- try {
- $result = $cosClient->createBucket(array('Bucket' => $bucket));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 删除bucket
- ## deleteBucket
- try {
- $result = $cosClient->deleteBucket(array(
- 'Bucket' => $bucket
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 获取bucket信息
- ## headBucket
- /*
- * 可代替isBucketExist接口,查询bucket是否存在
- */
- try {
- $result = $cosClient->headBucket(array(
- 'Bucket' => $bucket
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 列出bucket下的object
- ## listObjects
- ### 列出所有object
- /*
- * 该接口一次最多列出1000个,需要列出所有请参考其他服务中的清空并删除bucket接口
- */
- try {
- $result = $cosClient->listObjects(array(
- 'Bucket' => $bucket
- ));
- foreach ($result['Contents'] as $rt) {
- print_r($rt);
- }
- } catch (\Exception $e) {
- echo($e);
- }
- ### 列出带有前缀的object
- try {
- $result = $cosClient->listObjects(array(
- 'Bucket' => $bucket,
- 'Prefix' => 'string'
- ));
- foreach ($result['Contents'] as $rt) {
- print_r($rt);
- }
- } catch (\Exception $e) {
- echo($e);
- }
- # 获取bucket地域
- ## getBucketLocation
- try {
- $result = $cosClient->getBucketLocation(array(
- 'Bucket' => 'lewzylu02',
- ));
- } catch (\Exception $e) {
- echo($e);
- };
- # 多版本相关
- ## putBucketVersioning(开启关闭某个bucket的多版本)
- try {
- $result = $cosClient->putBucketVersioning(array(
- 'Bucket' => $bucket,
- 'Status' => 'Enabled'
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## ListObjectVersions(列出多版本object)
- /*
- * 同名文件会出现多个版本
- */
- try {
- $result = $cosClient->listObjectVersions(array(
- 'Bucket' => $bucket,
- 'Prefix' => 'string'
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## getBucketVersioning(获取某个bucket多版本属性)
- try {
- $result = $cosClient->getBucketVersioning(
- array('Bucket' => $bucket));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # ACL相关
- ## PutBucketAcl(设置bucketACL)
- try {
- $result = $cosClient->putBucketAcl(array(
- 'Bucket' => $bucket,
- 'Grants' => array(
- array(
- 'Grantee' => array(
- 'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
- 'ID' => 'qcs::cam::uin/327874225:uin/327874225',
- 'Type' => 'CanonicalUser',
- ),
- 'Permission' => 'FULL_CONTROL',
- ),
- // ... repeated
- ),
- 'Owner' => array(
- 'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
- 'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
- )));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## getBucketAcl(获取bucketACL)
- try {
- $result = $cosClient->getBucketAcl(array(
- 'Bucket' => $bucket));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## putObjectAcl(设置objectACL)
- try {
- $result = $cosClient->putObjectAcl(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Grants' => array(
- array(
- 'Grantee' => array(
- 'DisplayName' => 'qcs::cam::uin/327874225:uin/327874225',
- 'ID' => 'qcs::cam::uin/327874225:uin/327874225',
- 'Type' => 'CanonicalUser',
- ),
- 'Permission' => 'FULL_CONTROL',
- ),
- // ... repeated
- ),
- 'Owner' => array(
- 'DisplayName' => 'qcs::cam::uin/3210232098:uin/3210232098',
- 'ID' => 'qcs::cam::uin/3210232098:uin/3210232098',
- )));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## GetObjectAcl(获取objectACL)
- try {
- $result = $cosClient->getObjectAcl(array(
- 'Bucket' => $bucket,
- 'Key' => $key));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 生命周期相关
- ## putBucketLifecycle(设置bucket生命周期)
- try {
- $result = $cosClient->putBucketLifecycle(array(
- 'Bucket' => $bucket,
- 'Rules' => array(
- array(
- 'Expiration' => array(
- 'Days' => 1000,
- ),
- 'ID' => 'id1',
- 'Filter' => array(
- 'Prefix' => 'documents/',
- ),
- 'Status' => 'Enabled',
- 'Transitions' => array(
- array(
- 'Days' => 200,
- 'StorageClass' => 'NEARLINE'),
- ),
- ),
- )));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## getBucketLifecycle(获取bucket生命周期)
- try {
- $result = $cosClient->getBucketLifecycle(array(
- 'Bucket' => $bucket,
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## deleteBucketLifecycle(删除bucket生命周期)
- try {
- $result = $cosClient->deleteBucketLifecycle(array(
- 'Bucket' => $bucket,
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 跨域相关
- ## putBucketCors(设置bucket跨域)
- try {
- $result = $cosClient->putBucketCors(array(
- 'Bucket' => $bucket,
- 'CORSRules' => array(
- array(
- 'ID' => '1234',
- 'AllowedHeaders' => array('*'),
- 'AllowedMethods' => array('PUT'),
- 'AllowedOrigins' => array('http://www.qq.com'),
- ),
- ),
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## getBucketCors(获取bucket跨域信息)
- try {
- $result = $cosClient->getBucketCors(array());
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## deleteBucketCors(删除bucket跨域)
- try {
- $result = $cosClient->deleteBucketCors(array(
- // Bucket is required
- 'Bucket' => $bucket,
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 跨区域复制相关
- ## PutBucketReplication(设置bucket跨区域复制)
- ### 注意:目标bucket和源bucket都需要开启多版本
- try {
- $result = $cosClient->putBucketReplication(array(
- 'Bucket' => $bucket,
- 'Role' => 'qcs::cam::uin/327874225:uin/327874225',
- 'Rules'=>array(
- array(
- 'Status' => 'Enabled',
- 'ID' => 'string',
- 'Prefix' => 'string',
- 'Destination' => array(
- 'Bucket' => 'qcs::cos:ap-guangzhou::lewzylu01-1252448703',
- 'StorageClass' => 'standard',
- ),
- // ...repeated
- ),
- ),
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## GetBucketReplication(获取bucket跨区域复制信息)
- try {
- $result = $cosClient->getBucketReplication(array(
- 'Bucket' => $bucket
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## DeleteBucketReplication(删除bucket跨区域复制信息)
- try {
- $result = $cosClient->deleteBucketReplication(array(
- 'Bucket' => $bucket
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 回调相关
- ## PutBucketNotification
- try {
- $result = $cosClient->putBucketNotification(array(
- "Bucket" => $bucket,
- "CloudFunctionConfigurations"=> array(
- array(
- "Id" => "test-1",
- "Filter" => array(
- "Key" => array(
- "FilterRules" => array(
- array(
- "Name" => "Prefix",
- "Value" => "111"
- ),
- array(
- "Name" => "Suffix",
- "Value" => "111"
- ),
- ),
- )
- ),
- "CloudFunction" => "qcs:0:video:sh:appid/1253125191:video/10010",
- "Events" => array(
- 'Event' => "cos:ObjectCreated:*"
- )
- ),
- array(
- "Id" => "test-2",
- "Filter" => array(
- "Key" => array(
- "FilterRules" => array(
- array(
- "Name" => "Prefix",
- "Value" => "111"
- ),
- array(
- "Name" => "Suffix",
- "Value" => "111"
- ),
- ),
- )
- ),
- "CloudFunction" => "qcs:0:video:sh:appid/1253125191:video/10010",
- "Events" => array(
- 'Event' => "cos:ObjectRemove:*"
- )
- ),
- ))
- );
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## GetBucketNotification
- try {
- $result = $cosClient->getBucketNotification(array(
- 'Bucket' => $bucket
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 复制
- ## copyobject(简单复制)
- /*
- * 将{bucket},{region},{cos_path},{versionId}替换成复制源的真实信息
- */
- try {
- $result = $cosClient->copyObject(array(
- 'Bucket' => $bucket,
- 'CopySource' => '{bucket}.cos.{region}.myqcloud.com/{cos_path}?versionId={versionId}',
- 'Key' => 'string',
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## Copy(分块并发复制)
- /*
- * 将{bucket},{region},{cos_path},{versionId}替换成复制源的真实信息
- */
- try {
- $result = $cosClient->copy(
- $bucket = $bucket,
- $key = $key,
- $copysource = '{bucket}.cos.{region}.myqcloud.com/{cos_path}',
- $options = array('VersionId' => '{versionId}'
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 恢复归档文件
- ## restoreObject
- try {
- $result = $cosClient->restoreObject(array(
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'Days' => 7,
- 'CASJobParameters' => array(
- 'Tier' => 'Bulk',
- ),
- ));
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- # 其他服务
- ## 列出某bucket下所有的object
- try {
- $prefix = '';
- $marker = '';
- while (true) {
- $result = $cosClient->listObjects(array(
- 'Bucket' => $bucket,
- 'Marker' => $marker,
- 'MaxKeys' => 1000
- ));
- foreach ($result['Contents'] as $rt) {
- print_r($rt['Key'] . " ");
- /*
- * 使用下面的代码可以删除全部object
- */
- // try {
- // $result = $cosClient->deleteobjects(array(
- // 'Bucket' => $bucket,
- // 'Key' => $rt['Key']));
- // print_r($result);
- // } catch (\Exception $e) {
- // echo($e);
- // }
- }
- $marker = $result['NextMarker'];
- if (!$result['IsTruncated']) {
- break;
- }
- }
- } catch (\Exception $e) {
- echo($e);
- }
- ## 删除所有因上传失败而产生的分块
- /*
- * 可以清理掉因分块上传失败
- */
- try {
- while (true) {
- $result = $cosClient->listMultipartUploads(
- array('Bucket' => $bucket,
- 'Prefix' => ''));
- if (count($result['Uploads']) == 0) {
- break;
- }
- foreach ($result['Uploads'] as $upload) {
- try {
- $rt = $cosClient->abortMultipartUpload(array(
- 'Bucket' => $bucket,
- 'Key' => $upload['Key'],
- 'UploadId' => $upload['UploadId']
- ));
- print_r($rt);
- } catch (\Exception $e) {
- echo($e);
- }
- }
- }
- } catch (\Exception $e) {
- echo($e);
- }
- ## 分块上传断点重传
- /*
- * 仅适用于分块上传失败的情况
- * 需要填写上传失败的uploadId
- */
- try {
- $result = $cosClient->resumeUpload(
- $bucket = $bucket,
- $key = $key,
- $body = fopen("E:/test.txt", 'rb'),
- $uploadId = '152448808231afdf221eb558ab15d1e455d2afd025c5663936142fdf5614ebf6d1668e2eda'
- );
- print_r($result);
- } catch (\Exception $e) {
- echo($e);
- }
- ## 删除某些前缀的空bucket
- function startsWith($haystack, $needle)
- {
- $length = strlen($needle);
- return (substr($haystack, 0, $length) === $needle);
- }
- try {
- $result = $cosClient->listBuckets();
- foreach ($result['Buckets'] as $bucket) {
- $region = $bucket['Location'];
- $name = $bucket['Name'];
- if (startsWith($name, 'lewzylu')) {
- try {
- $cosClient2 = new Qcloud\Cos\Client(array(
- 'region' => $region,
- 'credentials' => array(
- //getenv为获取本地环境变量,请替换为真实密钥
- 'secretId' => getenv('COS_KEY'),
- 'secretKey' => getenv('COS_SECRET'))
- ));
- $rt = $cosClient2->deleteBucket(array('Bucket' => $name));
- print_r($rt);
- } catch (\Exception $e) {
- }
- }
- }
- } catch (\Exception $e) {
- echo($e);
- }
|