2017 © Pedro Peláez
 

library cos-sdk-v5

PHP SDK for QCloud COS

image

qcloud/cos-sdk-v5

PHP SDK for QCloud COS

  • Monday, July 9, 2018
  • by lewzylu
  • Repository
  • 36 Watchers
  • 36 Stars
  • 10,676 Installations
  • PHP
  • 14 Dependents
  • 0 Suggesters
  • 19 Forks
  • 7 Open issues
  • 14 Versions
  • 79 % Grown

The README.md

COS-PHP-SDK-V5

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

PHP Version License Latest Stable Version Total Downloads Build Status codecov Support Multiple Versions, (*2)

依赖

  • [x] PHP >= 5.6, (*3)

    如果您的 php 版本 >=5.3<5.6 , 请使用 v1.3 版本, (*4)

  • [x] ext-curl, (*5)

  • [x] ext-json
  • [x] ext-simplexml
  • [x] ext-mbstring

安装

SDK 安装有三种方式:, (*6)

Composer 方式

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

composer require qcloud/cos-sdk-v5

您可以在 Composer 官网 上找到更多关于如何安装 Composer,配置自动加载以及用于定义依赖项的其他最佳实践等相关信息。, (*8)

安装步骤

  1. 打开终端;
  2. 下载 Composer,执行以下命令:
curl -sS https://getcomposer.org/installer | php
  1. 创建一个名为composer.json的文件,内容如下:
{
    "require": {
        "qcloud/cos-sdk-v5": "2.*"
    }
}
  1. 使用 Composer 安装,执行以下命令:
php composer.phar install

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

  1. 通过 autoload.php 脚本调用 cos-php-sdk-v5:
require '/path/to/vendor/autoload.php';

现在您的项目已经可以使用 COS 的 V5 版本 SDK 了。, (*10)

Phar 方式

Phar 方式安装 SDK 的步骤如下:, (*11)

  1. GitHub 发布页面 下载相应的 phar 文件; > 对于 PHP 版本>= 5.6<7.2.5的用户请下载cos-sdk-v5-6.phar以使用 Guzzle6 版本。
    > 对于 PHP 版本>=7.2.5的用户请下载cos-sdk-v5-7.phar以使用 Guzzle7 版本。
  2. 在代码中引入 phar 文件:
require '/path/to/cos-sdk-v5.phar';

源码方式

源码方式安装 SDK 的步骤如下:, (*12)

  1. GitHub 发布页面 下载相应的 cos-sdk-v5.tar.gz 文件; > 对于 PHP 版本>= 5.6<7.2.5的用户请下载cos-sdk-v5-6.tar.gz以使用 Guzzle6 版本。
    > 对于 PHP 版本>=7.2.5的用户请下载cos-sdk-v5-7.tar.gz以使用 Guzzle7 版本。
  2. 解压后通过 autoload.php 脚本加载 SDK:
require '/path/to/cos-sdk-v5/vendor/autoload.php';

快速入门

可参照 Demo 程序,详见 sample 目录 。, (*13)

接口文档

PHP SDK 接口文档,详见 https://cloud.tencent.com/document/product/436/12267, (*14)

配置文件

$cosClient = new Qcloud\Cos\Client(array(
    'region' => '<Region>',
    'credentials' => array(
        'secretId' => '<SecretId>',
        'secretKey' => '<SecretKey>'
    )
));

若您使用 临时密钥 初始化,请用下面方式创建实例。, (*15)

$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) {
    echo "$e\n";
}

The Versions

09/07 2018

dev-master

9999999-dev

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

28/05 2018

v1.2.3

1.2.3.0

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

03/05 2018

v1.2.2

1.2.2.0

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

02/05 2018

v1.2.1.1

1.2.1.1

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

26/04 2018

v1.2.1

1.2.1.0

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

24/04 2018

v1.2.0

1.2.0.0

PHP SDK for QCloud COS

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar yaozongyou
by Avatar lewzylu

php cos qcloud

02/02 2018

v1.1.4

1.1.4.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

05/01 2018

v1.1.3

1.1.3.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

19/12 2017

v1.1.2

1.1.2.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

17/10 2017

dev-add-license-1

dev-add-license-1

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

28/09 2017

v1.1.1

1.1.1.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

26/09 2017

v1.1.0

1.1.0.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

13/09 2017

v1.0.1

1.0.1.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu

01/09 2017

v1.0.0

1.0.0.0

COS SDK for PHP

  Sources   Download

The Requires

 

by Avatar lewzylu