简介
本文档提供关于对象的上传相关的 API 概览以及 SDK 示例代码。
简单上传
分块上传
高级接口(推荐)
该小节主要讲述由 COS 提供的封装了上传和复制操作的高级接口,用户只需要设置相应的参数,该接口内部会根据文件大小决定是进行简单上传(复制)还是分块上传(复制),使用接口前请确认已完成了 快速入门 中指引的初始化步骤。
上传对象(断点续传)
方法原型
public Qcloud\Cos\Client upload(string $bucket, string $key, $body, array $options = array());
功能说明
该接口内部会根据文件大小,对小文件调用简单上传接口,对大文件调用分块上传接口。接口参数可参照PUT Object
和Upload Part
接口。
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
key |
String |
对象键 |
是 |
body |
Stream/String |
上传的内容 |
是 |
options |
Array |
附加的配置项 |
否 |
options 参数 |
类型 |
描述 |
是否必填 |
Progress |
Function |
进度条回调,参数为总大小($totalSize), 已上传大小($uploadedSize) |
否 |
PartSize |
Int |
最小分块文件大小,默认为5M |
否 |
Concurrency |
Int |
并发度,默认为10 |
否 |
ACL |
String |
设置对象的 ACL,例如 private、public-read |
否 |
CacheControl |
String |
缓存策略,设置 Cache-Control |
否 |
ContentDisposition |
String |
文件名称,设置 Content-Disposition |
否 |
ContentEncoding |
String |
编码格式,设置 Content-Encoding |
否 |
ContentLanguage |
String |
语言类型,设置 Content-Language |
否 |
ContentLength |
Int |
设置传输长度 |
否 |
ContentType |
String |
内容类型,设置 Content-Type |
否 |
Expires |
String |
设置 Content-Expires |
否 |
Metadata |
Array |
用户自定义的文件元信息 |
否 |
StorageClass |
String |
文件的存储类型,例如 STANDARD、STANDARD_IA、ARCHIVE,默认值:STANDARD。更多存储类型,请参见 存储类型概述 |
否 |
ContentMD5 |
Boolean |
是否自动生成上传文件的 MD5 值用于校验 |
否 |
ServerSideEncryption |
String |
服务端加密方法 |
否 |
请求示例
示例一:上传本地对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->upload(
$bucket = 'examplebucket-1250000000',
$key = 'exampleobject',
$body = fopen($local_path, 'rb')
);
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例二:上传归档对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->upload(
$bucket = 'examplebucket-1250000000',
$key = 'exampleobject',
$body = fopen($local_path, 'rb'),
$options = array(
'StorageClass' => 'Archive'
)
);
print_r($result);
} catch (\Exception $e) {
echo($e);
}
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->upload(
$bucket = 'examplebucket-1250000000',
$key = 'exampleobject',
$body = fopen($local_path, 'rb'),
$options = array(
'Metadata' => array(
'string' => 'string',
),
'PartSize' => 10 * 1024 * 1024
)
);
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例四:断点续传对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 10; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
$partSize = 1024 * 1024 + 1;
$rt = $this->cosClient->CreateMultipartUpload(array(
'Bucket' => $this->bucket,
'Key' => $this->key
));
$uploadId = $rt['UploadId'];
$this->cosClient->uploadPart(array(
'Bucket' => $this->bucket,
'Key' => $this->key,
'Body' => substr($body, 0, $partSize),
'UploadId' => $uploadId,
'PartNumber' => 1
));
$this->cosClient->resumeUpload(
$bucket=$this->bucket,
$this->key,
$body,
$uploadId,
array('PartSize'=>$partSize)
);
} catch (\Exception $e) {
echo($e);
}
批量上传(本地文件夹上传)
功能说明
将本地一个文件夹里所有的文件上传至 COS。
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
function uploadfiles( $path, $cosClient ) {
foreach ( scandir( $path ) as $afile ) {
if ( $afile == '.' || $afile == '..' ) continue;
if ( is_dir( $path.'/'.$afile ) ) {
uploadfiles( $path.'/'.$afile, $cosClient );
} else {
$local_file_path = $path.'/'.$afile;
$cos_file_path = $local_file_path;
try {
$cosClient->upload(
$bucket = 'examplebucket-1250000000',
$key = $cos_file_path,
$body = fopen( $cos_file_path, 'rb' )
);
} catch ( \Exception $e ) {
echo( $e );
}
}
}
}
$local_path = '/data/home/folder';
uploadfiles( $local_path, $cosClient );
简单上传
简单上传对象
功能说明
上传对象到指定的存储桶中(PUT Object),最大支持上传不超过5GB的对象,5GB以上对象请使用 分块上传 或 高级接口 上传。
方法原型
public Guzzle\Service\Resource\Model putObject(array $args = array())
请求示例
示例一:上传本地文件
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Body' => fopen($local_path, 'rb'),
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例二:上传归档文件
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Body' => fopen($local_path, 'rb'),
'StorageClass' => 'Archive'
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例三:上传指定 Content-type 的文件
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Body' => fopen($local_path, 'rb'),
'ContentType' => 'text/xml'
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例四:创建一个空的虚拟目录
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'folder/',
'Body' => "",
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例五:上传一个生成 ContentMD5 校验的本地文件
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Body' => fopen($local_path, 'rb'),
'ContentMD5' => true,
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例六:上传对象(单链接限速)
说明:
关于上传对象的限速说明,请参见 单链接限速。
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->putObject(array(
'Bucket' => 'examplebucket-125000000',
'Key' => 'exampleobject',
'Body' => fopen($local_path, 'rb'),
'TrafficLimit' => 8 * 1024 * 1024
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
否 |
ACL |
String |
设置对象的 ACL,例如 private、public-read |
否 |
Body |
Stream/String |
上传的内容 |
是 |
CacheControl |
String |
缓存策略,设置 Cache-Control |
否 |
ContentDisposition |
String |
文件名称,设置 Content-Disposition |
否 |
ContentEncoding |
String |
编码格式,设置 Content-Encoding |
否 |
ContentLanguage |
String |
语言类型,设置 Content-Language |
否 |
ContentLength |
Int |
设置传输长度 |
否 |
ContentType |
String |
内容类型,设置 Content-Type |
否 |
Expires |
String |
设置 Content-Expires |
否 |
Metadata |
Array |
用户自定义的文件元信息 |
否 |
StorageClass |
String |
文件的存储类型,例如 STANDARD、STANDARD_IA、ARCHIVE,默认值:STANDARD。更多存储类型,请参见 存储类型概述 |
否 |
ContentMD5 |
Boolean |
是否自动生成上传文件的 MD5 值用于校验 |
否 |
ServerSideEncryption |
String |
服务端加密方法 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[ETag] => "698d51a19d8a121ce581499d7b701668"
[VersionId] => MTg0NDUxODMyMTE2ODY0OTExOTk
[RequestId] => NWQwOGRkNDdfMjJiMjU4NjRfNzVjXzEwNmVjY2M=
[Location] => http:
[CRC] => 16749565679157681890
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
ETag |
String |
上传文件的 MD5 值 |
无 |
VersionId |
String |
开启版本控制后,文件的版本号 |
无 |
CRC |
String |
CRC64 检验码来进行 数据校验 |
无 |
追加上传对象
功能说明
将对象以分块追加的方式上传至存储桶(APPEND Object)。
方法原型
public Guzzle\Service\Resource\Model appendObject(array $args = array());
请求示例
示例一:追加上传输入流
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->appendObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Position' => 0,
'Body' => "hello,world",
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例二:追加上传本地文件流
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->appendObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Position' => 0,
'Body' => fopen($local_path, 'rb'),
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例三:多数据追加上传
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/Users/xxx/Desktop/exampleobject.txt";
try {
$result = $cosClient->appendObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Position' => 0,
'Body' => fopen($local_path, 'rb'),
));
$result = $cosClient->appendObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Position' => (integer)$result['Position'],
'Body' => "hello, world",
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名
examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
Position |
Integer |
追加操作的起始点。首次追加则设置 Position=0,后续追加则设置 Position 为当前 Object 的 content-length |
是 |
Body |
File/String |
上传分块的内容,可以为本地文件流或输入流 |
是 |
返回结果示例
GuzzleHttp\Command\Result Object
(
[ETag] => "9a74ded332531da4c295934ba5a9cf8b"
[Position] => 4
[RequestId] => NjExNWU4NTlfZDIyZjJjMGJfM2Q2ZV8xMzJjZThhZg==
[Key] => exampleobject
[Bucket] => examplebucket-1250000000
[Location] => examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/exampleobject
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
ETag |
String |
文件的 MD5 值 |
无 |
Position |
Integer |
追加操作的起始点 |
无 |
查询对象元数据
功能说明
查询 Object 的 Meta 信息(HEAD Object)。
方法原型
public Guzzle\Service\Resource\Model headObject(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->headObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名
examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
VersionId |
String |
开启版本控制后,指定文件的具体版本 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[DeleteMarker] =>
[AcceptRanges] =>
[Expiration] =>
[Restore] =>
[LastModified] => Tue, 02 Apr 2019 12:38:09 GMT
[ContentLength] => 238186
[ETag] => "af9f3b8eaf64473278909183abba1e31"
[MissingMeta] =>
[VersionId] =>
[CacheControl] =>
[ContentDisposition] =>
[ContentEncoding] =>
[ContentLanguage] =>
[ContentType] => text/plain; charset=utf-8
[Expires] =>
[ServerSideEncryption] =>
[Metadata] => Array
(
[md5] => af9f3b8eaf64473278909183abba1e31
)
[SSECustomerAlgorithm] =>
[SSECustomerKeyMD5] =>
[SSEKMSKeyId] =>
[StorageClass] =>
[RequestCharged] =>
[ReplicationStatus] =>
[RequestId] => NWNhMzU3Y2ZfMzFhYzM1MGFfODdhMF8xOTExM2U=
[CRC] => 16749565679157681890
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
CacheControl |
String |
缓存策略,设置 Cache-Control |
无 |
ContentDisposition |
String |
文件名称,设置 Content-Disposition |
无 |
ContentEncoding |
String |
编码格式,设置 Content-Encoding |
无 |
ContentLanguage |
String |
语言类型,设置 Content-Language |
无 |
ContentLength |
Int |
设置传输长度 |
无 |
ContentType |
String |
内容类型,设置 Content-Type |
无 |
Metadata |
Array |
用户自定义的文件元信息 |
无 |
StorageClass |
String |
文件的存储类型,例如 STANDARD、STANDARD_IA、ARCHIVE。更多存储类型请参见 存储类型概述 |
无 |
ServerSideEncryption |
String |
服务端加密方法 |
无 |
ETag |
String |
文件的 MD5 值 |
无 |
Restore |
String |
归档文件的回热信息 |
无 |
CRC |
String |
CRC64 检验码来进行 数据校验 |
无 |
设置对象复制
将一个对象复制到目标路径(PUT Object - Copy)。
方法原型
public Guzzle\Service\Resource\Model copyObject(array $args = array());
请求示例
示例一:复制对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->copyObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'CopySource' => 'sourcebucket-1250000000.cos.ap-guangzhou.myqcloud.com/sourceObject',
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例二:复制指定版本的对象
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->copyObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'CopySource' => 'sourcebucket-1250000000.cos.ap-guangzhou.myqcloud.com/sourceObject?versionId=MTg0NDUxNjI3NTM0ODE2Njc0MzU',
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
示例三:修改存储类型为归档
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->copyObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'CopySource' => 'sourcebucket-1250000000.cos.ap-guangzhou.myqcloud.com/sourceObject',
'StorageClass' => 'Archive'
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->copyObject(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'CopySource' => 'sourcebucket-1250000000.cos.ap-guangzhou.myqcloud.com/sourceObject',
'MetadataDirective' => 'Replaced',
'Metadata' => array(
'key1' => 'value1',
'key2' => 'value2',
)
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名
examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
CopySource |
String |
描述拷贝源文件的路径,包含 Appid、Bucket、Key、Region, 例如examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg |
是 |
MetadataDirective |
String |
可选值为 Copy、Replaced。设置为 Copy 时,忽略设置的用户元数据信息直接复制,设置为 Replaced 时,按设置的元信息修改元数据,当目标路径和源路径一样时,必须设置为 Replaced |
否 |
分块上传
分块上传对象可包括的操作:
- 分块上传对象: 初始化分块上传, 上传分块, 完成分块上传。
- 分块续传:查询已上传块, 上传分块,完成分块上传。
- 删除已上传分块。
查询分块上传
功能说明
查询指定存储桶中正在进行的分块上传(List Multipart Uploads)。
方法原型
public Guzzle\Service\Resource\Model listMultipartUploads(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->listMultipartUploads(array(
'Bucket' => 'examplebucket-1250000000',
'Delimiter' => '/',
'EncodingType' => 'url',
'KeyMarker' => 'prfixKeyMarker',
'UploadIdMarker' => 'string',
'Prefix' => 'prfix',
'MaxUploads' => 1000,
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Delimiter |
String |
默认为空,设置分隔符,例如设置/ 来模拟文件夹 |
否 |
EncodingType |
String |
默认不编码,规定返回值的编码方式,可选值:url |
否 |
KeyMarker |
String |
标记返回 parts 的 list 的起点位置 |
否 |
UploadIdMarker |
String |
标记返回 parts 的 list 的起点位置 |
否 |
Prefix |
String |
默认为空,对 parts 的 key 进行筛选,匹配指定前缀(prefix)的 objects |
否 |
MaxUploads |
Int |
最多返回的 parts 数量,默认为最大的1000 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Bucket] => examplebucket-1250000000
[EncodingType] =>
[KeyMarker] =>
[UploadIdMarker] =>
[MaxUploads] => 1000
[Prefix] =>
[IsTruncated] =>
[Uploads] => Array
(
[0] => Array
(
[Key] => exampleobject
[UploadId] => 1551693693b1e6d0e00eec30c534059865ec89c9393028b60bfaf167e9420524b25eeb2940
[Initiator] => Array
(
[ID] => qcs::cam::uin/100000000001:uin/100000000001
[DisplayName] => 100000000001
)
[Owner] => Array
(
[ID] => qcs::cam::uin/100000000001:uin/100000000001
[DisplayName] => 100000000001
)
[StorageClass] => STANDARD
[Initiated] => 2019-03-04T10:01:33.000Z
)
[1] => Array
(
[Key] => exampleobject
[UploadId] => 155374001100563fe0e9d37964d53077e54e9d392bce78f630359cd3288e62acee2b719534
[Initiator] => Array
(
[ID] => qcs::cam::uin/100000000001:uin/100000000001
[DisplayName] => 100000000001
)
[Owner] => Array
(
[ID] => qcs::cam::uin/100000000001:uin/100000000001
[DisplayName] => 100000000001
)
[StorageClass] => STANDARD
[Initiated] => 2019-03-28T02:26:51.000Z
)
)
[RequestId] => NWNhNDJmNzBfZWFhZDM1MGFfMjYyM2FfMWIyNzhh
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
无 |
IsTruncated |
Int |
表示返回的 objects 否被截断 |
无 |
Uploads |
Array |
返回的分块列表 |
无 |
Upload |
Array |
返回的分块属性 |
Uploads |
Key |
String |
对象键 |
Upload |
UploadId |
String |
对象的分块上传 ID |
Upload |
Initiator |
String |
初始化该分块的操作者 |
Upload |
Owner |
String |
分块拥有者 |
Upload |
StorageClass |
String |
分块的存储类型 |
Upload |
Initiated |
String |
分块初始化时间 |
Upload |
初始化分块上传
功能说明
初始化 Multipart Upload 上传操作(Initiate Multipart Upload)。
方法原型
public Guzzle\Service\Resource\Model createMultipartUpload(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->createMultipartUpload(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
此处的 Key 为对象键,对象键是对象在存储桶中的唯一标识。例如,在对象的访问域名
examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg 中,对象键为doc/pic.jpg |
是 |
CacheControl |
String |
缓存策略,设置 Cache-Control |
否 |
ContentDisposition |
String |
文件名称,设置 Content-Disposition |
否 |
ContentEncoding |
String |
编码格式,设置 Content-Encoding |
否 |
ContentLanguage |
String |
语言类型,设置 Content-Language |
否 |
ContentLength |
Int |
设置传输长度 |
否 |
ContentType |
String |
内容类型,设置 Content-Type |
否 |
Expires |
String |
设置 Content-Expires |
否 |
Metadata |
Array |
用户自定义的文件元信息 |
否 |
StorageClass |
String |
文件的存储类型,例如 STANDARD、STANDARD_IA、ARCHIVE,默认值:STANDARD。更多存储类型请参见 存储类型概述 |
否 |
ContentMD5 |
Boolean |
是否自动生成上传文件的 MD5 值用于校验 |
否 |
ServerSideEncryption |
String |
服务端加密方法 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Bucket] => examplebucket-1250000000
[Key] => exampleobject
[UploadId] => 1554277569b3e83df05c730104c325eb7b56000449fb7d51300b0728aacde02a6ea7f6c033
[RequestId] => NWNhNDY0YzFfMmZiNTM1MGFfNTM2YV8xYjliMTg=
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
无 |
Key |
String |
对象键 |
无 |
UploadId |
String |
对象分块上传的 ID |
无 |
上传分块
分块上传文件(Upload Part)。
方法原型
public Guzzle\Service\Resource\Model uploadPart(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->uploadPart(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'Body' => 'string',
'UploadId' => 'exampleUploadId',
'PartNumber' => 1,
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
对象键 |
是 |
UploadId |
String |
UploadId 为对象分块上传的 ID,在分块上传初始化的返回参数里获得 |
是 |
Body |
File/String |
上传的内容 |
是 |
PartNumber |
Int |
PartNumber 为分块的序列号,COS 会根据携带序列号合并分块 |
是 |
ContentLength |
Int |
设置传输长度 |
否 |
ContentMD5 |
Boolean |
是否自动生成上传文件的 MD5 值用于校验 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[ETag] => "96e79218965eb72c92a549dd5a330112"
[RequestId] => NWNhNDdjYWFfNjNhYjM1MGFfMjk2NF8xY2ViMWM=
[CRC] => 16749565679157681890
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
ETag |
String |
分块的 MD5 值 |
无 |
CRC |
String |
CRC64 检验码来进行 数据校验 |
无 |
查询已上传的分块
功能说明
查询特定分块上传操作中的已上传的块(List Parts)。
方法原型
public Guzzle\Service\Resource\Model listParts(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->listParts(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'UploadId' => 'exampleUploadId',
'PartNumberMarker' => 1,
'MaxParts' => 1000,
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
对象键 |
是 |
UploadId |
String |
对象分块上传的 ID |
是 |
PartNumberMarker |
Int |
标记返回 parts 的 list 的起点位置 |
否 |
MaxParts |
Int |
最多返回的 parts 数量,默认最大值为1000 |
否 |
返回结果示例
Guzzle\Service\Resource\Model Object
(
[structure:protected] =>
[data:protected] => Array
(
[Bucket] => examplebucket-1250000000
[Key] => exampleobject
[UploadId] => 1554279643cf19d71bb5fb0d29613e5541131f3a96387d9e168cd939c23a3d608c9eb94707
[Owner] => Array
(
[ID] => 1250000000
[DisplayName] => 1250000000
)
[PartNumberMarker] => 1
[Initiator] => Array
(
[ID] => qcs::cam::uin/100000000001:uin/100000000001
[DisplayName] => 100000000001
)
[StorageClass] => Standard
[MaxParts] => 1000
[IsTruncated] =>
[Parts] => Array
(
[0] => Array
(
[PartNumber] => 2
[LastModified] => 2019-04-03T08:21:28.000Z
[ETag] => "b948e77469189ac94b98e09755a6dba9"
[Size] => 1048576
)
[1] => Array
(
[PartNumber] => 3
[LastModified] => 2019-04-03T08:21:22.000Z
[ETag] => "9e5060e2994ec8463bfbebd442fdff16"
[Size] => 1048576
)
)
[RequestId] => NWNhNDZkNTJfOGNiMjM1MGFfMTRlYl8xYmJiOTU=
)
)
返回结果说明
参数名称 |
类型 |
描述 |
父节点 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
无 |
Key |
String |
对象键 |
无 |
UploadId |
String |
对象分块上传的 ID |
无 |
IsTruncated |
Int |
表示返回的 objects 否被截断 |
无 |
PartNumberMarker |
Int |
标记返回 parts 的 list 的起点位置 |
无 |
MaxParts |
Int |
最多返回的 parts 数量,默认最大值为1000 |
无 |
Initiator |
String |
初始化该分块的操作者 |
无 |
Parts |
Array |
返回的分块列表 |
无 |
Part |
Array |
返回的分块属性 |
Parts |
PartNumber |
Int |
分块标号 |
Part |
LastModified |
String |
分块的最后上传时间 |
Part |
ETag |
String |
分块的 MD5 值 |
Part |
Size |
String |
分块的大小 |
Part |
完成分块上传
功能说明
完成整个文件的分块上传(Complete Multipart Upload)。
方法原型
public Guzzle\Service\Resource\Model completeMultipartUpload(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->completeMultipartUpload(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'UploadId' => 'exampleUploadId',
'Parts' => array(
array(
'ETag' => 'exampleETag',
'PartNumber' => 1,
)),
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
对象键 |
是 |
UploadId |
String |
对象分块上传的 ID |
是 |
Parts |
Array |
分块信息列表 |
是 |
Part |
Array |
上传分块的内容信息 |
是 |
ETag |
String |
分块内容的 MD5 |
是 |
PartNumber |
Int |
分块编号 |
是 |
终止分块上传
功能说明
终止一个分块上传操作并删除已上传的块(Abort Multipart Upload)。
方法原型
public Guzzle\Service\Resource\Model abortMultipartUpload(array $args = array());
请求示例
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID";
$secretKey = "SECRETKEY";
$region = "ap-beijing";
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https',
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->abortMultipartUpload(array(
'Bucket' => 'examplebucket-1250000000',
'Key' => 'exampleobject',
'UploadId' => 'exampleUploadId',
));
print_r($result);
} catch (\Exception $e) {
echo($e);
}
参数说明
参数名称 |
类型 |
描述 |
是否必填 |
Bucket |
String |
存储桶名称,格式:BucketName-APPID |
是 |
Key |
String |
对象键 |
是 |
UploadId |
String |
对象分块上传的 ID |
是 |
本页内容是否解决了您的问题?