/** * 使用基本的api分阶段进行分片上传 * * @param OssClient $ossClient OssClient实例 * @param string $bucket 存储空间名称 * @throws OssException */ function putObjectByRawApis($ossClient, $bucket) { $object = "test/multipart-test.txt"; /** * step 1. 初始化一个分块上传事件, 也就是初始化上传Multipart, 获取upload id */ try { $uploadId = $ossClient->initiateMultipartUpload($bucket, $object); } catch (OssException $e) { printf(__FUNCTION__ . ": initiateMultipartUpload FAILED\n"); printf($e->getMessage() . "\n"); return; } print __FUNCTION__ . ": initiateMultipartUpload OK" . "\n"; /* * step 2. 上传分片 */ $partSize = 10 * 1024 * 1024; $uploadFile = __FILE__; $uploadFileSize = filesize($uploadFile); $pieces = $ossClient->generateMultiuploadParts($uploadFileSize, $partSize); $responseUploadPart = array(); $uploadPosition = 0; $isCheckMd5 = true; foreach ($pieces as $i => $piece) { $fromPos = $uploadPosition + (int) $piece[$ossClient::OSS_SEEK_TO]; $toPos = (int) $piece[$ossClient::OSS_LENGTH] + $fromPos - 1; $upOptions = array($ossClient::OSS_FILE_UPLOAD => $uploadFile, $ossClient::OSS_PART_NUM => $i + 1, $ossClient::OSS_SEEK_TO => $fromPos, $ossClient::OSS_LENGTH => $toPos - $fromPos + 1, $ossClient::OSS_CHECK_MD5 => $isCheckMd5); if ($isCheckMd5) { $contentMd5 = OssUtil::getMd5SumForFile($uploadFile, $fromPos, $toPos); $upOptions[$ossClient::OSS_CONTENT_MD5] = $contentMd5; } //2. 将每一分片上传到OSS try { $responseUploadPart[] = $ossClient->uploadPart($bucket, $object, $uploadId, $upOptions); } catch (OssException $e) { printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} FAILED\n"); printf($e->getMessage() . "\n"); return; } printf(__FUNCTION__ . ": initiateMultipartUpload, uploadPart - part#{$i} OK\n"); } $uploadParts = array(); foreach ($responseUploadPart as $i => $eTag) { $uploadParts[] = array('PartNumber' => $i + 1, 'ETag' => $eTag); } /** * step 3. 完成上传 */ try { $ossClient->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts); } catch (OssException $e) { printf(__FUNCTION__ . ": completeMultipartUpload FAILED\n"); printf($e->getMessage() . "\n"); return; } printf(__FUNCTION__ . ": completeMultipartUpload OK\n"); }
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 + (int) $piece[OssClient::OSS_SEEK_TO]; $to_pos = (int) $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); } }
/** * multipart上传统一封装,从初始化到完成multipart,以及出错后中止动作 * * @param string $bucket bucket名称 * @param string $object object名称 * @param string $file 需要上传的本地文件的路径 * @param array $options Key-Value数组 * @return null * @throws OssException */ public function multiuploadFile($bucket, $object, $file, $options = null) { $this->precheckCommon($bucket, $object, $options); if (isset($options[self::OSS_LENGTH])) { $options[self::OSS_CONTENT_LENGTH] = $options[self::OSS_LENGTH]; unset($options[self::OSS_LENGTH]); } if (empty($file)) { throw new OssException("parameter invalid, file is empty"); } $uploadFile = OssUtil::encodePath($file); if (!isset($options[self::OSS_CONTENT_TYPE])) { $options[self::OSS_CONTENT_TYPE] = $this->getMimeType($object, $uploadFile); } $upload_position = isset($options[self::OSS_SEEK_TO]) ? (int) $options[self::OSS_SEEK_TO] : 0; if (isset($options[self::OSS_CONTENT_LENGTH])) { $upload_file_size = (int) $options[self::OSS_CONTENT_LENGTH]; } else { $upload_file_size = filesize($uploadFile); if ($upload_file_size !== false) { $upload_file_size -= $upload_position; } } if ($upload_position === false || !isset($upload_file_size) || $upload_file_size === false || $upload_file_size < 0) { throw new OssException('The size of `fileUpload` cannot be determined in ' . __FUNCTION__ . '().'); } // 处理partSize if (isset($options[self::OSS_PART_SIZE])) { $options[self::OSS_PART_SIZE] = $this->computePartSize($options[self::OSS_PART_SIZE]); } else { $options[self::OSS_PART_SIZE] = self::OSS_MID_PART_SIZE; } $is_check_md5 = $this->isCheckMD5($options); // 如果上传的文件小于partSize,则直接使用普通方式上传 if ($upload_file_size < $options[self::OSS_PART_SIZE] && !isset($options[self::OSS_UPLOAD_ID])) { return $this->uploadFile($bucket, $object, $uploadFile, $options); } // 初始化multipart if (isset($options[self::OSS_UPLOAD_ID])) { $uploadId = $options[self::OSS_UPLOAD_ID]; } else { // 初始化 $uploadId = $this->initiateMultipartUpload($bucket, $object, $options); } // 获取的分片 $pieces = $this->generateMultiuploadParts($upload_file_size, (int) $options[self::OSS_PART_SIZE]); $response_upload_part = array(); foreach ($pieces as $i => $piece) { $from_pos = $upload_position + (int) $piece[self::OSS_SEEK_TO]; $to_pos = (int) $piece[self::OSS_LENGTH] + $from_pos - 1; $up_options = array(self::OSS_FILE_UPLOAD => $uploadFile, self::OSS_PART_NUM => $i + 1, self::OSS_SEEK_TO => $from_pos, self::OSS_LENGTH => $to_pos - $from_pos + 1, self::OSS_CHECK_MD5 => $is_check_md5); if ($is_check_md5) { $content_md5 = OssUtil::getMd5SumForFile($uploadFile, $from_pos, $to_pos); $up_options[self::OSS_CONTENT_MD5] = $content_md5; } $response_upload_part[] = $this->uploadPart($bucket, $object, $uploadId, $up_options); } $uploadParts = array(); foreach ($response_upload_part as $i => $etag) { $uploadParts[] = array('PartNumber' => $i + 1, 'ETag' => $etag); } return $this->completeMultipartUpload($bucket, $object, $uploadId, $uploadParts); }
public function testGetMd5SumForFile() { $this->assertEquals(OssUtil::getMd5SumForFile(__FILE__, 0, filesize(__FILE__) - 1), base64_encode(md5(file_get_contents(__FILE__), true))); }