/**
  * 解析从ListMultipartUpload接口的返回数据
  *
  * @return ListMultipartUploadInfo
  */
 protected function parseDataFromResponse()
 {
     $content = $this->rawResponse->body;
     $xml = simplexml_load_string($content);
     $encodingType = isset($xml->EncodingType) ? strval($xml->EncodingType) : "";
     $bucket = isset($xml->Bucket) ? strval($xml->Bucket) : "";
     $keyMarker = isset($xml->KeyMarker) ? strval($xml->KeyMarker) : "";
     $keyMarker = OssUtil::decodeKey($keyMarker, $encodingType);
     $uploadIdMarker = isset($xml->UploadIdMarker) ? strval($xml->UploadIdMarker) : "";
     $nextKeyMarker = isset($xml->NextKeyMarker) ? strval($xml->NextKeyMarker) : "";
     $nextKeyMarker = OssUtil::decodeKey($nextKeyMarker, $encodingType);
     $nextUploadIdMarker = isset($xml->NextUploadIdMarker) ? strval($xml->NextUploadIdMarker) : "";
     $delimiter = isset($xml->Delimiter) ? strval($xml->Delimiter) : "";
     $delimiter = OssUtil::decodeKey($delimiter, $encodingType);
     $prefix = isset($xml->Prefix) ? strval($xml->Prefix) : "";
     $prefix = OssUtil::decodeKey($prefix, $encodingType);
     $maxUploads = isset($xml->MaxUploads) ? intval($xml->MaxUploads) : 0;
     $isTruncated = isset($xml->IsTruncated) ? strval($xml->IsTruncated) : "";
     $listUpload = array();
     if (isset($xml->Upload)) {
         foreach ($xml->Upload as $upload) {
             $key = isset($upload->Key) ? strval($upload->Key) : "";
             $key = OssUtil::decodeKey($key, $encodingType);
             $uploadId = isset($upload->UploadId) ? strval($upload->UploadId) : "";
             $initiated = isset($upload->Initiated) ? strval($upload->Initiated) : "";
             $listUpload[] = new UploadInfo($key, $uploadId, $initiated);
         }
     }
     return new ListMultipartUploadInfo($bucket, $keyMarker, $uploadIdMarker, $nextKeyMarker, $nextUploadIdMarker, $delimiter, $prefix, $maxUploads, $isTruncated, $listUpload);
 }
Пример #2
0
 /**
  * 工具方法,创建一个存储空间,如果发生异常直接exit
  */
 public static function createBucket()
 {
     $ossClient = self::getOssClient();
     if (is_null($ossClient)) {
         exit(1);
     }
     $bucket = self::getBucketName();
     $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
     try {
         $ossClient->createBucket($bucket, $acl);
     } catch (OssException $e) {
         $message = $e->getMessage();
         if (\OSS\Core\OssUtil::startsWith($message, 'http status: 403')) {
             echo "Please Check your AccessKeyId and AccessKeySecret" . "\n";
             exit(0);
         } elseif (strpos($message, "BucketAlreadyExists") !== false) {
             echo "Bucket already exists. Please check whether the bucket belongs to you, or it was visited with correct endpoint. " . "\n";
             exit(0);
         }
         printf(__FUNCTION__ . ": FAILED\n");
         printf($e->getMessage() . "\n");
         return;
     }
     print __FUNCTION__ . ": OK" . "\n";
 }
/**
 * 使用基本的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");
}
 private function parsePrefixList($xml, $encodingType)
 {
     $retList = array();
     if (isset($xml->CommonPrefixes)) {
         foreach ($xml->CommonPrefixes as $commonPrefix) {
             $prefix = isset($commonPrefix->Prefix) ? strval($commonPrefix->Prefix) : "";
             $prefix = OssUtil::decodeKey($prefix, $encodingType);
             $retList[] = new PrefixInfo($prefix);
         }
     }
     return $retList;
 }
 public function testBucket()
 {
     $this->ossClient->createBucket($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
     $bucketListInfo = $this->ossClient->listBuckets();
     $this->assertNotNull($bucketListInfo);
     $bucketList = $bucketListInfo->getBucketList();
     $this->assertTrue(is_array($bucketList));
     $this->assertGreaterThan(0, count($bucketList));
     $this->ossClient->putBucketAcl($this->bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
     $this->assertEquals($this->ossClient->getBucketAcl($this->bucket), OssClient::OSS_ACL_TYPE_PUBLIC_READ_WRITE);
     $this->assertTrue($this->ossClient->doesBucketExist($this->bucket));
     $this->assertFalse($this->ossClient->doesBucketExist($this->bucket . '-notexist'));
     try {
         $this->ossClient->deleteBucket($this->bucket);
     } catch (OssException $e) {
         $this->assertTrue(OssUtil::startsWith($e->getMessage(), "http status: 409"));
     }
 }
Пример #6
0
 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);
     }
 }
Пример #7
0
 /**
  * 检查endpoint的种类
  * 如有有协议头,剥去协议头
  * 并且根据参数 is_cname 和endpoint本身,判定域名类型,是ip,cname,还是专有域或者官网域名
  *
  * @param string $endpoint
  * @param boolean $isCName
  * @return string 剥掉协议头的域名
  */
 private function checkEndpoint($endpoint, $isCName)
 {
     $ret_endpoint = null;
     if (strpos($endpoint, 'http://') === 0) {
         $ret_endpoint = substr($endpoint, strlen('http://'));
     } elseif (strpos($endpoint, 'https://') === 0) {
         $ret_endpoint = substr($endpoint, strlen('https://'));
         $this->useSSL = true;
     } else {
         $ret_endpoint = $endpoint;
     }
     if ($isCName) {
         $this->hostType = self::OSS_HOST_TYPE_CNAME;
     } elseif (OssUtil::isIPFormat($ret_endpoint)) {
         $this->hostType = self::OSS_HOST_TYPE_IP;
     } else {
         $this->hostType = self::OSS_HOST_TYPE_NORMAL;
     }
     return $ret_endpoint;
 }
Пример #8
0
 public function testThrowOssExceptionWithMessageIfEmpty3()
 {
     $null = "xx";
     try {
         OssUtil::throwOssExceptionWithMessageIfEmpty($null, "xx");
         $this->assertTrue(True);
     } catch (OssException $e) {
         $this->assertTrue(false);
     }
 }