12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アップロードされたファイルをそのままAmazonS3に転送する

Last updated at Posted at 2014-11-30

PHPでブラウザからアップロードされたファイルをサーバ内に保存する場合、普通なら move_uploaded_file 関数を使うと思います。

では受け取ったファイルをサーバ内ではなくAmazonS3に保存したい場合はどうすればよいでしょうか?

AWS SDK for PHPを使えば簡単にできます。

$result = $s3client->putObject([
    'Bucket'     => $bucket,
    'Key'        => 'dir/to/picture.jpg',
    'SourceFile' => $_FILES['picture']['tmp_name'],
    'Metadata'   => [
        'Foo' => 'abc',
        'Baz' => '123'
    ]
]);

move_uploaded_fileする代わりに、pubObject()のSourceFileパラメータに $_FILES[フォーム項目名]['tmp_name']を指定するのがポイントです。

12
13
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
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?