Skip to content

Commit

Permalink
Merge aab611b into 28ec537
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertYue19900425 committed Mar 7, 2017
2 parents 28ec537 + aab611b commit a1874ad
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 26 deletions.
86 changes: 86 additions & 0 deletions samples/Progress_Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
require_once __DIR__ . '/Common.php';

use OSS\OssClient;
use OSS\Core\OssException;

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);
//*******************************简单使用***************************************************************

/**
* 上传下载回调函数
*/
function progress_callback($consumed, $total)
{
echo "consumed: ". $consumed . ", total: " . $total . "\n";
}

/**
* 上传本地文件到oss
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$result = $ossClient->uploadFile($bucket, "oss_file", "local_file", $options);

/**
* 上传字符串到oss
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$result = $ossClient->putObject($bucket, "oss_file", "this is a test for progress callback.", $options);

/**
* 下载oss文件到本地
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
OssClient::OSS_FILE_DOWNLOAD => "sample_progress_callback",
);
$result = $ossClient->getObject($bucket, "oss_file", $options);

/**
* 下载oss文件到临时变量
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$content = $ossClient->getObject($bucket, "oss_file", $options);

/**
* range get oss 到临时变量
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
OssClient::OSS_RANGE => "0=>5",
);
$content = $ossClient->getObject($bucket, "oss_file", $options);

/**
* 追加本地文件到oss
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$ossClient->appendFile($bucket, "oss_append_file0", "local_file", 0, $options);

/**
* 追加临时变量到oss
*/
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$content_array = array('This is for test.');
$position = $ossClient->appendObject($bucket, "oss_append_object0", $content_array[0], 0, $options);

/**
* 分片上传本地文件到oss
*/
exec('dd if=/dev/zero of=multipart_progress_sample bs=1M count=64 >/dev/null');
$options = array(
OssClient::OSS_PROGRESS_CALLBACK => "progress_callback",
);
$result = $ossClient->multiuploadFile($bucket, "oss_file", "multipart_progress_sample", $options);
11 changes: 11 additions & 0 deletions src/OSS/Core/OssUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,15 @@ public static function decodeKey($key, $encoding)
throw new OssException("Unrecognized encoding type: " . $encoding);
}
}
/**
* @param string $crc_init
* @param string $data
* @return string
*/

public static function crc64($crc_init, $data)
{
return crc64_sum($crc_init, $data);
}

}
71 changes: 56 additions & 15 deletions src/OSS/Http/RequestCore.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace OSS\Http;


use OSS\Core\OssUtil;
/**
* Handles all HTTP requests using cURL and manages the responses.
*
Expand Down Expand Up @@ -168,6 +168,16 @@ class RequestCore
*/
public $connect_timeout = 10;

public $crc64 = "0";

public $consumed_bytes = 0;

public $total_bytes = 0;

public $progress_callback = null;

public $response_headers_callback = null;

/*%******************************************************************************************%*/
// CONSTANTS

Expand Down Expand Up @@ -394,7 +404,6 @@ public function set_read_stream($resource, $size = null)
}

$this->read_stream = $resource;

return $this->set_read_stream_size($size);
}

Expand Down Expand Up @@ -489,7 +498,6 @@ public function set_seek_position($position)
public function register_streaming_read_callback($callback)
{
$this->registered_streaming_read_callback = $callback;

return $this;
}

Expand Down Expand Up @@ -517,6 +525,22 @@ public function register_streaming_write_callback($callback)
return $this;
}

public function enable_crc64_check()
{
$this->register_streaming_read_callback(array($this, "crc64_check"));
$this->register_streaming_write_callback(array($this, "crc64_check"));
}

public function enable_progress_callback($progress_callback_func, $total_bytes)
{
$this->progress_callback = $progress_callback_func;
$this->total_bytes = $total_bytes;
}

public function crc64_check($crc, $data)
{
$this->crc64 = OssUtil::crc64($this->crc64, $data);
}

/*%******************************************************************************************%*/
// PREPARE, SEND, AND PROCESS REQUEST
Expand Down Expand Up @@ -545,13 +569,19 @@ public function streaming_read_callback($curl_handle, $file_handle, $length)
}

