php -v command to view the current PHP version.php -m command to check whether the preceding extensions have been installed properly.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
vendor folder in the current directory. The folder will contain the SDK's dependent library and an autoload.php script file for future calls in your project.autoloader script to call cos-php-sdk-v5 and import the autoload.php file into your code.require '/path/to/sdk/vendor/autoload.php';
autoload.php file. Otherwise, the corresponding method cannot be called. For example, if your installation path is /Users/username/project, set the referenced path in the project to /Users/username/project/vendor/autoload.php.cos-sdk-v5-6.phar to use Guzzle 6.cos-sdk-v5-7.phar to use Guzzle 7.require '/path/to/cos-sdk-v5-x.phar';
cos-sdk-v5.tar.gz compressed file on the SDK releases page.cos-sdk-v5-6.tar.gz to use Guzzle 6.cos-sdk-v5-7.tar.gz to use Guzzle 7.autoload.php script to load the SDK and import the autoload.php file into your code:require '/path/to/sdk/vendor/autoload.php';
Source code is the default GitHub-compressed code package, which does not include the vendor directory. Note that you should download the release package (cos-sdk-v5-x.tar.gz) rather than the Source package, and should not clone the entire repository (otherwise, index.php and the vendor package will be missing).$tmpSecretId = "TmpSecretId"; //SecretId of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.$tmpSecretKey = "TmpSecretKey"; //SecretKey of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.$tmpToken = "TmpToken"; //Token of a temporary key. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048?from_cn_redirect=1.$region = "ap-beijing"; //Replace it with the actual `region`, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', //Protocol header, which is http by default'credentials'=> array('secretId' => $tmpSecretId,'secretKey' => $tmpSecretKey,'token' => $tmpToken)));
COSClient, you need to get your SecretId and SecretKey on the API Key Management page in the CAM console. A permanent key is suitable for most application scenarios.// You can log in to the CAM console to view and manage the `SecretId` and `SecretKey` of your project.$secretId = getenv('COS_SECRET_ID'); // User `SecretId`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.$secretKey = getenv('COS_SECRET_KEY'); // User `SecretKey`. We recommend you use a sub-account key and follow the principle of least privilege to reduce risks. For information about how to obtain a sub-account key, visit https://www.tencentcloud.com/document/product/598/32675.$region = "ap-beijing"; //User `region`, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.$cosClient = new Qcloud\\Cos\\Client(array('region' => $region,'schema' => 'https', // Protocol, which is `http` by default'credentials'=> array('secretId' => $secretId ,'secretKey' => $secretKey)));
schema parameter or enter 'schema' => 'http'. If you enter https, a certificate problem will be reported. If you want to configure an HTTPS certificate, see PHP SDK FAQs.try {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$result = $cosClient->createBucket(array('Bucket' => $bucket));// Request succeededprint_r($result);} catch (\\Exception $e) {// Request failedecho($e);}
try {// Request succeeded$result = $cosClient->listBuckets();print_r($result);} catch (\\Exception $e) {// Request failedecho($e);}
# Upload a file## putObject (an API that can upload files of up to 5 GB)### Uploading strings in memorytry {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$result = $cosClient->putObject(array('Bucket' => $bucket,'Key' => $key,'Body' => 'Hello World!'));print_r($result);} catch (\\Exception $e) {echo "$e\\n";}### Uploading a file streamtry {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$srcPath = "path/to/localFile";// Absolute path to 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, which can upload files of up to 50 TB with multipart upload)### Uploading strings in memorytry {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$result = $cosClient->Upload($bucket = $bucket,$key = $key,$body = 'Hello World!');print_r($result);} catch (\\Exception $e) {echo "$e\\n";}### Uploading a file streamtry {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$srcPath = "path/to/localFile";// Absolute path to 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";}
try {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$result = $cosClient->listObjects(array('Bucket' => $bucket));// Request succeededif (isset($result['Contents'])) {foreach ($result['Contents'] as $rt) {print_r($rt);}}} catch (\\Exception $e) {// Request failedecho($e);}
listObjects API can query up to 1,000 objects. If you want to query all objects, you need to call it repeatedly.try {$bucket = "examplebucket-1250000000"; // Bucket name in the format of BucketName-APPID$prefix = ''; // Prefix of the objects to be listed$marker = ''; // End marker in the last listwhile (true) {$result = $cosClient->listObjects(array('Bucket' => $bucket,'Marker' => $marker,'MaxKeys' => 1000 // Set the maximum number of entries to be listed in one single query, which is up to 1,000));if (isset($result['Contents'])) {foreach ($result['Contents'] as $rt) {// Print keyecho($rt['Key'] . "\\n");}}$marker = $result['NextMarker']; // Set a new end markerif (!$result['IsTruncated']) {break; // Determine whether the query is complete}}} catch (\\Exception $e) {echo($e);}
# Downloading a file## getObject (download file)### Download to memorytry {$bucket = "examplebucket-1250000000"; // Bucket in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key));// Request succeededecho($result['Body']);} catch (\\Exception $e) {// Request failedecho "$e\\n";}### Download to the local file systemtry {$bucket = "examplebucket-1250000000"; // Bucket in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$localPath = @"path/to/localFile";// Download to a specific local path$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key,'SaveAs' => $localPath));} catch (\\Exception $e) {// Request failedecho "$e\\n";}### Specifying the download range/** Range field is in the format of 'bytes=a-b'*/try {$bucket = "examplebucket-1250000000"; // Bucket in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$localPath = @"path/to/localFile";// Download to a specific local path$result = $cosClient->getObject(array('Bucket' => $bucket,'Key' => $key,'Range' => 'bytes=0-10','SaveAs' => $localPath));} catch (\\Exception $e) {// Request failedecho "$e\\n";}## getObjectUrl (get the file URL)try {$bucket = "examplebucket-1250000000"; // Bucket in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the object in the bucket$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');// Request succeededecho $signedUrl;} catch (\\Exception $e) {// Request failedprint_r($e);}
# Delete an object## deleteObjecttry {$bucket = "examplebucket-1250000000"; // Bucket in the format of BucketName-APPID$key = "exampleobject"; // Object key, the unique identifier of the 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 in the format of BucketName-APPID$key1 = "exampleobject1"; // Object key, the unique identifier of the object in the bucket$key2 = "exampleobject2"; // Object key, the unique identifier of the 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);}
Feedback