Skip to content

add resuamable upload and download #243

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: 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
77 changes: 77 additions & 0 deletions samples/Resumable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
require_once __DIR__ . '/Common.php';

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

$bucket = Common::getBucketName();
$ossClient = Common::getOssClient();
if (is_null($ossClient)) exit(1);

//******************************* Simple usage ***************************************************************

// Upload a file using resumable upload, which determines to use simple upload or multipart upload based on the file size.
$ossClient->resumableUpload($bucket, "a.file", __FILE__, array());
Common::println("local file " . __FILE__ . " is uploaded to the bucket $bucket, a.file");


// Dwonload file useing resumable dwonload
$ossClient->resumableDownload($bucket, "b.file", $options = array(
OssClient::OSS_FILE_DOWNLOAD => "./c.file",
));
Common::println("b.file is fetched to the local file: c.file");


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

resumableUpload($ossClient,$bucket);
resumableDownload($ossClient,$bucket);

/**
* Upload files using resumable upload
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/
function resumableUpload($ossClient, $bucket)
{
$object = "test/multipart-test.txt";
$file = __FILE__;
$options = array();

try {
$ossClient->resumableUpload($bucket, $object, $file, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}


/**
* Download files using resumable download
*
* @param OssClient $ossClient OssClient instance
* @param string $bucket bucket name
* @return null
*/
function resumableDownload($ossClient, $bucket)
{
$object = "test/multipart-test.txt";
$options = array(
OssClient::OSS_FILE_DOWNLOAD => "./c.file",
);

try {
$ossClient->resumableDownload($bucket, $object, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
}
31 changes: 31 additions & 0 deletions src/OSS/Core/OssUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,4 +531,35 @@ public static function decodeKey($key, $encoding)
throw new OssException("Unrecognized encoding type: " . $encoding);
}
}

/**
* getUploadCpFilePath return the file path of the checkpoint
* @param string $filePath
* @param $bucketName
* @param $objectKey
* @return string
*/
public static function getCpFilePath($bucketName,$objectKey,$versionId=""){
$dest = sprintf("oss://%s/%s", $bucketName, $objectKey);
$cpFileName = self::getCpFileName("", $dest, $versionId);
return sys_get_temp_dir(). DIRECTORY_SEPARATOR . $cpFileName;
}

/**
* getCpFileName return the name of the checkpoint file
* @param string $src
* @param string $dest
* @param string $versionId
* @return string
*/
public static function getCpFileName($src,$dest,$versionId){
$srcCheckSum = md5($src);
$destCheckSum = md5($dest);
if ($versionId == ''){
return sprintf("%s-%s.cp", $srcCheckSum, $destCheckSum);
}
$versionCheckSum = md5($versionId);
return sprintf("%s-%s-%s.cp", $srcCheckSum, $destCheckSum,$versionCheckSum);
}

}
Loading