API | 操作名 | 操作描述 |
下载对象 | 下载一个对象至本地 |
GET Object
接口。public Qcloud\\Cos\\Client download(string $bucket, string $key, string $saveAs, array $options = array());
参数名称 | 类型 | 描述 | 是否必填 |
bucket | String | 存储桶名称,格式:BucketName-APPID | 是 |
key | String | 对象键 | 是 |
saveAs | String | 保存的本地路径 | 是 |
options | Array | 附加的配置项 | 否 |
options 参数 | 类型 | 描述 | 是否必填 |
Progress | Function | 进度条回调,参数为总大小($totalSize),已上传大小($downloadedSize) | 否 |
PartSize | Int | 最小分块文件大小,默认为5M | 否 |
Concurrency | Int | 并发度,默认为10 | 否 |
ResumableDownload | Bool | 是否开启断点续传,默认为 False | 否 |
ResumableTaskFile | String | 断点文件路径。默认为<saveAs.cosresumabletask> | 否 |
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));$local_path = "/Users/xxx/Desktop/exampleobject.txt"; //保存到用户本地路径$printbar = function($totalSize, $downloadedSize) {printf("downloaded [%d/%d]\\n", $downloadedSize, $totalSize);};try {$result = $cosClient->download($bucket = 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket$key = 'exampleobject',$saveAs = $local_path,$options= array('Progress' => $printbar, //指定进度条'PartSize' => 10 * 1024 * 1024, //分块大小'Concurrency' => 5, //并发数'ResumableDownload' => true, //是否开启断点续传,默认为false'ResumableTaskFile' => 'tmp.cosresumabletask' //断点文件信息路径,默认为<localpath>.cosresumabletask));// 请求成功print_r($result);} catch (\\Exception $e) {// 请求失败echo($e);}
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));$cos_path = 'cos/folder';$nextMarker = '';$isTruncated = true;while ($isTruncated) {try {$result = $cosClient->listObjects(array('Bucket' => 'examplebucket-125000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket'Delimiter' => '/', //Delimiter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的object'EncodingType' => 'url',//编码格式,对应请求中的 encoding-type 参数'Marker' => 'prefix/picture.jpg',//起始对象键标记'Prefix' => 'prfix/', //Prefix表示列出的object的key以prefix开始'MaxKeys' => 1000, // 设置最大遍历出多少个对象, 一次listObjects最大支持1000));} catch (\\Exception $e) {echo($e);}$isTruncated = $result['IsTruncated'];$nextMarker = $result['NextMarker'];foreach ( $result['Contents'] as $content ) {$cos_file_path = $content['Key'];$local_file_path = $content['Key'];// 按照需求自定义拼接下载路径try {$result = $cosClient->download($bucket = 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket$key = $cos_file_path,$saveAs = $local_file_path);echo ($cos_file_path . "\\n");} catch ( \\Exception $e ) {echo($e);}}}
public Guzzle\\Service\\Resource\\Model getObject(array $args = array());
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));$local_path = "/Users/xxx/Desktop/exampleobject.txt"; //保存到用户本地路径try {$result = $cosClient->getObject(array('Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket'Key' => 'exampleobject','SaveAs' => $local_path,));// 请求成功} catch (\\Exception $e) {// 请求失败echo($e);}
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));try {$result = $cosClient->getObject(array('Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket'Key' => 'exampleobject','Range' => 'bytes=0-10'));// 请求成功print_r($result);} catch (\\Exception $e) {// 请求失败echo($e);}
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));try {$result = $cosClient->getObject(array('Bucket' => 'examplebucket-1250000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket'Key' => 'exampleobject','VersionId' => 'exampleVersionId'));// 请求成功print_r($result);} catch (\\Exception $e) {// 请求失败echo($e);}
<?phprequire dirname(__FILE__) . '/../vendor/autoload.php';$secretId = "SECRETID"; //替换为用户的 secretId,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$secretKey = "SECRETKEY"; //替换为用户的 secretKey,请登录访问管理控制台进行查看和管理,https://console.tencentcloud.com/cam/capi$region = "ap-beijing"; //替换为用户的 region,已创建桶归属的region可以在控制台查看,https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //协议头部,默认为http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));try {$result = $cosClient->getObject(array('Bucket' => 'examplebucket-125000000', //存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucket'Key' => 'exampleobject','SaveAs' => '/data/exampleobject','TrafficLimit' => 8 * 1024 * 1024 // 限制为1MB/s));// 请求成功print_r($result);} catch (\\Exception $e) {// 请求失败echo($e);}
本页内容是否解决了您的问题?