分块上传预签名Demo

2018-05-07  本文已影响0人  lewzylu

分块上传预签名Demo

<?php

require(__DIR__ . DIRECTORY_SEPARATOR . 'cos-autoloader.php');

$cosClient = new Qcloud\Cos\Client(array('region' => getenv('COS_REGION'),
    'credentials'=> array(
        'secretId'    => '',
        'secretKey' => '')));

//bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
$bucket = 'lewzylu02-1252448703';
$key = 'a.txt';
$local_path = "E:/a.txt";
$uploadId = "111";

//初始化分块上传获取uploadId
try {
    $result = $cosClient->createMultipartUpload(array(
        'Bucket' => $bucket,
        'Key' => $key,
    ));
    print_r($result);
    $uploadId = $result['UploadId'];
} catch (\Exception $e) {
    echo "$e\n";
}

// 获取分块上传预签名
try {
    #此处可以替换为其他上传接口
    $command = $cosClient->getCommand('uploadPart', array(
        'Bucket' => $bucket,
        'Key' => $key,
        'UploadId' => $uploadId,
        'PartNumber' => '1',
        'Body' => ''
    ));
    $signedUrl = $command->createPresignedUrl('+10 minutes');
    echo ($signedUrl);
} catch (\Exception $e) {
    echo "$e\n";
}
# completeMultipartUpload接口
$rt = $cosClient->completeMultipartUpload(array(
            'Bucket' => $bucket,
            'Key' => $key,
            'UploadId' => $uploadId,
            'Parts' => array(
        array(
            'ETag' => 'string',
            'PartNumber' => integer,
        ),
        // ... repeated
    )));
# ListParts 接口
try {
    $result = $cosClient->ListParts(array(
        'Bucket' => $bucket,
        'Key' => $key,
        'UploadId'=> $uploadId
    ));
    print_r($result);
} catch (\Exception $e) {
    echo "$e\n";
}
上一篇下一篇

猜你喜欢

热点阅读