| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 | <?phprequire_once __DIR__ . '/Common.php';use OSS\OssClient;use OSS\Core\OssException;use OSS\Model\RestoreConfig;$bucket = Common::getBucketName();$ossClient = Common::getOssClient();if (is_null($ossClient)) exit(1);//******************************* Simple usage ***************************************************************// Upload the in-memory string (hi, oss) to an OSS file$result = $ossClient->putObject($bucket, "b.file", "hi, oss");Common::println("b.file is created");Common::println($result['x-oss-request-id']);Common::println($result['etag']);Common::println($result['content-md5']);Common::println($result['body']);// Uploads a local file to an OSS file$result = $ossClient->uploadFile($bucket, "c.file", __FILE__);Common::println("c.file is created");Common::println("b.file is created");Common::println($result['x-oss-request-id']);Common::println($result['etag']);Common::println($result['content-md5']);Common::println($result['body']);// Download an oss object as an in-memory variable$content = $ossClient->getObject($bucket, "b.file");Common::println("b.file is fetched, the content is: " . $content);// Add a symlink to an object$content = $ossClient->putSymlink($bucket, "test-symlink", "b.file");Common::println("test-symlink is created");Common::println($result['x-oss-request-id']);Common::println($result['etag']);// Get a symlink$content = $ossClient->getSymlink($bucket, "test-symlink");Common::println("test-symlink refer to : " . $content[OssClient::OSS_SYMLINK_TARGET]);// Download an object to a local file.$options = array(    OssClient::OSS_FILE_DOWNLOAD => "./c.file.localcopy",);$ossClient->getObject($bucket, "c.file", $options);Common::println("b.file is fetched to the local file: c.file.localcopy");Common::println("b.file is created");// Restore Object$day = 3;$tier = 'Expedited';$config = new RestoreConfig($day,$tier);$options = array(	OssClient::OSS_RESTORE_CONFIG => $config);$ossClient->restoreObject($bucket, 'b.file',$options);// Copy an object$result = $ossClient->copyObject($bucket, "c.file", $bucket, "c.file.copy");Common::println("lastModifiedTime: " . $result[0]);Common::println("ETag: " . $result[1]);// Check whether an object exists$doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));// Delete an object$result = $ossClient->deleteObject($bucket, "c.file.copy");Common::println("c.file.copy is deleted");Common::println("b.file is created");Common::println($result['x-oss-request-id']);// Check whether an object exists$doesExist = $ossClient->doesObjectExist($bucket, "c.file.copy");Common::println("file c.file.copy exist? " . ($doesExist ? "yes" : "no"));// Delete multiple objects in batch$result = $ossClient->deleteObjects($bucket, array("b.file", "c.file"));foreach($result as $object)    Common::println($object);sleep(2);unlink("c.file.localcopy");// Normal upload and download speed limit$object= "b.file";$content = "hello world";// The speed limit is 100 KB/s, which is 819200 bit/s.$options = array(	OssClient::OSS_HEADERS => array(		OssClient::OSS_TRAFFIC_LIMIT => 819200,	));// Speed limit upload.$ossClient->putObject($bucket, $object, $content, $options);// Speed limit download.$ossClient->getObject($bucket, $object, $options);// Signed URL upload and download speed limit// Create a URL for uploading with a limited rate, and the validity period is 60s.$timeout = 60;$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);Common::println("b.file speed limit upload url:".$signedUrl.PHP_EOL);// Create a URL for speed-limited downloads, with a validity period of 120s.$timeout = 120;$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);Common::println("b.file speed limit download url:".$signedUrl.PHP_EOL);//******************************* For complete usage, see the following functions ****************************************************listObjects($ossClient, $bucket);listObjectsV2($ossClient, $bucket);listAllObjects($ossClient, $bucket);createObjectDir($ossClient, $bucket);putObject($ossClient, $bucket);uploadFile($ossClient, $bucket);getObject($ossClient, $bucket);getObjectToLocalFile($ossClient, $bucket);copyObject($ossClient, $bucket);modifyMetaForObject($ossClient, $bucket);getObjectMeta($ossClient, $bucket);deleteObject($ossClient, $bucket);deleteObjects($ossClient, $bucket);doesObjectExist($ossClient, $bucket);getSymlink($ossClient, $bucket);putSymlink($ossClient, $bucket);putObjectSpeed($ossClient, $bucket);getObjectSpeed($ossClient, $bucket);signUrlSpeedUpload($ossClient, $bucket);signUrlSpeedDownload($ossClient, $bucket);restoreObject($ossClient,$bucket);/** * Create a 'virtual' folder * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function createObjectDir($ossClient, $bucket){    try {        $ossClient->createObjectDir($bucket, "dir");    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * 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 putObject($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    $content = file_get_contents(__FILE__);    $options = array();    try {        $ossClient->putObject($bucket, $object, $content, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Uploads a local file to OSS * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function uploadFile($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    $filePath = __FILE__;    $options = array();    try {        $ossClient->uploadFile($bucket, $object, $filePath, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Lists all files and folders in the bucket.  * Note if there's more items than the max-keys specified, the caller needs to use the nextMarker returned as the value for the next call's maker paramter. * Loop through all the items returned from ListObjects. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function listObjects($ossClient, $bucket){    $prefix = 'oss-php-sdk-test/';    $delimiter = '/';    $nextMarker = '';    $maxkeys = 1000;    $options = array(        'delimiter' => $delimiter,        'prefix' => $prefix,        'max-keys' => $maxkeys,        'marker' => $nextMarker,    );    try {        $listObjectInfo = $ossClient->listObjects($bucket, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    $objectList = $listObjectInfo->getObjectList(); // object list    $prefixList = $listObjectInfo->getPrefixList(); // directory list    if (!empty($objectList)) {        print("objectList:\n");        foreach ($objectList as $objectInfo) {            print($objectInfo->getKey() . "\n");            if($objectInfo->getOwner() != null){                printf("owner id:".$objectInfo->getOwner()->getId() . "\n");                printf("owner name:".$objectInfo->getOwner()->getDisplayName() . "\n");            }        }    }    if (!empty($prefixList)) {        print("prefixList: \n");        foreach ($prefixList as $prefixInfo) {            print($prefixInfo->getPrefix() . "\n");        }    }}/** * Lists all files and folders in the bucket. * Note if there's more items than the max-keys specified, the caller needs to use the nextMarker returned as the value for the next call's maker paramter. * Loop through all the items returned from ListObjects. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function listObjectsV2($ossClient, $bucket){	$prefix = 'oss-php-sdk-test/';	$delimiter = '/';	$maxkeys = 1000;	$options = array(		'delimiter' => $delimiter,		'prefix' => $prefix,		'max-keys' => $maxkeys,		'start-after' =>'test-object',		'fetch-owner' =>'true',	);	try {		$listObjectInfo = $ossClient->listObjectsV2($bucket, $options);	} catch (OssException $e) {		printf(__FUNCTION__ . ": FAILED\n");		printf($e->getMessage() . "\n");		return;	}	print(__FUNCTION__ . ": OK" . "\n");	$objectList = $listObjectInfo->getObjectList(); // object list	$prefixList = $listObjectInfo->getPrefixList(); // directory list	if (!empty($objectList)) {		print("objectList:\n");		foreach ($objectList as $objectInfo) {			print($objectInfo->getKey() . "\n");			if($objectInfo->getOwner() != null){				printf("owner id:".$objectInfo->getOwner()->getId() . "\n");				printf("owner name:".$objectInfo->getOwner()->getDisplayName() . "\n");			}		}	}	if (!empty($prefixList)) {		print("prefixList: \n");		foreach ($prefixList as $prefixInfo) {			print($prefixInfo->getPrefix() . "\n");		}	}}/** * Lists all folders and files under the bucket. Use nextMarker repeatedly to get all objects. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function listAllObjects($ossClient, $bucket){    // Create dir/obj 'folder' and put some files into it.    for ($i = 0; $i < 100; $i += 1) {        $ossClient->putObject($bucket, "dir/obj" . strval($i), "hi");        $ossClient->createObjectDir($bucket, "dir/obj" . strval($i));    }    $prefix = 'dir/';    $delimiter = '/';    $nextMarker = '';    $maxkeys = 30;    while (true) {        $options = array(            'delimiter' => $delimiter,            'prefix' => $prefix,            'max-keys' => $maxkeys,            'marker' => $nextMarker,        );        var_dump($options);        try {            $listObjectInfo = $ossClient->listObjects($bucket, $options);        } catch (OssException $e) {            printf(__FUNCTION__ . ": FAILED\n");            printf($e->getMessage() . "\n");            return;        }        // Get the nextMarker, and it would be used as the next call's marker parameter to resume from the last call        $nextMarker = $listObjectInfo->getNextMarker();        $listObject = $listObjectInfo->getObjectList();        $listPrefix = $listObjectInfo->getPrefixList();        var_dump(count($listObject));        var_dump(count($listPrefix));        if ($nextMarker === '') {            break;        }    }}/** * Get the content of an object. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function getObject($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    $options = array();    try {        $content = $ossClient->getObject($bucket, $object, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    if (file_get_contents(__FILE__) === $content) {        print(__FUNCTION__ . ": FileContent checked OK" . "\n");    } else {        print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");    }}/** * Put symlink * * @param OssClient $ossClient  The Instance of OssClient * @param string $bucket bucket name * @return null */function putSymlink($ossClient, $bucket){    $symlink = "test-samples-symlink";    $object = "test-samples-object";    try {        $ossClient->putObject($bucket, $object, 'test-content');        $ossClient->putSymlink($bucket, $symlink, $object);        $content = $ossClient->getObject($bucket, $symlink);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    if ($content == 'test-content') {        print(__FUNCTION__ . ": putSymlink checked OK" . "\n");    } else {        print(__FUNCTION__ . ": putSymlink checked FAILED" . "\n");    }}/** * Get symlink * * @param OssClient $ossClient  OssClient instance * @param string $bucket  bucket name * @return null */function getSymlink($ossClient, $bucket){    $symlink = "test-samples-symlink";    $object = "test-samples-object";    try {        $ossClient->putObject($bucket, $object, 'test-content');        $ossClient->putSymlink($bucket, $symlink, $object);        $content = $ossClient->getSymlink($bucket, $symlink);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    if ($content[OssClient::OSS_SYMLINK_TARGET]) {        print(__FUNCTION__ . ": getSymlink checked OK" . "\n");    } else {        print(__FUNCTION__ . ": getSymlink checked FAILED" . "\n");    }}/** * Get_object_to_local_file * * Get object * Download object to a specified file. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function getObjectToLocalFile($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    $localfile = "upload-test-object-name.txt";    $options = array(        OssClient::OSS_FILE_DOWNLOAD => $localfile,    );    try {        $ossClient->getObject($bucket, $object, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");    if (file_get_contents($localfile) === file_get_contents(__FILE__)) {        print(__FUNCTION__ . ": FileContent checked OK" . "\n");    } else {        print(__FUNCTION__ . ": FileContent checked FAILED" . "\n");    }    if (file_exists($localfile)) {        unlink($localfile);    }}/** * Copy object * When the source object is same as the target one, copy operation will just update the metadata. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function copyObject($ossClient, $bucket){    $fromBucket = $bucket;    $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";    $toBucket = $bucket;    $toObject = $fromObject . '.copy';    $options = array();    try {        $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $options);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Update Object Meta * it leverages the feature of copyObject: when the source object is just the target object, the metadata could be updated via copy * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function modifyMetaForObject($ossClient, $bucket){    $fromBucket = $bucket;    $fromObject = "oss-php-sdk-test/upload-test-object-name.txt";    $toBucket = $bucket;    $toObject = $fromObject;    $copyOptions = array(        OssClient::OSS_HEADERS => array(            'Cache-Control' => 'max-age=60',            'Content-Disposition' => 'attachment; filename="xxxxxx"',        ),    );    try {        $ossClient->copyObject($fromBucket, $fromObject, $toBucket, $toObject, $copyOptions);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Get object meta, that is, getObjectMeta * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function getObjectMeta($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    try {        $objectMeta = $ossClient->getObjectMeta($bucket, $object);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    if (isset($objectMeta[strtolower('Content-Disposition')]) &&        'attachment; filename="xxxxxx"' === $objectMeta[strtolower('Content-Disposition')]    ) {        print(__FUNCTION__ . ": ObjectMeta checked OK" . "\n");    } else {        print(__FUNCTION__ . ": ObjectMeta checked FAILED" . "\n");    }}/** * Delete an object * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function deleteObject($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    try {        $ossClient->deleteObject($bucket, $object);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Delete multiple objects in batch * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function deleteObjects($ossClient, $bucket){    $objects = array();    $objects[] = "oss-php-sdk-test/upload-test-object-name.txt";    $objects[] = "oss-php-sdk-test/upload-test-object-name.txt.copy";    try {        $ossClient->deleteObjects($bucket, $objects);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");}/** * Check whether an object exists * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function doesObjectExist($ossClient, $bucket){    $object = "oss-php-sdk-test/upload-test-object-name.txt";    try {        $exist = $ossClient->doesObjectExist($bucket, $object);    } catch (OssException $e) {        printf(__FUNCTION__ . ": FAILED\n");        printf($e->getMessage() . "\n");        return;    }    print(__FUNCTION__ . ": OK" . "\n");    var_dump($exist);}/** * Speed limit upload. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function putObjectSpeed($ossClient, $bucket){	$object = "upload-test-object-name.txt";	$content = file_get_contents(__FILE__);	$options = array(		OssClient::OSS_HEADERS => array(			OssClient::OSS_TRAFFIC_LIMIT => 819200,		));	try {		$ossClient->putObject($bucket, $object, $content, $options);	} catch (OssException $e) {		printf(__FUNCTION__ . ": FAILED\n");		printf($e->getMessage() . "\n");		return;	}	print(__FUNCTION__ . ": OK" . "\n");}/** * Speed limit download. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function getObjectSpeed($ossClient, $bucket){	$object = "upload-test-object-name.txt";	$options = array(		OssClient::OSS_HEADERS => array(			OssClient::OSS_TRAFFIC_LIMIT => 819200,		));	try {		$ossClient->getObject($bucket, $object, $options);	} catch (OssException $e) {		printf(__FUNCTION__ . ": FAILED\n");		printf($e->getMessage() . "\n");		return;	}	print(__FUNCTION__ . ": OK" . "\n");}/** * Speed limit download. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function signUrlSpeedUpload($ossClient, $bucket){	$object = "upload-test-object-name.txt";	$timeout = 120;	$options = array(		OssClient::OSS_TRAFFIC_LIMIT => 819200,	);	$timeout = 60;	$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "PUT", $options);	print($signedUrl);}/** * Speed limit download. * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function signUrlSpeedDownload($ossClient, $bucket){	$object = "upload-test-object-name.txt";	$timeout = 120;	$options = array(		OssClient::OSS_TRAFFIC_LIMIT => 819200,	);	$signedUrl = $ossClient->signUrl($bucket, $object, $timeout, "GET", $options);	print($signedUrl);	print(__FUNCTION__ . ": OK" . "\n");}/** * Restore object * * @param OssClient $ossClient OssClient instance * @param string $bucket bucket name * @return null */function restoreObject($ossClient, $bucket){	$object = "oss-php-sdk-test/upload-test-object-name.txt";	$day = 3;	$tier = 'Expedited';	$config = new RestoreConfig($day,$tier);	$options = array(		OssClient::OSS_RESTORE_CONFIG => $config	);	try {		$ossClient->restoreObject($bucket, $object,$options);	} catch (OssException $e) {		printf(__FUNCTION__ . ": FAILED\n");		printf($e->getMessage() . "\n");		return;	}	print(__FUNCTION__ . ": OK" . "\n");}
 |