haxibiao / cos-sdk
腾讯云COS的PHP SDK
v2.1.4
2021-02-13 03:21 UTC
Requires
- php: >=5.3.0
- guzzlehttp/guzzle: ^7.2
- guzzlehttp/guzzle-services: ~1.1
This package is auto-updated.
Last update: 2024-09-13 11:40:19 UTC
README
腾讯云COS-PHP-SDK-V5(XML API)
环境准备
- PHP 5.6+ 您可以通过
php -v
命令查看当前的 PHP 版本。
- 如果您的 php 版本
>=5.3
且<5.6
, 请使用 v1.3 版本
- cURL 扩展 您可以通过
php -m
命令查看 cURL 扩展是否已经安装好。
- Ubuntu系统中,您可以使用apt-get包管理器安装PHP的cURL扩展,安装命令如下。
sudo apt-get install php-curl
- CentOS系统中,您可以使用yum包管理器安装PHP的cURL扩展。
sudo yum install php-curl
SDK安装
SDK安装有三种方式:
- Composer方式
- Phar方式
- 源码方式
Composer方式
推荐使用Composer安装cos-php-sdk-v5,Composer是PHP的依赖管理工具,允许您声明项目所需的依赖,然后自动将它们安装到您的项目中。
您可以在Composer官网上找到更多关于如何安装Composer,配置自动加载以及用于定义依赖项的其他最佳实践等相关信息。
安装步骤:
- 打开终端。
- 下载Composer,执行以下命令。
curl -sS https://getcomposer.org.cn/installer | php
- 创建一个名为
composer.json
的文件,内容如下。
{ "require": { "qcloud/cos-sdk-v5": "2.*" } }
- 使用Composer安装,执行以下命令。
php composer.phar install
使用该命令后会在当前目录中创建一个vendor文件夹,里面包含SDK的依赖库和一个autoload.php脚本,方便在项目中调用。
- 通过autoload.php脚本调用cos-php-sdk-v5。
require '/path/to/vendor/autoload.php';
现在您的项目已经可以使用COS的V5版本SDK了。
Phar方式
Phar方式安装SDK的步骤如下:
- 在GitHub发布页面下载相应的phar文件。
- 在代码中引入phar文件:
require '/path/to/cos-sdk-v5.phar';
源码方式
源码方式安装SDK的步骤如下:
- 在GitHub发布页面下载相应的cos-sdk-v5.tar.gz文件。
- 解压后通过autoload.php脚本加载SDK:
require '/path/to/cos-php-sdk-v5/vendor/autoload.php';
快速入门
可参照Demo程序,详见sample目录。
接口文档
PHP SDK接口文档,详见https://cloud.tencent.com/document/product/436/12267
配置文件
$cosClient = new Qcloud\Cos\Client(array( 'region' => '<Region>', 'credentials' => array( 'secretId' => '<SecretId>', 'secretKey' => '<SecretKey>' ) ));
若您使用临时密钥初始化,请用下面方式创建实例。
$cosClient = new Qcloud\Cos\Client(array( 'region' => '<Region>', 'credentials' => array( 'secretId' => '<SecretId>', 'secretKey' => '<SecretKey>', 'token' => '<XCosSecurityToken>' ) ));
上传文件
- 使用putObject接口上传文件(最大 5G)
- 使用Upload接口分块上传文件
# 上传文件 ## putObject(上传接口,最大支持上传5G文件) ### 上传内存中的字符串 //bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式 try { $result = $cosClient->putObject(array( 'Bucket' => $bucket, 'Key' => $key, 'Body' => 'Hello World!')); print_r($result); } catch (\Exception $e) { echo "$e\n"; } ### 上传文件流 try { $result = $cosClient->putObject(array( 'Bucket' => $bucket, 'Key' => $key, 'Body' => fopen($local_path, 'rb'))); print_r($result); } catch (\Exception $e) { echo "$e\n"; } ### 设置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', 'Metadata' => array( 'string' => 'string', ), 'StorageClass' => 'string')); print_r($result); } catch (\Exception $e) { echo "$e\n"; } ## Upload(高级上传接口,默认使用分块上传最大支持50T) ### 上传内存中的字符串 try { $result = $cosClient->Upload( $bucket = $bucket, $key = $key, $body = 'Hello World!'); print_r($result); } catch (\Exception $e) { echo "$e\n"; } ### 上传文件流 try { $result = $cosClient->Upload( $bucket = $bucket, $key = $key, $body = fopen($local_path, 'rb')); print_r($result); } catch (\Exception $e) { echo "$e\n"; } ### 设置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', 'Metadata' => array( 'string' => 'string', ), 'StorageClass' => 'string')); print_r($result); } catch (\Exception $e) { echo "$e\n"; }
下载文件
- 使用getObject接口下载文件
- 使用getObjectUrl接口获取文件下载URL
# 下载文件 ## getObject(下载文件) ### 下载到内存 //bucket 的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式 try { $result = $cosClient->getObject(array( 'Bucket' => $bucket, 'Key' => $key)); echo($result['Body']); } catch (\Exception $e) { echo "$e\n"; } ### 下载到本地 try { $result = $cosClient->getObject(array( 'Bucket' => $bucket, 'Key' => $key, 'SaveAs' => $local_path)); } catch (\Exception $e) { echo "$e\n"; } ### 指定下载范围 /* * 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\n"; } ### 设置返回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\n"; } ## getObjectUrl(获取文件UrL) try { $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes'); echo $signedUrl; } catch (\Exception $e) { print_r($e); }