1
0

More than 3 years have passed since last update.

PHPでAWS CLIを用いてS3に画像をアップする

Posted at

EC2環境上でPHPのexec関数とAWS CLIを用いてS3に画像をアップしようと思ったら、嵌ったので備忘録メモです。

まず、事前にEC2上で

$ aws configure 

を叩いて、アクセスキー ID、シークレットアクセスキー、AWSリージョン、出力形式を入力した後に、PHPファイルに

exec('aws s3 sync  ./upload  s3://{バケット名}/img --acl public-read --delete', $out);

を挿入し、実行してみましたが$outに結果が返らず、S3にもアップされませんでした。
そこで、PHPのファイル上に事前に次の形式でアクセスキー ID、シークレットアクセスキー、AWSリージョンを記述したあとにexec関数を記述すると動作しました。


$region = 'ap-northeast-1';
$key = '****';
$secret = '**********************';
putenv('AWS_DEFAULT_REGION=' . $region);
putenv('AWS_ACCESS_KEY_ID=' . $key);
putenv('AWS_SECRET_ACCESS_KEY=' . $secret);
exec('aws s3 sync  ./upload  s3://{バケット名}/img --acl public-read --delete', $out);

ただし、PHPファイルにキー情報を直接書いてしまうのはセキュリティ的に問題ありそうなので、APPACHと同じキーをユーザーに割り当てる方法もあるようです。

1
0
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
1
0