Help us understand the problem. What is going on with this article?

AWSSDKforPHP S3

More than 5 years have passed since last update.

簡易的にS3を利用するメモ
参照:AWSSDKforPHP

S3Model.php
/*
$s3Config = array(
    'access_key' => 'ahoge', // アクセスキー
    'secret_key' => 'shoge', // シークレットキー
    'bucket'     => 'bhoge'  // バケット名
    );
*/
// S3接続
function connectS3($s3Config, $createFlg = true) {
    $this->_initS3($s3Config);
    $result = $this->_existBucket($s3Config['bucket']);

    // バケットが存在しない場合、新規作成
    if (!$result && $createFlg) {
        $this->s3->createBucket(array(
            'LocationConstraint' => 'ap-northeast-1', 
            'Bucket' => $s3Config['bucket']
            ));
        /*
        LocationConstraintの値は以下から選択
            'ap-northeast-1'
            'us-west-2'
            'us-west-1'
            'eu-west-1'
            'eu-central-1'
            'ap-southeast-1'
            'ap-southeast-2'
            'sa-east-1'

        ロケーションにより、S3の利用速度が変わってきます。
        「ap-northeast-1」(Tokyo)固定で良いかも。
        */
    }
}


// S3利用準備
private function _initS3($s3Config) {
    if (isset($this->s3)) return;

    $params = array(
        'key'      => $s3Config['access_key'],
        'secret'   => $s3Config['secret_key'],
    );
    $this->s3 = S3Client::factory($params);
}


// バケット確認
private function _existBucket($bucketName) {
    $bucketList = $this->s3->listBuckets();
    foreach ($bucketList["Buckets"] as $bucket) {
        if ($bucket['Name'] === $bucketName) {
            return true;
        }
    }
    return false;
}

// バケット削除
function deleteBucket($s3Config) {
    $this->_initS3($s3Config);

    // 削除するバケットのリージョンを設定
    $this->s3->setRegion('ap-northeast-1');

    return $this->s3->deleteBucket(array(
        'Bucket' => $s3Config['bucket']
        ));
}

terumichi1209
WEBエンジニアです!
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away