$read = fread($this->read_stream, min($this->read_stream_size - $this->read_stream_read, $length)); // Remaining upload data or cURL's requested chunk size
$this->read_stream_read += strlen($read);
$read_length = strlen($read);
$this->read_stream_read += $read_length;

$out = $read === false ? '' : $read;

// Execute callback function
if ($this->registered_streaming_read_callback) {
call_user_func($this->registered_streaming_read_callback, $curl_handle, $file_handle, $out);
call_user_func($this->registered_streaming_read_callback, $this->crc64, $out);
}

if ($this->progress_callback) {
$this->consumed_bytes += $read_length;;
call_user_func($this->progress_callback, $this->consumed_bytes, $this->total_bytes);
}

return $out;
Expand Down Expand Up @@ -579,14 +609,29 @@ public function streaming_write_callback($curl_handle, $data)

$written_total += $written_last;
}

// Execute callback function
if ($this->registered_streaming_write_callback) {
call_user_func($this->registered_streaming_write_callback, $curl_handle, $written_total);
call_user_func($this->registered_streaming_write_callback, $this->crc64, $data);
}

if ($this->progress_callback) {
$this->consumed_bytes += $written_total;
call_user_func($this->progress_callback, $this->consumed_bytes, $this->total_bytes);
}

return $written_total;
}

public function streaming_header_callback($curl_handle, $headerContent)
{
$this->response_headers_callback = $this->response_headers_callback . $headerContent;
$content = explode(": ", $headerContent);
if ($content[0] == "Content-Length") {
$content_length = explode("\r\n", $content[1]);
$this->total_bytes = $content_length[0];
}
return strlen($headerContent);
}

/**
* Prepares and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()>
Expand All @@ -598,7 +643,6 @@ public function streaming_write_callback($curl_handle, $data)
public function prep_request()
{
$curl_handle = curl_init();

// Set default options.
curl_setopt($curl_handle, CURLOPT_URL, $this->request_url);
curl_setopt($curl_handle, CURLOPT_FILETIME, true);
Expand Down Expand Up @@ -713,6 +757,8 @@ public function prep_request()
if (isset($this->write_stream)) {
curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, array($this, 'streaming_write_callback'));
curl_setopt($curl_handle, CURLOPT_HEADER, false);
curl_setopt($curl_handle, CURLOPT_HEADERFUNCTION, array($this, 'streaming_header_callback'));
curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1);
} else {
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body);
}
Expand Down Expand Up @@ -744,7 +790,6 @@ public function process_response($curl_handle = null, $response = null)
if ($curl_handle && $response) {
$this->response = $response;
}

// As long as this came back as a valid resource...
if (is_resource($curl_handle)) {
// Determine what's what.
Expand All @@ -753,8 +798,8 @@ public function process_response($curl_handle = null, $response = null)
$this->response_body = substr($this->response, $header_size);
$this->response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
$this->response_info = curl_getinfo($curl_handle);

// Parse out the headers
$this->response_headers = $this->response_headers . $this->response_headers_callback;
$this->response_headers = explode("\r\n\r\n", trim($this->response_headers));
$this->response_headers = array_pop($this->response_headers);
$this->response_headers = explode("\r\n", $this->response_headers);
Expand All @@ -766,12 +811,10 @@ public function process_response($curl_handle = null, $response = null)
$kv = explode(': ', $header);
$header_assoc[strtolower($kv[0])] = isset($kv[1]) ? $kv[1] : '';
}

// Reset the headers to the appropriate property.
$this->response_headers = $header_assoc;
$this->response_headers['info'] = $this->response_info;
$this->response_headers['info']['method'] = $this->method;

if ($curl_handle && $response) {
return new ResponseCore($this->response_headers, $this->response_body, $this->response_code);
}
Expand All @@ -793,15 +836,13 @@ public function send_request($parse = false)

$curl_handle = $this->prep_request();
$this->response = curl_exec($curl_handle);

if ($this->response === false) {
throw new RequestCore_Exception('cURL resource: ' . (string)$curl_handle . '; cURL error: ' . curl_error($curl_handle) . ' (' . curl_errno($curl_handle) . ')');
}

$parsed_response = $this->process_response($curl_handle, $this->response);

curl_close($curl_handle);

if ($parse) {
return $parsed_response;
}
Expand Down

0 comments on commit a1874ad

Please sign in to comment.