Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/v3 #16

Merged
merged 39 commits into from
Dec 21, 2016
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9f0a85d
php-sdk version 3, just for code review
sabakugaara Mar 14, 2016
e86829f
update directory structure
sabakugaara May 7, 2016
906601a
fix: travis add before_script composer install
sabakugaara May 7, 2016
da2868e
fix php5.3 compatible
sabakugaara May 7, 2016
2ef0f75
fix typo: .travis.yml php7.6 -> php7.0
sabakugaara May 7, 2016
2f32c4a
update test assets: use small jpg
sabakugaara May 7, 2016
20c541b
add Expect: header
sabakugaara May 7, 2016
7ec3df7
update multipart test case
sabakugaara May 7, 2016
9fa0c24
only test php5.3
sabakugaara May 7, 2016
964d5e1
update test case
sabakugaara May 7, 2016
f3b1272
fix double content-lenth header bug
sabakugaara May 8, 2016
eb587bb
rename Requests to Request
sabakugaara May 8, 2016
786ed6b
update: move sign method from BucketConfig to Signature
sabakugaara May 9, 2016
699294b
Merge branch 'master' into feature/refactor
sabakugaara Aug 9, 2016
baa54db
feat: add .editorconfig
sabakugaara Aug 9, 2016
72fbe75
feat: md5 operatorPassword when set
sabakugaara Aug 9, 2016
eb63d39
feat: only test php5.3
sabakugaara Aug 9, 2016
44a43b5
feat: rename Filesystem to Upyun, rename BucketConfig to Config
sabakugaara Aug 9, 2016
0be4d31
style: fix format
sabakugaara Aug 9, 2016
1629305
feat: Config::operatorPassword now is public
sabakugaara Aug 9, 2016
e5b4119
feat: add more config options, but not implement
sabakugaara Aug 9, 2016
7b4dfc1
wait revert
sabakugaara Aug 10, 2016
d60b62f
fix: Config class bug
sabakugaara Aug 10, 2016
179d789
feat: add GuzzleHttp
sabakugaara Aug 10, 2016
5398e29
Merge branch 'feature/refactor' into feature/refactor-no-guzzle
sabakugaara Dec 9, 2016
d5f7684
fix test case
sabakugaara Dec 13, 2016
4368b8b
feat: 增加音视频处理 API
sabakugaara Dec 13, 2016
5d845cd
feat: 增加表单 API 签名算法和客户端上传演示
sabakugaara Dec 13, 2016
04de7c5
update readme.md
sabakugaara Dec 13, 2016
fd33052
feat: 实现断点续传 API
sabakugaara Dec 13, 2016
ae510b3
feat: update php version
sabakugaara Dec 14, 2016
aa33468
feat: 增加删除文件后,`has` 方法会延迟的说明
sabakugaara Dec 14, 2016
7890935
feat: update php version
sabakugaara Dec 14, 2016
3137bdb
feat: rename Video -> Pretreat
sabakugaara Dec 14, 2016
8083c14
feat: add scrutinizer
sabakugaara Dec 19, 2016
a8e2146
feat: 完善文档注释
sabakugaara Dec 21, 2016
39d160b
fix: doc typo
sabakugaara Dec 21, 2016
5448ceb
fix: testDeleteDir test case failed
sabakugaara Dec 21, 2016
626d6e1
Merge branch 'master' into feature/v3
sabakugaara Dec 21, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*.php]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.DS_Store
vendor/
.idea
vendor
composer.lock
.idea/
9 changes: 9 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
filter:
paths:
- 'src/*'
- 'tests/*'

