<?php
if (file_exists ( 'vendor/autoload.php' )) {
require 'vendor/autoload.php';
} else {
require '../vendor/autoload.php';}
if (file_exists ( 'obs-autoloader.php' )) {
require 'obs-autoloader.php';
} else {
require '../obs-autoloader.php';}
use Obs\S3\ObsClient;
use Obs\Common\ObsException;
$ak = '*** Provide your Access Key ***';
$sk = '*** Provide your Secret Key ***';
$endpoint = 'https://obs.myhwclouds.com:443';
$bucketName = 'my-obs-bucket-demo';
$objectKey = 'my-obs-object-key-demo';
$localFilePath = '/temp/' . $objectKey;
$obsClient = ObsClient::factory ( [
'key' => $ak,
'secret' => $sk,
'endpoint' => $endpoint,
'signature' => 'v4',
'path_style' => true,
'region' => 'CHINA' ,
'socket_timeout' => 30,
'connect_timeout' => 10
] );
try
{
printf("Create a new bucket for demo\n\n");
$obsClient -> createBucket(['Bucket' => $bucketName]);
$sampleFilePath = '/temp/test.txt'; createSampleFile($sampleFilePath);
printf("Uploading a new object to OBS from a file\n\n");
$obsClient -> putObject(['Bucket' => $bucketName, 'Key' => $objectKey, 'SourceFile' => $sampleFilePath]);
$resp = $obsClient -> getObjectMetadata(['Bucket' => $bucketName, 'Key' => $objectKey]);
$objectSize = $resp['ContentLength'];
printf("Object size from metadata:%d\n\n", $objectSize);
$dir = dirname($localFilePath);
if(!is_dir($dir))
{
mkdir($dir,0755,true);
}
$blockSize = 5 * 1024 * 1024; $blockCount = intval($objectSize/ $blockSize);
if($objectSize % $blockSize !== 0){
$blockCount ++;
}
printf("Total blocks count:%d\n\n", $blockCount);
printf("Start to download %s\n\n", $objectKey);
$fp = fopen($localFilePath, 'w+');
$promise = null;
for($i = 0; $i < $blockCount;){
$startPos = $i++ * $blockSize;
$endPos = ($i == $blockCount) ? $objectSize : $i * $blockSize;
$range = sprintf('bytes=%d-%d',$startPos, $endPos);
$p = $obsClient -> getObjectAsync(['Bucket' => $bucketName, 'Key' => $objectKey, 'Range'=> $range], function($exception, $resp) use ($startPos, $endPos, $fp){
fseek($fp, $startPos, 0);
while(!$resp['Body']->eof())
{
$str = $resp['Body']->read(65536);
fwrite($fp, $str);
}
$resp['Body'] -> close();
});
if($promise === null){
$promise = $p;
}
}
$promise -> wait();
fclose($fp);
if(file_exists($sampleFilePath)){
unlink($sampleFilePath);
}
printf("Deleting object %s \n\n", $objectKey);
$obsClient -> deleteObject(['Bucket' => $bucketName, 'Key' => $objectKey]);
} catch ( ObsException $e ) {
echo 'Response Code:' . $e->getStatusCode () . PHP_EOL;
echo 'Error Message:' . $e->getExceptionMessage () . PHP_EOL;
echo 'Error Code:' . $e->getExceptionCode () . PHP_EOL;
echo 'Request ID:' . $e->getRequestId () . PHP_EOL;
echo 'Exception Type:' . $e->getExceptionType () . PHP_EOL;
} finally{
$obsClient->close ();
}
function createSampleFile($filePath)
{
if(file_exists($filePath)){
return;
}
$filePath = iconv('UTF-8', 'GBK', $filePath);
if(is_string($filePath) && $filePath !== '')
{
$fp = null;
$dir = dirname($filePath);
try{
if(!is_dir($dir))
{
mkdir($dir,0755,true);
}
if(($fp = fopen($filePath, 'w+')))
{
for($i=0;$i< 1000000;$i++){
fwrite($fp, uniqid() . "\n");
fwrite($fp, uniqid() . "\n");
if($i % 100 === 0){
fflush($fp);
}
}
}
}finally{
if($fp){
fclose($fp);
}
}
}
}