# COS-PHP-SDK-V5
腾讯云COS-PHP-SDK-V5([XML API](https://cloud.tencent.com/document/product/436/7751))
[![Total Downloads](https://img.shields.io/packagist/dt/qcloud/cos-sdk-v5.svg?style=flat)](https://packagist.org/packages/qcloud/cos-sdk-v5)
[![Build Status](https://travis-ci.org/tencentyun/cos-php-sdk-v5.svg?branch=master)](https://travis-ci.org/tencentyun/cos-php-sdk-v5)
## 开发准备
### SDK 获取
#### 1、github
```php
#在github中获取代码
https://github.com/tencentyun/cos-php-sdk-v5
```
将源代码放入您的项目目录下即可使用
#### 2、composer
在项目目录下,新建一个composer.json的文件,内容如下
```php
#利用composer下载
{
"require": {
"qcloud/cos-sdk-v5": ">=1.0"
}
}
```
然后使用下面的命令进行安装
```
composer install
```
## 快速入门
```
可参照Demo程序,详见https://github.com/tencentyun/cos-php-sdk-v5/blob/master/sample.php
```
### 配置文件
```php
#这里请填写根路径下cos-autoloader.php文件所在的相对路径
require('/path/to/cos-autoloader.php');
#如果使用composer安装,则可以填写composer生成的autoload.php文件的相对路径
#require('/path/to/autoload.php')
$cosClient = new Qcloud\Cos\Client(array('region' => getenv('COS_REGION'),
'credentials'=> array(
'secretId' => getenv('COS_KEY'),
'secretKey' => getenv('COS_SECRET'))));
```
### 上传文件
* 使用putObject接口上传文件(最大5G)
* 使用Upload接口分块上传文件
```php
# 上传文件
## putObject(上传接口,最大支持上传5G文件)
### 上传内存中的字符串
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',
'GrantFullControl' => 'string',
'GrantRead' => 'string',
'GrantWrite' => 'string',
'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',
'GrantFullControl' => 'string',
'GrantRead' => 'string',
'GrantWrite' => 'string',
'Metadata' => array(
'string' => 'string',
),
'StorageClass' => 'string'));
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
## 预签名上传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\n";
}
### 分块上传预签名
try {
#此处可以替换为其他上传接口
$command = $cosClient->getCommand('uploadPart', array(
'Bucket' => $bucket,
'Key' => $key,
'UploadId' => $uploadId,
'PartNumber' => '1',
'Body' => '' //Body可以任意
));
$signedUrl = $command->createPresignedUrl('+10 minutes');
echo ($signedUrl);
} catch (\Exception $e) {
echo "$e\n";
}
# 下载文件
## getObject(下载文件)
### 下载到内存
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) {
echo "$e\n";
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
php图片加水印且上传至cos (311个子文件)
phpunit.xml.dist 881B
.gitignore 22B
composer.json 1KB
composer.json 968B
composer.json 815B
composer.json 808B
composer.json 798B
composer.json 766B
composer.json 757B
composer.json 723B
composer.json 713B
composer.json 705B
composer.json 690B
composer.json 679B
composer.json 678B
composer.json 672B
composer.json 669B
composer.json 668B
composer.json 668B
composer.json 664B
composer.json 664B
composer.json 653B
composer.json 626B
composer.json 537B
composer.json 503B
composer.json 471B
LICENSE 1KB
LICENSE 1KB
README.md 6KB
README.md 677B
README.md 666B
CHANGELOG.md 471B
cacert.pem 245KB
Service.php 198KB
Test.php 46KB
Mimetypes.php 41KB
cos-autoloader.php 27KB
Response.php 26KB
Parameter.php 24KB
Request.php 19KB
Client.php 16KB
sample.php 16KB
Operation.php 15KB
CurlHandle.php 15KB
Url.php 14KB
CurlMulti.php 14KB
CachePlugin.php 13KB
RequestFactory.php 13KB
Cookie.php 12KB
AbstractCommand.php 12KB
SchemaValidator.php 12KB
Collection.php 11KB
EventDispatcherTest.php 11KB
OauthPlugin.php 10KB
RedirectPlugin.php 10KB
ClientInterface.php 10KB
Client.php 9KB
PhpStreamRequestFactory.php 9KB
Client.php 9KB
QueryString.php 9KB
RequestInterface.php 8KB
DefaultCacheStorage.php 8KB
UriTemplate.php 8KB
XmlVisitor.php 8KB
ContainerAwareEventDispatcherTest.php 8KB
EntityEnclosingRequest.php 7KB
ResourceIterator.php 7KB
ArrayCookieJar.php 7KB
Stream.php 7KB
OperationResponseParser.php 7KB
ServiceDescription.php 7KB
MessageFormatter.php 7KB
MockPlugin.php 7KB
ContainerAwareEventDispatcher.php 6KB
DefaultRevalidation.php 6KB
CachingEntityBody.php 6KB
EntityBody.php 6KB
AbstractConfigLoader.php 6KB
ServiceBuilder.php 6KB
LogPlugin.php 6KB
DefaultRequestSerializer.php 6KB
AbstractMessage.php 6KB
BatchBuilder.php 5KB
XmlVisitor.php 5KB
EventDispatcher.php 5KB
RequestFactoryInterface.php 5KB
VisitorFlyweight.php 5KB
CookieParser.php 5KB
StreamInterface.php 5KB
StaticClient.php 5KB
MultipartUpload.php 5KB
RequestMediator.php 4KB
BackoffPlugin.php 4KB
CurlMultiProxy.php 4KB
Header.php 4KB
EntityEnclosingRequestInterface.php 4KB
AbstractEntityBodyDecorator.php 4KB
ServiceResponseException.php 4KB
CacheAdapterFactory.php 4KB
CompositeFactory.php 4KB
共 311 条
- 1
- 2
- 3
- 4
资源评论
pan235467
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功