Skip to content

Add show progress bar Setting and Demo #233

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

Open
wants to merge 1 commit into
base: dev-2207
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
84 changes: 84 additions & 0 deletions samples/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);
Common::println("b.file speed limit download url:".$signedUrl.PHP_EOL);

// Show a progress bar of get a object
$options = array(
OssClient::OSS_REGISTERED_PROGRESS_CALLBACK=>'process_callback',
OssClient::OSS_CALLBACK_CONTEXT=> time().rand(1111,9999)
);
$ossClient->getObject($bucket,$object,$options);

// Show a progress bar of upload a object
$options = array(
OssClient::OSS_REGISTERED_PROGRESS_CALLBACK=>'process_callback',
OssClient::OSS_CALLBACK_CONTEXT=> time().rand(1111,9999)
);
$ossClient->putObject($bucket,$object,$content,$options);


//******************************* For complete usage, see the following functions ****************************************************

listObjects($ossClient, $bucket);
Expand All @@ -137,6 +152,8 @@
signUrlSpeedUpload($ossClient, $bucket);
signUrlSpeedDownload($ossClient, $bucket);
restoreObject($ossClient,$bucket);
getObjectProgress($ossClient,$bucket);
putObjectProgress($ossClient,$bucket);
/**
* Create a 'virtual' folder
*
Expand Down Expand Up @@ -727,3 +744,70 @@ function restoreObject($ossClient, $bucket)
}
print(__FUNCTION__ . ": OK" . "\n");
}

/**
* Show progress of upload in-memory data to oss
*
* Simple upload---upload specified in-memory data to an OSS object
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/
function putObjectProgress($ossClient, $bucket)
{
$object = "oss-php-sdk-test/upload-test-object-name.txt";
$content = file_get_contents(__FILE__);
$options = array(
OssClient::OSS_REGISTERED_PROGRESS_CALLBACK=>'process_callback',
OssClient::OSS_CALLBACK_CONTEXT=> time().rand(1111,9999)
);
try {
$ossClient->putObject($bucket, $object, $content, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
/**
* Show progress bar of get the content of an object .
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/
function getObjectProgress($ossClient, $bucket)
{
$object = "oss-php-sdk-test/upload-test-object-name.txt";
$options = array(
OssClient::OSS_REGISTERED_PROGRESS_CALLBACK=>'process_callback',
OssClient::OSS_CALLBACK_CONTEXT=> time().rand(1111,9999)
);
try {
$ossClient->getObject($bucket, $object, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}

/**
* A funtion to to show progress bar
* @param $context callback context Unique identification
* @param int $download_size download object size
* @param int $downloaded has downloaded size
* @param int $upload_size upload object size
* @param int $uploaded has uploaded size
*/
function progressCallback($context, $download_size, $downloaded,$upload_size,$uploaded){
if($download_size > 0){
echo "Task:".$context." Downloaded : total length:".$download_size.'B,downloaded:'.$downloaded.'B,percent:'.$downloaded*100/$download_size.'%'.PHP_EOL;
}
if($upload_size > 0){
echo "Task:".$context." Upload : total length:".$upload_size.'B,uploaded:'.$uploaded.'B,percent:'.$uploaded*100/$upload_size.'%'.PHP_EOL;
}
}
43 changes: 43 additions & 0 deletions src/OSS/Http/RequestCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ class RequestCore
*/
public $registered_streaming_write_callback = null;

/**
* The user-defined callback function to call when need to show progress bar.
*/
public $registered_progress_callback = null;

/**
* @var progress callback context Unique identification
*/
public $callback_context = null;

/**
* The request timeout time, which is 5,184,000 seconds,that is, 6 days by default
*
Expand Down Expand Up @@ -551,6 +561,35 @@ public function register_streaming_write_callback($callback)
return $this;
}

/**
* Register a callback function to execute whenever a progress to using
* @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul>
* <li>The name of a global function to execute, passed as a string.</li>
* <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li>
* <li>An anonymous function (PHP 5.3+).</li></ul>
* @param $context callback context Unique identification
* @return $this A reference to the current instance.
*/
public function register_progress_callback($callback,$context)
{
$this->registered_progress_callback = $callback;
$this->callback_context = $context;
return $this;
}


/**
* A callback function that is invoked by cURL for progress.
* @param resource $curl_handle (Required) The cURL handle for the request.
* @param int $download_size (Required) download size
* @param int $downloaded (Required) downloaded size
* @param int $upload_size upload size B
*/
public function streaming_progress_callback($curl_handle, $download_size,$downloaded,$upload_size,$uploaded){
if ($this->registered_progress_callback) {
call_user_func($this->registered_progress_callback, $this->callback_context, $download_size, $downloaded,$upload_size,$uploaded);
}
}

/*%******************************************************************************************%*/
// PREPARE, SEND, AND PROCESS REQUEST
Expand Down Expand Up @@ -657,6 +696,10 @@ public function prep_request()
curl_setopt($curl_handle, CURLOPT_HEADERFUNCTION, array($this, 'streaming_header_callback'));
curl_setopt($curl_handle, CURLOPT_READFUNCTION, array($this, 'streaming_read_callback'));

if($this->registered_progress_callback){
curl_setopt($curl_handle, CURLOPT_NOPROGRESS, false);
curl_setopt($curl_handle, CURLOPT_PROGRESSFUNCTION, array($this, 'streaming_progress_callback'));
}
// Verification of the SSL cert
if ($this->ssl_verification) {
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true);
Expand Down
8 changes: 8 additions & 0 deletions src/OSS/OssClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,12 @@ private function auth($options)
$headers[self::OSS_CONTENT_MD5] = base64_encode(md5($options[self::OSS_CONTENT], true));
}

if (isset($options[self::OSS_REGISTERED_PROGRESS_CALLBACK])){
if(!isset($options[self::OSS_CALLBACK_CONTEXT]) || empty($options[self::OSS_CALLBACK_CONTEXT])){
throw new OssException("callback context can not be empty!");
}
$request->register_progress_callback($options[self::OSS_REGISTERED_PROGRESS_CALLBACK],$options[self::OSS_CALLBACK_CONTEXT]);
}
if (isset($options[self::OSS_CALLBACK])) {
$headers[self::OSS_CALLBACK] = base64_encode($options[self::OSS_CALLBACK]);
}
Expand Down Expand Up @@ -3651,6 +3657,8 @@ public function setConnectTimeout($connectTimeout)

const OSS_LIST_TYPE = "list-type";

const OSS_REGISTERED_PROGRESS_CALLBACK = 'register-progress-callback';
const OSS_CALLBACK_CONTEXT = 'callback-context';
// Domain Types
const OSS_HOST_TYPE_NORMAL = "normal";//http://bucket.oss-cn-hangzhou.aliyuncs.com/object
const OSS_HOST_TYPE_IP = "ip"; //http://1.1.1.1/bucket/object
Expand Down