tools:
php_code_sniffer:
config:
standard: PSR4
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
language: php
php:
- "5.3"
- "5.4"
- "5.5"
- "5.6"
- "7.0"
before_script:
- composer install
script:
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 UPYUN
Copyright (c) 2016 UPYUN

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
212 changes: 28 additions & 184 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
# 又拍云PHP SDK
![build](https://travis-ci.org/upyun/php-sdk.svg)

又拍云存储PHP SDK,基于 [又拍云存储HTTP REST API接口](http://docs.upyun.com/api/rest_api/) 开发。
又拍云存储PHP SDK,基于[又拍云存储 HTTP API 接口](http://docs.upyun.com/api/) 开发。SDK 包含了文件上传下载刷新等基本操作,以及图片、视频云处理等功能。

- [更新说明](#update instructions)
- [使用说明](#use instructions)
- [安装](#install)
- [初始化UpYun](#init)
- [示例](#usage)
- [上传文件](#upload file)
- [上传图片](#upload img)
- [下载文件](#download file)
- [创建目录](#mkdir)
- [删除目录或者文件](#delete)
- [获取目录文件列表](#file list)
- [获取文件信息](#file info)
- [获取空间使用状况](#bucket info)
- [异常处理](#exception)
- [使用](#usage)
- [贡献代码](#contribute)
- [社区](#community)
- [许可证](#license)

<a name="update instructions"></a>
## 更新说明
#### 3.0.0

- 重写 API 接口,不兼容 2.x 版本
- 集合分块、刷新、视频预处理功能

#### 2.2.0

@@ -45,195 +40,44 @@
composer require upyun/sdk
```

<a name="init"></a>
### 初始化UpYun
```php
require_once('vendor/autoload.php');
$upyun = new UpYun('bucketname', 'operator_name', 'operator_pwd');
```

参数 `bucketname` 为空间名称,`operator_name`、`operator_pwd` 为授权操作员的账号密码。

根据国内的网络情况,又拍云存储API目前提供了电信、联通网通、移动铁通三个接入点,在初始化的时候可以添加可选的第四个参数来指定API接入点。

```php
$upyun = new UpYun('bucketname', 'operator_name', 'operator_pwd', UpYun::ED_TELECOM);
```

接入点有四个值可选:

* `UpYun::ED_AUTO` 根据网络条件自动选择接入点
* `UpYun::ED_TELECOM` 电信接入点
* `UpYun::ED_CNC` 联通网通接入点
* `UpYun::ED_CTT` 移动铁通接入点

默认参数为自动选择API接入点。但是我们推荐根据服务器网络状况,手动设置合理的接入点已获取最佳的访问速度。

**超时时间设置**

在初始化UpYun上传时,可以选择设置上传请求超时时间(默认30s):
```php
$upyun = new UpYun('bucketname', 'operator_name', 'operator_pwd', UpYun::ED_TELECOM, 600);
```

<a name="usage"></a>
## 示例

*示例代码中所有`bucketname`,`operator_name`,`operator_pwd`以及路径需要替换成实际环境的值,账户密码请注意保密*

<a name="upload file"></a>
### 上传文件
### 初始化

文件类空间可以上传任意形式的二进制文件

**1.直接读取整个文件内容:**
```php
$upyun->writeFile('/path/to/server/file.ext', 'your file content', true);
```
require_once('vendor/autoload.php');

**2.文件流的方式上传,可降低内存占用:**
```php
$file_handler = fopen('demo.png', 'r');
$upyun->writeFile('/path/to/server/demo.png', $file_handler, true);
fclose($file_handler);
use Upyun\Upyun;
use Upyun\Config;
$bucketConfig = new Config('yourBucketName', 'yourOperatorName', 'yourOperatorPwd');
$client = new Upyun($bucketConfig);
```
`writeFile()`第三个参数为可选,`true`表示自动创建相应目录,默认值为`false`。
文件空间上传成功后返回`true`。
如果上传失败,则会抛出异常。

<a name="upload img"></a>
### 上传图片
图片可以上传到图片类空间或文件类空间
* 图片空间上传的图片不能超过20M,图片`宽*高*帧数`不能超过`2亿`
* 文件空间上传的图片不能超过1G
*建议站点图片上传到图片空间,便于在请求图片时可以生成自定义版本图片*

**1.上传图片并创建缩略图:**

`writeFile()`方法第四个参数为数组类型可选参数,用来设置文件类型、缩略图处理。
```php
$opts = array(
UpYun::X_GMKERL_THUMBNAIL => 'square' //创建缩略图
);
1. 字符串写入又拍云服务器

$fh = fopen('demo.png', 'r');
$upyun->writeFile('/temp/upload_demo.png', $fh, true, $opts);
fclose($fh);
```
`writeFile()`方法第四个参数可以设置的值还包括:

* UpYun::CONTENT_TYPE
* UpYun::CONTENT_MD5
* UpYun::CONTENT_SECRET
* UpYun::X_GMKERL_THUMBNAIL
* UpYun::X_GMKERL_TYPE
* UpYun::X_GMKERL_VALUE
* UpYun::X_GMKERL_QUALITY
* UpYun::X_GMKERL_UNSHARP

参数的具体使用方法,请参考[标准API上传文件](http://docs.upyun.com/api/rest_api/#_4)

* 图片空间上传成功后会返回一维数组,包含了图片信息,示例如下:

```php
array(
'x-upyun-width' => 2000,
'x-upyun-height' => 1000,
'x-upyun-frames' => 1
'x-upyun-file-type' => "JPEG"
)
$client->write('/save/path', 'file content');
```
如果上传失败,则会抛出异常。

<a name="download file"></a>
### 下载文件
2. 文件流写入又拍云服务器

**1.直接读取文件内容:**
```php
$data = $upyun->readFile('/temp/upload_demo.png');
```

**2.使用文件流模式下载:**
```php
$fh = fopen('/tmp/demo.png', 'w');
$upyun->readFile('/temp/upload_demo.png', $fh);
fclose($fh);
$file = fopen('/local/path/file', 'r');
$client->write('/save/path', $file);
```

直接获取文件时,返回文件内容,使用数据流形式获取时,成功返回`true`。
如果获取文件失败,则抛出异常。

<a name="mkdir"></a>
### 创建目录
```php
$upyun->makeDir('/demo/');
```
目录路径必须以斜杠 `/` 结尾,创建成功返回 `true`,否则抛出异常。
3. 上传图片并转换格式为 `png`,详见[上传作图](http://docs.upyun.com/cloud/image/#_2)

<a name="delete"></a>
### 删除目录或者文件
```php
$upyun->delete('/demo/'); // 删除目录
$upyun->delete('/demo/demo.png'); // 删除文件
```
删除成功返回`true`,否则抛出异常。注意删除目录时,`必须保证目录为空` ,否则也会抛出异常。

<a name="file list"></a>
### 获取目录文件列表
```php
$list = $upyun->getList('/demo/');
$file = $list[0];
echo $file['name']; // 文件名
echo $file['type']; // 类型(目录: folder; 文件: file)
echo $file['size']; // 尺寸
echo $file['time']; // 创建时间
$file = fopen('/local/path/image.jpg', 'r');
$client->write('/save/image.png', $file, array('x-gmkerl-thumb' => '/format/png'));
```
获取目录文件以及子目录列表。需要获取根目录列表是,使用 `$upyun->getList('/')` ,或直接表用方法不传递参数。
目录获取失败则抛出异常。

<a name="file info"></a>
### 获取文件信息
```php
$result = $upyun->getFileInfo('/demo/demo.png');
echo $result['x-upyun-file-type']; // 文件类型
echo $result['x-upyun-file-size']; // 文件大小
echo $result['x-upyun-file-date']; // 创建日期
```
返回结果为一个数组。
4. 下载文件并保存到本地

<a name="bucket info"></a>
### 获取空间使用状况
```php
$upyun->getBucketUsage(); // 获取Bucket空间使用情况
```
返回的结果为空间使用量,单位 ***Byte***

<a name="exception"></a>
## 异常处理
当API请求发生错误时,SDK将抛出异常,具体错误代码请参考[标准API错误代码表](http://docs.upyun.com/api/rest_api/#rest-api)

根据返回HTTP CODE的不同,SDK将抛出以下异常:

* **UpYunAuthorizationException** 401,授权错误
* **UpYunForbiddenException** 403,权限错误
* **UpYunNotFoundException** 404,文件或目录不存在
* **UpYunNotAcceptableException** 406, 目录错误
* **UpYunServiceUnavailable** 503,系统错误

未包含在以上异常中的错误,将统一抛出 `UpYunException` 异常。

为了正确处理API请求中可能出现的异常,建议将API操作放在`try{...}catch(Exception
$e){…}` 块中,如下所示:

```php
try {
$upyun->getFolderUsage('/demo/');
//your code here

} catch(Exception $e) {
echo $e->getCode(); // 错误代码
echo $e->getMessage(); // 具体错误信息
}
$saveLocal = fopen('/local/path/image.jpg', 'w');
// 第二个参数不传时,read 方法将直接返回文件内容
$client->read('/remote/server/image.png', $saveLocal);
```

<a name="contribute"></a>
@@ -245,13 +89,13 @@ try {
<a name="community"></a>
## 社区

- [UPYUN问答社区](http://segmentfault.com/upyun)
- [UPYUN微博](http://weibo.com/upaiyun)
- [问答社区](http://segmentfault.com/upyun)
- [微博](http://weibo.com/upaiyun)

<a name="license"></a>
## 许可证

UPYUN PHP-SDK基于 MIT 开源协议
UPYUN PHP-SDK 基于 MIT 开源协议

<http://www.opensource.org/licenses/MIT>

19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -7,11 +7,19 @@
"homepage": "https://github.com/upyun/php-sdk/",
"license": "MIT",
"require": {
"php": ">=5.3.0",
"ext-curl": "*"
"php": ">=5.5.0",
"ext-curl": "*",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "~4.0",
"phpdocumentor/phpdocumentor": "^2.9"
},
"autoload": {
"psr-4": { "Upyun\\": "src/Upyun/" }
},
"autoload-dev": {
"psr-4": { "Upyun\\Tests\\": "tests/" }
},
"authors": [
{
@@ -30,8 +38,5 @@
"name": "sabakugaara",
"email": "[email protected]"
}
],
"autoload": {
"files": ["upyun.class.php"]
}
]
}
12 changes: 12 additions & 0 deletions examples/client-upload/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## 客户端上传

本示例展示了如何使用表单 API, 直接从客户端进行安全的文件上传, 这种方式不需要客户服务器进行中转, 节省了客户服务器流量, 并且支持 HTTP/HTTPS 两种协议

DEMO 使用 `sdkimg` 空间进行演示, 上传成功后, 访问路径为 `http://sdkimg.b0.upaiyun.com/` 拼接保存路径

#### 运行示例

- `cd examples/client-upload`
- `php -S localhost:9000`

打开浏览器访问 `http://localhost:9000`, 选则文件上传即可.
Loading