简介
本文档提供关于对象的删除操作相关的 API 概览以及 SDK 示例代码。
API | 操作名 | 操作描述 |
DELETE Object | 删除单个对象 | 在存储桶中删除指定对象 |
DELETE Multiple Objects | 删除多个对象 | 在存储桶中批量删除对象 |
删除单个对象
功能说明
在存储桶中删除指定 Object (文件/对象)。
方法原型
public Guzzle\Service\Resource\Model deleteObject(array $args = array());
请求示例
示例一:删除一个简单对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client( array( 'region' => $region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey)));
try { $result = $cosClient->deleteObject(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Key' => 'exampleobject' //若多路径则写为folder/exampleobject,不要在第一层带/,否则删除会失败 )); // 请求成功 print_r($result);} catch (\Exception $e) { // 请求失败 echo($e);}
示例二:删除一个带版本号的对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client( array( 'region' => $region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey)));
try { $result = $cosClient->deleteObject(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Key' => 'exampleobject', //若多路径则写为folder/exampleobject,不要在第一层带/,否则删除会失败 'VersionId' => 'exampleVersionId' //存储桶未开启版本控制时请勿携带此参数 )); // 请求成功 print_r($result);} catch (\Exception $e) { // 请求失败 echo($e);}
参数说明
参数名称 | 类型 | 描述 | 是否必填 |
Bucket | String | 存储桶名称,格式:BucketName-APPID | 是 |
Key | String | 此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
VersionId | String | 删除文件的版本号 | 否 |
删除多个对象
功能说明
在存储桶中批量删除 Object (文件/对象)。
方法原型
public Guzzle\Service\Resource\Model deleteObjects(array $args = array());
请求示例
示例一:删除多个简单对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client( array( 'region' => $region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey)));
try { $result = $cosClient->deleteObjects(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Objects' => array( array( 'Key' => 'exampleobject', //若多路径则写为folder/exampleobject,不要在第一层带/,否则删除会失败 ), // ... repeated ), )); // 请求成功 print_r($result);} catch (\Exception $e) { // 请求失败 echo($e);}
示例二:删除多个带版本号的对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client( array( 'region' => $region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey)));
try { $result = $cosClient->deleteObjects(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Objects' => array( array( 'Key' => 'exampleobject', //若多路径则写为folder/exampleobject,不要在第一层带/,否则删除会失败 'VersionId' => 'string' //存储桶未开启版本控制时请勿携带此参数 ), // ... repeated ), )); // 请求成功 print_r($result);} catch (\Exception $e) { // 请求失败 echo($e);}
指定前缀删除(删除文件夹)
功能说明
在存储桶中删除文件夹。
方法原型
public Guzzle\Service\Resource\Model deleteObjects(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.cloud.tencent.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket$cosClient = new Qcloud\Cos\Client( array( 'region' => $region, 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId , 'secretKey' => $secretKey)));
$cos_prefix = "cos/folder";$nextMarker = '';$isTruncated = true;while ( $isTruncated ) { try { $result = $cosClient->listObjects( ['Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Delimiter' => '', 'EncodingType' => 'url', 'Marker' => $nextMarker, 'Prefix' => $cos_prefix, 'MaxKeys' => 1000] ); $isTruncated = $result['IsTruncated']; $nextMarker = $result['NextMarker']; foreach ( $result['Contents'] as $content ) { $cos_file_path = $content['Key']; $local_file_path = $content['Key']; // 按照需求自定义拼接下载路径 try { $cosClient->deleteObject(array( 'Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket 'Key' => $cos_file_path, )); echo ( $cos_file_path . "\n" ); } catch ( \Exception $e ) { echo( $e ); } } } catch ( \Exception $e ) { echo( $e ); }}
参数说明
参数名称 | 类型 | 描述 | 是否必填 |
Bucket | String | 存储桶名称,格式:BucketName-APPID | 是 |
Objects | Array | 删除对象列表 | 是 |
Object | Array | 删除的对象 | 是 |
Key | String | 此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
VersionId | String | 删除文件的版本号 | 否 |
返回结果示例
Guzzle\Service\Resource\Model Object( [structure:protected] => [data:protected] => Array ( [Deleted] => Array ( [0] => Array ( [Key] => exampleobject1 ) ) [Errors] => Array ( [0] => Array ( [Key] => exampleobject2 [Code] => [Message] => ) ) [RequestId] => NWNhZWYzYWNfMTlhYTk0MGFfNGRjX2MzZTVhOQ== ))
返回结果说明
参数名称 | 类型 | 描述 | 父节点 |
Deleted | Array | 成功删除的对象的列表 | 无 |
Errors | Array | 失败删除的对象的列表 | 无 |
Key | String | 对象键 | Deleted/Errors |
Code | String | 失败错误码 | Errors |
Message | String | 失败错误信息 | Errors |
对象存储官网1折活动,限时活动,即将结束,速速收藏
同尘科技为腾讯云授权服务中心。
购买腾讯云产品享受折上折,更有现金返利。同意关联立享优惠
转转请注明出处:https://www.yunxiaoer.com/145919.html