123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- namespace OSS\Tests;
- use OSS\Core\OssException;
- use OSS\OssClient;
- use OSS\Core\OssUtil;
- require_once __DIR__ . DIRECTORY_SEPARATOR . 'TestOssClientBase.php';
- class OssClientMultipartUploadTest extends TestOssClientBase
- {
- public function testInvalidDir()
- {
- try {
- $this->ossClient->uploadDir($this->bucket, "", "abc/ds/s/s/notexitst");
- $this->assertFalse(true);
- } catch (OssException $e) {
- $this->assertEquals("parameter error: abc/ds/s/s/notexitst is not a directory, please check it", $e->getMessage());
- }
- }
- public function testMultipartUploadBigFile()
- {
- $bigFileName = __DIR__ . DIRECTORY_SEPARATOR . "/bigfile.tmp";
- $localFilename = __DIR__ . DIRECTORY_SEPARATOR . "/localfile.tmp";
- OssUtil::generateFile($bigFileName, 6 * 1024 * 1024);
- $object = 'mpu/multipart-bigfile-test.tmp';
- try {
- $this->ossClient->multiuploadFile($this->bucket, $object, $bigFileName, array(OssClient::OSS_PART_SIZE => 1));
- $options = array(OssClient::OSS_FILE_DOWNLOAD => $localFilename);
- $this->ossClient->getObject($this->bucket, $object, $options);
- $this->assertEquals(md5_file($bigFileName), md5_file($localFilename));
- } catch (OssException $e) {
- var_dump($e->getMessage());
- $this->assertFalse(true);
- }
- unlink($bigFileName);
- unlink($localFilename);
- }
-
- public function testMultipartUploadBigFileWithMD5Check()
- {
- $bigFileName = __DIR__ . DIRECTORY_SEPARATOR . "/bigfile.tmp";
- $localFilename = __DIR__ . DIRECTORY_SEPARATOR . "/localfile.tmp";
- OssUtil::generateFile($bigFileName, 6 * 1024 * 1024);
- $object = 'mpu/multipart-bigfile-test.tmp';
- $options = array(
- OssClient::OSS_CHECK_MD5 => true,
- OssClient::OSS_PART_SIZE => 1,
- );
- try {
- $this->ossClient->multiuploadFile($this->bucket, $object, $bigFileName, $options);
- $options = array(OssClient::OSS_FILE_DOWNLOAD => $localFilename);
- $this->ossClient->getObject($this->bucket, $object, $options);
- $this->assertEquals(md5_file($bigFileName), md5_file($localFilename));
- } catch (OssException $e) {
- var_dump($e->getMessage());
- $this->assertFalse(true);
- }
- unlink($bigFileName);
- unlink($localFilename);
- }
- public function testCopyPart()
- {
- $object = "mpu/multipart-test.txt";
- $copiedObject = "mpu/multipart-test.txt.copied";
- $this->ossClient->putObject($this->bucket, $copiedObject, file_get_contents(__FILE__));
- /**
- * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
- */
- try {
- $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- /*
- * step 2. uploadPartCopy
- */
- $copyId = 1;
- $eTag = $this->ossClient->uploadPartCopy($this->bucket, $copiedObject, $this->bucket, $object, $copyId, $upload_id);
- $upload_parts[] = array(
- 'PartNumber' => $copyId,
- 'ETag' => $eTag,
- );
- try {
- $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
- $this->assertNotNull($listPartsInfo);
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- /**
- * step 3.
- */
- try {
- $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts);
- } catch (OssException $e) {
- var_dump($e->getMessage());
- $this->assertTrue(false);
- }
- $this->assertEquals($this->ossClient->getObject($this->bucket, $object), file_get_contents(__FILE__));
- $this->assertEquals($this->ossClient->getObject($this->bucket, $copiedObject), file_get_contents(__FILE__));
- }
- public function testAbortMultipartUpload()
- {
- $object = "mpu/multipart-test.txt";
- /**
- * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
- */
- try {
- $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- /*
- * step 2. 上传分片
- */
- $part_size = 10 * 1024 * 1024;
- $upload_file = __FILE__;
- $upload_filesize = filesize($upload_file);
- $pieces = $this->ossClient->generateMultiuploadParts($upload_filesize, $part_size);
- $response_upload_part = array();
- $upload_position = 0;
- $is_check_md5 = true;
- foreach ($pieces as $i => $piece) {
- $from_pos = $upload_position + (integer)$piece[OssClient::OSS_SEEK_TO];
- $to_pos = (integer)$piece[OssClient::OSS_LENGTH] + $from_pos - 1;
- $up_options = array(
- OssClient::OSS_FILE_UPLOAD => $upload_file,
- OssClient::OSS_PART_NUM => ($i + 1),
- OssClient::OSS_SEEK_TO => $from_pos,
- OssClient::OSS_LENGTH => $to_pos - $from_pos + 1,
- OssClient::OSS_CHECK_MD5 => $is_check_md5,
- );
- if ($is_check_md5) {
- $content_md5 = OssUtil::getMd5SumForFile($upload_file, $from_pos, $to_pos);
- $up_options[OssClient::OSS_CONTENT_MD5] = $content_md5;
- }
- //2. 将每一分片上传到OSS
- try {
- $response_upload_part[] = $this->ossClient->uploadPart($this->bucket, $object, $upload_id, $up_options);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- $upload_parts = array();
- foreach ($response_upload_part as $i => $eTag) {
- $upload_parts[] = array(
- 'PartNumber' => ($i + 1),
- 'ETag' => $eTag,
- );
- }
- try {
- $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
- $this->assertNotNull($listPartsInfo);
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- $this->assertEquals(1, count($listPartsInfo->getListPart()));
- $numOfMultipartUpload1 = 0;
- $options = null;
- try {
- $listMultipartUploadInfo = $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
- $this->assertNotNull($listMultipartUploadInfo);
- $numOfMultipartUpload1 = count($listMultipartUploadInfo->getUploads());
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- try {
- $this->ossClient->abortMultipartUpload($this->bucket, $object, $upload_id);
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- $numOfMultipartUpload2 = 0;
- try {
- $listMultipartUploadInfo = $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
- $this->assertNotNull($listMultipartUploadInfo);
- $numOfMultipartUpload2 = count($listMultipartUploadInfo->getUploads());
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- $this->assertEquals($numOfMultipartUpload1 - 1, $numOfMultipartUpload2);
- }
- public function testPutObjectByRawApis()
- {
- $object = "mpu/multipart-test.txt";
- /**
- * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id
- */
- try {
- $upload_id = $this->ossClient->initiateMultipartUpload($this->bucket, $object);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- /*
- * step 2. 上传分片
- */
- $part_size = 10 * 1024 * 1024;
- $upload_file = __FILE__;
- $upload_filesize = filesize($upload_file);
- $pieces = $this->ossClient->generateMultiuploadParts($upload_filesize, $part_size);
- $response_upload_part = array();
- $upload_position = 0;
- $is_check_md5 = true;
- foreach ($pieces as $i => $piece) {
- $from_pos = $upload_position + (integer)$piece[OssClient::OSS_SEEK_TO];
- $to_pos = (integer)$piece[OssClient::OSS_LENGTH] + $from_pos - 1;
- $up_options = array(
- OssClient::OSS_FILE_UPLOAD => $upload_file,
- OssClient::OSS_PART_NUM => ($i + 1),
- OssClient::OSS_SEEK_TO => $from_pos,
- OssClient::OSS_LENGTH => $to_pos - $from_pos + 1,
- OssClient::OSS_CHECK_MD5 => $is_check_md5,
- );
- if ($is_check_md5) {
- $content_md5 = OssUtil::getMd5SumForFile($upload_file, $from_pos, $to_pos);
- $up_options[OssClient::OSS_CONTENT_MD5] = $content_md5;
- }
- //2. 将每一分片上传到OSS
- try {
- $response_upload_part[] = $this->ossClient->uploadPart($this->bucket, $object, $upload_id, $up_options);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- $upload_parts = array();
- foreach ($response_upload_part as $i => $eTag) {
- $upload_parts[] = array(
- 'PartNumber' => ($i + 1),
- 'ETag' => $eTag,
- );
- }
- try {
- $listPartsInfo = $this->ossClient->listParts($this->bucket, $object, $upload_id);
- $this->assertNotNull($listPartsInfo);
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- /**
- * step 3.
- */
- try {
- $this->ossClient->completeMultipartUpload($this->bucket, $object, $upload_id, $upload_parts);
- } catch (OssException $e) {
- $this->assertTrue(false);
- }
- }
- function testPutObjectsByDir()
- {
- $localDirectory = dirname(__FILE__);
- $prefix = "samples/codes";
- try {
- $this->ossClient->uploadDir($this->bucket, $prefix, $localDirectory);
- } catch (OssException $e) {
- var_dump($e->getMessage());
- $this->assertFalse(true);
- }
- $this->assertTrue($this->ossClient->doesObjectExist($this->bucket, 'samples/codes/' . "OssClientMultipartUploadTest.php"));
- }
- public function testPutObjectByMultipartUpload()
- {
- $object = "mpu/multipart-test.txt";
- $file = __FILE__;
- $options = array();
- try {
- $this->ossClient->multiuploadFile($this->bucket, $object, $file, $options);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
-
- public function testPutObjectByMultipartUploadWithMD5Check()
- {
- $object = "mpu/multipart-test.txt";
- $file = __FILE__;
- $options = array(OssClient::OSS_CHECK_MD5 => true);
-
- try {
- $this->ossClient->multiuploadFile($this->bucket, $object, $file, $options);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- public function testListMultipartUploads()
- {
- $options = null;
- try {
- $listMultipartUploadInfo = $this->ossClient->listMultipartUploads($this->bucket, $options);
- $this->assertNotNull($listMultipartUploadInfo);
- } catch (OssException $e) {
- $this->assertFalse(true);
- }
- }
- }
|