php -v command.php -m command.sudo apt-get install php-curl php-xml php-dom php-mbstring php-json
sudo yum install php-curl php-xml php-dom php-mbstring php-json
curl -sS https://getcomposer.org/installer | php
composer.json with the following content.{"require": {"qcloud/cos-sdk-v5": ">=2.0"}}
php composer.phar install
require '/path/to/sdk/vendor/autoload.php';
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: PHP 8.3.6 (cli) (built: Jul 14 2025 18:30:55) (NTS)
cos-sdk-v5-6.phar and use the Guzzle6 version.cos-sdk-v5-7.phar and use the Guzzle7 version.require '/path/to/cos-sdk-v5-x.phar';
cos-sdk-v5.tar.gz compressed file from the SDK download page.cos-sdk-v5-6.tar.gz and use the Guzzle6 version.cos-sdk-v5-7.tar.gz and use the Guzzle7 version.autoload.php script by including the autoload.php file in your code.require '/path/to/sdk/vendor/autoload.php';
Source code archive is the code package packaged by GitHub by default and does not contain the vendor directory. Please download the release package (the cos-sdk-v5-x.tar.gz package) instead of the Source package, and do not clone the entire repository directly. Otherwise, the index.php file and the vendor package will be missing.$tmpSecretId = "TmpSecretId"; // SecretId of the temporary key. For guidance on generating and using temporary keys, see https://www.tencentcloud.com/document/product/436/14048$tmpSecretKey = "TmpSecretKey"; // SecretKey of the temporary key. For guidance on generating and using temporary keys, see https://www.tencentcloud.com/document/product/436/14048$tmpToken = "TmpToken"; // Token of the temporary key. For guidance on generating and using temporary keys, see https://www.tencentcloud.com/document/product/436/14048$region = "ap-beijing"; // Replace with your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $tmpSecretId,'secretKey' => $tmpSecretKey,'token' => $tmpToken)));
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: PHP 8.3.6 (cli) (built: Jul 14 2025 18:30:55) (NTS)
// Log in to the CAM console to view and manage SECRETID and SECRETKEY.$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = "ap-beijing"; // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));
'scheme' => 'http'. If you enter https, a certificate problem will occur. If you need to configure a certificate, see PHP SDK FAQ.require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$result = $cosClient->createBucket(array('Bucket' => $bucket));//Request succeededprint_r($result);} catch (\\Exception $e) {//Request failedecho($e);}
require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));try {//Request succeeded$result = $cosClient->listBuckets();// Print bucket list informationif (isset($result['Buckets'])) {foreach ($result['Buckets'] as $bucket) {echo "BucketName: " . $bucket['Name'] . "\\n";echo "Location: " . $bucket['Location'] . "\\n";echo "CreationDate: " . $bucket['CreationDate'] . "\\n";echo "---\\n";}}} catch (\\Exception $e) {//Request failedecho($e);}
require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));# Upload documents.## putObject (upload API, supporting files up to 5 GB).### Upload strings in the memory.try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$result = $cosClient->putObject(array('Bucket' => $bucket,'Key' => $key,'Body' => 'Hello World!'));print_r($result);} catch (\\Exception $e) {echo "$e\\n";}### Upload file streams.try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$srcPath = "path/to/localFile"; // absolute path of the local file$file = fopen($srcPath, "rb");if ($file) {$result = $cosClient->putObject(array('Bucket' => $bucket,'Key' => $key,'Body' => $file));print_r($result);}} catch (\\Exception $e) {echo "$e\\n";}## Upload (advanced upload API. By default, multipart upload is used, and files up to 50 TB are supported.)### Upload strings in the memory.try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$result = $cosClient->Upload($bucket = $bucket,$key = $key,$body = 'Hello World!');print_r($result);} catch (\\Exception $e) {echo "$e\\n";}### Upload file streams.try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$srcPath = "path/to/localFile"; // absolute path of the local file$file = fopen($srcPath, 'rb');if ($file) {$result = $cosClient->Upload($bucket = $bucket,$key = $key,$body = $file);}print_r($result);} catch (\\Exception $e) {echo "$e\\n";}
require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$result = $cosClient->listObjects(array('Bucket' => $bucket));// Request succeededif (isset($result['Contents'])) {foreach ($result['Contents'] as $rt) {// print the object's keyecho "Key: " . $rt['Key'] . "\\n";// print the object's ETagecho "ETag: " . $rt['ETag'] . "\\n";// print the object's sizeecho "Size: " . $rt['Size'] . "\\n";echo "---\\n";}}} catch (\\Exception $e) {//Request failedecho($e);}
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: PHP 8.3.6 (cli) (built: Jul 14 2025 18:30:55) (NTS)
require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));// To obtain more objects than the maxkeys limit or to obtain all objects, you need to call listObjects repeatedly.// Use the NextMarker returned last time as the Marker for the next call until the returned IsTruncated is false.try {$bucket = "examplebucket-1250000000"; // Bucket name format: BucketName-APPID.$prefix = ''; // Prefix of the list object$marker = ''; // The breakpoint where the object was last listedwhile (true) {$result = $cosClient->listObjects(array('Bucket' => $bucket,'Marker' => $marker,'MaxKeys' => 1000 // Set the maximum quantities of prints in a single query to 1,000.));if (isset($result['Contents'])) {foreach ($result['Contents'] as $rt) {// print keyecho($rt['Key'] . "\\n");}}$marker = $result['NextMarker']; // Set a new breakpoint.if (!$result['IsTruncated']) {break; // Judge whether the query has been completed or not.}}} catch (\\Exception $e) {echo($e);}
require dirname(__FILE__) . '/../vendor/autoload.php';$secretId = getenv('COS_SECRET_ID'); // User's SecretId. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$secretKey = getenv('COS_SECRET_KEY'); // User's SecretKey. It is recommended to use a sub-account key; authorization follows the principle of least privilege to reduce usage risks. For obtaining sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1$region = getenv('COS_REGION'); // Your region. The region of the created bucket can be viewed in the console: https://console.tencentcloud.com/cos5/bucket$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'scheme' => 'https', // Protocol header, which defaults to http'credentials'=> array('secretId' => $secretId,'secretKey' => $secretKey)));# Download files.## getObject (Download files).### Download files to the memory.try {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key));// Request succeededecho($result['Body']);} catch (\\Exception $e) {//Request failedecho "$e\\n";}### Download files to a local directory.try {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$localPath = @"path/to/localFile"; // Download to the specified local path.$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key,'SaveAs' => $localPath));} catch (\\Exception $e) {//Request failedecho "$e\\n";}### Specify the download range./** The format of the Range field is 'bytes=ab'.*/try {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$localPath = @"path/to/localFile"; // Download to the specified local path.$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key,'Range' => 'bytes=0-10','SaveAs' => $localPath));} catch (\\Exception $e) {//Request failedecho "$e\\n";}## getObjectUrl (Obtain the file UrL).try {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');// Request succeededecho $signedUrl;} catch (\\Exception $e) {//Request failedprint_r($e);}
# Delete object## deleteObjecttry {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key = "exampleobject"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$result = $cosClient->deleteObject(array('Bucket' => $bucket,'Key' => $key,'VersionId' => 'string'));// Request succeededprint_r($result);} catch (\\Exception $e) {//Request failedecho($e);}# Delete multiple objects## deleteObjectstry {$bucket = "examplebucket-1250000000"; //bucket, format: BucketName-APPID$key1 = "exampleobject1"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$key2 = "exampleobject2"; // Here, key is the object key, which is the unique identifier of an object in the bucket.$result = $cosClient->deleteObjects(array('Bucket' => $bucket,'Objects' => array(array('Key' => $key1,),array('Key' => $key2,),//...),));// Request succeededprint_r($result);} catch (\\Exception $e) {//Request failedecho($e);}
フィードバック