PHP code example of jerray / qcloud-cos-php-sdk
1. Go to this page and download the library: Download jerray/qcloud-cos-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jerray / qcloud-cos-php-sdk example snippets
$options = [
'appId' => 'Your app id',
'secretId' => 'Your secret id',
'secretKey' => 'Your secret key',
];
$cos = new jerray\QCloudCos\QCloudCos($options);
$localFilePath = '/path/to/a/local/file';
$bucketName = 'bucket';
$cosFilePath = '/remote/file/path';
$bizAttr = 'File attributes';
try {
$response = $cos->upload($localFilePath, $bucketName, $cosFilePath, $bizAttr);
$code = $response->code;
$fileUrl = $response->data->access_url;
} catch (jerray\QCloudCos\Exceptions\RequestException $e) {
$response = $e->getBody();
$httpMessage = $e->getMessage();
$httpCode = $e->getCode();
} catch (Exception $e) {
// ...
}
// response为最后一片的响应,与完整上传结构相同
$response = $cos->uploadSlice($localFilePath, $bucketName, $cosFilePath, $bizAttr);
$response = $cos->queryFile($bucketName, $cosFilePath);
$response = $cos->updateFile($bucketName, $cosFilePath, $bizAttr);
$response = $cos->deleteFile($bucketName, $cosFilePath);
$response = $cos->createFolder($bucketName, 'test/');
$limit = 20; // 每页列表数量
$pattern = 'both'; // 显示所有文件和目录 file - 只显示文件;foler - 只显示目录
// direction参数需要配合context参数使用
// context为空时始终取第一页,第一页返回的数据中会含有context参数
// 将此context值传入再次调用,即取到第二页
// direction用来控制翻页方向,next下一页,prev前一页
$context = '';
$direction = 'next';
// 取到第一页 返回当前context为第一页
$response = $cos->listFolder($bucketName, 'test/', $limit, $pattern, $context, $direction);
// next 向后翻页,取到第二页,返回当前context为第二页
$response = $cos->listFolder($bucketName, 'test/', $limit, $pattern, $response->data->context, 'next');
// prev 向前翻页,取到第一页,返回当前context为第一页
$response = $cos->listFolder($bucketName, 'test/', $limit, $pattern, $response->data->context, 'prev');
$response = $cos->updateFolder($bucketName, 'test/');
$response = $cos->queryFolder($bucketName, 'test/');
$response = $cos->deleteFolder($bucketName, 'test/');
composer