10
5

More than 5 years have passed since last update.

PHP 5.6でS3にファイルをアップロード

Last updated at Posted at 2017-02-25

1. インストール

1.1 参考記事

1.2 インストール

curl -sS https://getcomposer.org/installer | php

V2 インストール:
composer.jsonファイルを新規作成

composer.json
{
    "require": {
        "aws/aws-sdk-php": "2.*"
    }
}

V3 インストール

php composer.phar require aws/aws-sdk-php

2. 接続

2.1 AWS Credential file

~/.aws/credentials
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
region=us-east-1

[test-user]
aws_access_key_id=AKIAI44QH8DHBEXAMPLE
aws_secret_access_key=je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
region=us-west-2

2.2 設定ファイル

my_config.json
{
    "includes": ["_aws"],
    "services": {
        "default_settings": {
            "params": {
                "profile": "test-user",
                "region": "us-east-1"
            }
        }
    }
}

2.3 uploader.php PHPソースコード

uploader_v2.php
<?php

require '/Users/zeng/s3upload/vendor/autoload.php';
use Aws\Common\Aws;


$aws = Aws::factory('./my_config.json');

$client = $aws->get('S3');


$result = $client->listBuckets();

foreach ($result['Buckets'] as $bucket) {
    // Each Bucket value will contain a Name and CreationDate
    echo "{$bucket['Name']} - {$bucket['CreationDate']}\n";
}

設定ファイルKeyの意味:
http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/configuration.html#profile

uploader_v3.php
<?php

require '/Users/*****/s3upload/vendor/autoload.php';

date_default_timezone_set("Asia/Tokyo");

// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
    'profile'  => 'test-user',
    'region'  => 'us-west-1',
    'version' => 'latest'
];


// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);

// Create an Amazon S3 client using the shared configuration data.
$client = $sdk->createS3();


$result = $client->listBuckets();

foreach ($result['Buckets'] as $bucket) {
    // Each Bucket value will contain a Name and CreationDate
    echo "{$bucket['Name']} - {$bucket['CreationDate']}\n";
}


3. List Bucket実行結果

/usr/bin/php /Users/zeng/s3upload/uploader.php
s-web - 2017-02-08T06:21:18.000Z
s**** - 2016-04-26T06:31:00.000Z
Process finished with exit code 0

4. Upload File to S3

参考資料
http://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/UploadObjSingleOpPHP.html


# http://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/UploadObjSingleOpPHP.html
# File upload
// Upload a file.
$result = $client->putObject(array(
    'Bucket'       => "******",
    'Key'          => "test01/s3upload/test01.jpg",
    'SourceFile'   => "/Users/******/IMG_1583.JPG",
    'ACL'          => 'public-read',
    'Metadata'     => array(
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));

echo $result['ObjectURL'];

出力結果:

https://s3-ap-northeast-1.amazonaws.com/******/test01/s3upload/test01.jpg
10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5