This document provides sample code for quickly checking whether a bucket exists.
The sample code actually calls the HEAD Bucket COS API and is a simplified version of the API.
In addition to checking whether a bucket exists, HEAD Bucket
also checks whether you have permission to access the bucket. Possible scenarios are as follows:
This API is used to check whether a bucket exists.
public Guzzle\Service\Resource\Model doesBucketExist(array $args = array());
<?php
require dirname(__FILE__) . '/../vendor/autoload.php';
$secretId = "SECRETID"; //Replace it with the actual `SecretId`, which can be viewed and managed in the CAM console at https://console.intl.cloud.tencent.com/cam/capi
$secretKey = "SECRETKEY"; //Replace it with the actual `SecretKey`, which can be viewed and managed in the CAM console at https://console.intl.cloud.tencent.com/cam/capi
$region = "ap-beijing"; //Replace it with the actual `region`, which can be viewed in the console at https://console.intl.cloud.tencent.com/cos5/bucket
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', // Protocol header, which is http by default
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
try {
$result = $cosClient->doesBucketExist(
'examplebucket-125000000'// Bucket name in the format of `BucketName-APPID`, which can be viewed in the COS console at https://console.intl.cloud.tencent.com/cos5/bucket
); ;
// Request succeeded
print_r($result);
} catch (\Exception $e) {
// Request failed
echo($e);
}
Was this page helpful?