2

我想检查是否存在特定的存储桶,如果不创建新存储桶。

我需要知道如何使用 aws.phar + php 检测存储桶是否已存在

这是伪代码

$bucket = 'my-bucket';

//some code to detect if bucket name 'my-bucket' exists *suggest here*
//$myBucketExists = $client->someMethode()

if(!$myBucketExists){

      $result = $client->createBucket(array(
         'Bucket' => $bucket
      ));

      // Wait until the bucket is created
      $client->waitUntil('BucketExists', array('Bucket' => $bucket));
}
// rest of the code using the bucket
4

2 回答 2

5

使用headBucket()

$myBucketExists = $client->headBucket( array('Bucket' => $bucket) );
于 2013-08-05T13:52:34.663 回答
1

尝试这个:

$s3 = $this->awsService->get('S3');

if(!$s3->doesBucketExist($bucket)) {
    $s3->createBucket(array(
        'Bucket' => $bucket
    ));
}
于 2014-06-13T09:51:53.827 回答