PHP code example of chinac / cos-php-sdk
1. Go to this page and download the library: Download chinac/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/ */
chinac / cos-php-sdk example snippets
$accessKeyId = "<您从COS获得的AccessKeyId>"; ;
$accessKeySecret = "<您从COS获得的AccessKeySecret>";
$endpoint = "<您选定的COS数据中心访问域名,例如cos-cn-hangzhou.chinac.com>";
try {
$cosClient = new CosClient($accessKeyId, $accessKeySecret, $endpoint);
} catch (CosException $e) {
print $e->getMessage();
}
$bucket = "<您使用的Bucket名字,注意命名规范>";
$object = "<您使用的Object名字,注意命名规范>";
$content = "Hello, COS!"; // 上传的文件内容
try {
$cosClient->putObject($bucket, $object, $content);
} catch (CosException $e) {
print $e->getMessage();
}
$bucket = "<您使用的Bucket名字,注意命名规范>";
try {
$cosClient->createBucket($bucket);
} catch (CosException $e) {
print $e->getMessage();
}
$bucketListInfo = $cosClient->listBuckets();
$bucketList = $bucketListInfo->getBucketList();
foreach($bucketList as $bucket) {
print($bucket->getLocation() . "\t" . $bucket->getName() . "\t" . $bucket->getCreatedate() . "\n");
}