cos php sdk v5使用,GitHub - lixiang1216/cos-php-sdk-v5: cos-php-sdk-v5

COS-PHP-SDK-V5

腾讯云COS-PHP-SDK-V5(XML API)

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f71636c6f75642f636f732d73646b2d76352e7376673f7374796c653d666c6174

68747470733a2f2f7472617669732d63692e6f72672f74656e63656e7479756e2f636f732d7068702d73646b2d76352e7376673f6272616e63683d6d6173746572

环境准备

PHP 5.3+

您可以通过php -v命令查看当前的PHP版本。

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方式

源码方式

1、Composer方式

推荐用户使用 Composer 安装 cos-php-sdk-v5,Composer是PHP的依赖管理工具,允许您声明项目所需的依赖,然后自动将它们安装到您的项目中。

提示:您可以在 getcomposer.org 上找到更多关于如何安装Composer,配置自动加载以及用于定义依赖项的其他最佳实践。

使用 Composer 安装 COS-PHP-SDK-V5

打开终端

下载 Composer

curl -sS https://getcomposer.org/installer | php

创建一个名为composer.json的文件,内容为

{

"require": {

"qcloud/cos-sdk-v5": "1.*"

}

}

使用 Composer 安装

php composer.phar install

使用该命令后会在当前目录中创建一个vendor文件夹,里面包含 sdk 的依赖库和一个 autoload.php 脚本,方便用户在自己的项目中调用。

5. 通过 autoloader 脚本调用cos-php-sdk-v5

require '/path/to/sdk/vendor/autoload.php';

现在您的项目已经可以使用COS的V5 SDK了。

2、Phar方式

phar方式安装SDK的步骤如下:

在github发布页面下载相应的phar文件

在代码中引入phar文件:

require '/path/to/cos-sdk-v5.phar';

3、源码方式

源码方式安装SDK的步骤如下:

在github发布页面下载相应的zip文件

解压通过 autoload.php 脚本加载sdk

require '/path/to/sdk/vendor/autoload.php';

快速入门

可参照Demo程序,详见 sample.php

接口文档

配置文件

$cosClient = new Qcloud\Cos\Client(array('region' => getenv('COS_REGION'),

'credentials'=> array(

'secretId' => getenv('COS_KEY'),

'secretKey' => getenv('COS_SECRET'))));

上传文件

使用putObject接口上传文件(最大5G)

使用Upload接口分块上传文件

# 上传文件

## 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";

}

下载文件

使用getObject接口下载文件

使用getObjectUrl接口获取文件下载URL

# 下载文件

## 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) {

print_r($e);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值