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

修改guzzle超时时间 #72

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ SDK 包含如下功能
建议使用速度很快的国内[全量镜像](https://pkg.phpcomposer.com/#how-to-use-packagist-mirror)([又拍云赞助](https://pkg.phpcomposer.com/#donation))

```
composer require upyun/sdk
composer require jasonmann/upyun-sdk
```

2.如果不适应 `composer` 管理,可以直接下载[压缩包](https://github.com/upyun/php-sdk/releases)(注意需要下载 `php-sdk-版本号.zip` 格式的 zip 压缩包,不是 Source code 源码压缩包),解压后,项目中添加如下代码:
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "upyun/sdk",
"name": "jasonmann/upyun-sdk",
"description": "UPYUN sdk for php",
"keywords": ["UPYUN", "sdk"],
"type": "library",
"minimum-stability": "stable",
"homepage": "https://github.com/upyun/php-sdk/",
"license": "MIT",
"require": {
"php": ">=5.5.0",
"php": "^7.2.5 || ^8.0",
"ext-curl": "*",
"guzzlehttp/guzzle": "~6.0"
"guzzlehttp/guzzle": "~7.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down Expand Up @@ -38,6 +39,10 @@
{
"name": "sabakugaara",
"email": "[email protected]"
},
{
"name": "jasonmann",
"email": "[email protected]"
}
]
}
2 changes: 1 addition & 1 deletion src/Upyun/Api/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function request($method, $storagePath)
*/
public function withFile($file)
{
$stream = Psr7\stream_for($file);
$stream = Psr7\Utils::streamFor($file);
$this->file = $stream;

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Upyun/Api/SyncVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function process($params, $path) {
'json' => $params
]);

$body = $response->getBody()->getContents();
$body = (string)$response->getBody();
return json_decode($body, true);
}
}
}
2 changes: 1 addition & 1 deletion src/Upyun/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Config
/**
* @var int request timeout seconds
*/
public $timeout = 60;
public $timeout = 600;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/Upyun/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function getPurgeSignHeader(Config $serviceConfig, $urlString)
* @param $policy
* @param $contentMd5 请求 body 的 md5
*
* @return array
* @return string
*/
public static function getBodySignature(Config $serviceConfig, $method, $uri, $date = null, $policy = null, $contentMd5 = null)
{
Expand Down
93 changes: 89 additions & 4 deletions src/Upyun/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(Config $config)

public function upload($path, $file, $params, $withAsyncProcess)
{
$stream = Psr7\stream_for($file);
$stream = Psr7\Utils::streamFor($file);
$size = $stream->getSize();
$useBlock = $this->needUseBlock($size);

Expand Down Expand Up @@ -87,7 +87,7 @@ private function pointUpload($path, $stream, $params)
'X-Upyun-Multi-Uuid' => $uuid,
'X-Upyun-Part-Id' => $partId
))
->withFile(Psr7\stream_for($fileBlock))
->withFile(Psr7\Utils::streamFor($fileBlock))
->send();

if ($res->getStatusCode() !== 204) {
Expand All @@ -110,6 +110,91 @@ private function pointUpload($path, $stream, $params)
return $res;
}

/**
* 初始化一个分片上传事件
* @param $path
* @param $size
* @param array $params
* @return mixed
* @throws \Exception
*/
public function initiateMultipartUpload($path, $size, $params=[])
{
$req = new Rest($this->config);
$headers = array();
if (is_array($params)) {
foreach ($params as $key => $val) {
$headers['X-Upyun-Meta-' . $key] = $val;
}
}

$res = $req->request('PUT', $path)
->withHeaders(array_merge(array(
'X-Upyun-Multi-Stage' => 'initiate',
'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path),
'X-Upyun-Multi-Length' => $size,
), $headers))
->send();
if ($res->getStatusCode() !== 204) {
throw new \Exception('init request failed when poinit upload!');
}
$init = Util::getHeaderParams($res->getHeaders());
$uuid = $init['x-upyun-multi-uuid'];
return $uuid;
}

