如果它已经存在,则创建存储桶

时间:2013-08-05 13:26:31

标签: php amazon-s3

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

我需要知道如何使用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

2 个答案:

答案 0 :(得分:4)

使用headBucket()

$myBucketExists = $client->headBucket( array('Bucket' => $bucket) );

答案 1 :(得分:1)

试试这个:

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

if(!$s3->doesBucketExist($bucket)) {
    $s3->createBucket(array(
        'Bucket' => $bucket
    ));
}