/**
* 上传分片
* @param $path
* @param $fileBlock
* @param $i
* @param $uuid
* @return mixed
* @throws \Exception
*/
public function uploadPart($path, $fileBlock, $i, $uuid)
{
$req = new Rest($this->config);

$res = $req->request('PUT', $path)
->withHeaders(array(
'X-Upyun-Multi-Stage' => 'upload',
'X-Upyun-Multi-Uuid' => $uuid,
'X-Upyun-Part-Id' => $i
))
->withFile(Psr7\Utils::streamFor($fileBlock))
->send();
if ($res->getStatusCode() !== 204) {
throw new \Exception('upload request failed when point upload!');
}
$data = Util::getHeaderParams($res->getHeaders());
$partId = $data['x-upyun-next-part-id'];
return $partId;
}

/**
* 完成分片上传
* @param $path
* @param $uuid
* @return array
* @throws \Exception
*/
public function completeMultipartUpload($path, $uuid)
{
$req = new Rest($this->config);

$res = $req->request('PUT', $path)
->withHeaders(array(
'X-Upyun-Multi-Uuid' => $uuid,
'X-Upyun-Multi-Stage' => 'complete'
))
->send();
if ($res->getStatusCode() != 204 && $res->getStatusCode() != 201) {
throw new \Exception('end request failed when poinit upload!');
}
return Util::getHeaderParams($res->getHeaders());
}

private function needUseBlock($fileSize)
{
if ($this->config->uploadType === 'BLOCK' ||
Expand Down Expand Up @@ -146,7 +231,7 @@ private function concurrentPointUpload($path, $stream, $params)
->withHeaders(array_merge(array(
'X-Upyun-Multi-Disorder' => 'true',
'X-Upyun-Multi-Stage' => 'initiate',
'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path),
'X-Upyun-Multi-Type' => Psr7\MimeType::fromFilename($path),
'X-Upyun-Multi-Length' => $stream->getSize(),
), $headers))
->send();
Expand All @@ -167,7 +252,7 @@ private function concurrentPointUpload($path, $stream, $params)
'X-Upyun-Multi-Uuid' => $uuid,
'X-Upyun-Part-Id' => $i
))
->withFile(Psr7\stream_for($fileBlock))
->withFile(Psr7\Utils::streamFor($fileBlock))
->toRequest();
}
};
Expand Down
46 changes: 44 additions & 2 deletions src/Upyun/Upyun.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,48 @@ public function write($path, $content, $params = array(), $withAsyncProcess = fa
return Util::getHeaderParams($response->getHeaders());
}

/**
* 初始化一个分片上传事件
* @param $path
* @param $size
* @param array $params
* @return mixed
* @throws \Exception
*/
public function initiateMultipartUpload($path, $size, $params=[])
{
$upload = new Uploader($this->config);
return $upload->initiateMultipartUpload($path, $size, $params);
}

/**
* 上传分片
* @param $path
* @param $fileBlock
* @param $i
* @param $uuid
* @return mixed
* @throws \Exception
*/
public function uploadPart($path, $fileBlock, $i, $uuid)
{
$upload = new Uploader($this->config);
return $upload->uploadPart($path, $fileBlock, $i, $uuid);
}

/**
* 完成分片上传
* @param $path
* @param $uuid
* @return array
* @throws \Exception
*/
public function completeMultipartUpload($path, $uuid)
{
$upload = new Uploader($this->config);
return $upload->completeMultipartUpload($path, $uuid);
}

/**
* 读取云存储文件/目录内容
*
Expand Down Expand Up @@ -154,7 +196,7 @@ public function read($path, $saveHandler = null, $params = array())

if (! isset($params['x-upyun-list-iter'])) {
if (is_resource($saveHandler)) {
Psr7\copy_to_stream($response->getBody(), Psr7\stream_for($saveHandler));
Psr7\Utils::copyToStream($response->getBody(), Psr7\stream_for($saveHandler));
return true;
} else {
return $response->getBody()->getContents();
Expand Down Expand Up @@ -351,7 +393,7 @@ public function purge($urls)
'headers' => Signature::getPurgeSignHeader($this->config, $urlString),
'form_params' => ['purge' => $urlString]
]);
$result = json_decode($response->getBody()->getContents(), true);
$result = json_decode((string)$response->getBody(), true);
return $result['invalid_domain_of_url'];
}

Expand Down