LoginSignup
0
0

More than 1 year has passed since last update.

PHP:Laravel Strageを利用した複数画像uploadする方法1つとFlysystem-aws-s3で複数画像をuploadする方法2つとその処理速度

Last updated at Posted at 2021-05-18

前提

・AWSアカウントが存在していてS3バケットが作成済である。
https://qiita.com/Yorinton/items/bdf5f9bc05629c9a0df7
・laravel Strageが利用できる。
https://readouble.com/laravel/8.x/ja/filesystem.html
・Flysystem-aws-s3をインストール済みである (AWS SDK for PHP laravelでもよいかな?)
https://www.ritolab.com/entry/8
https://qiita.com/ucan-lab/items/61903ce10a186e78f15f

laravel Strageを利用したupload方法

upload方法その1

foreach (Storage::disk('public')->files('TARGET_DIR') as $fileName){
    $contents = Storage::disk('public')->get($fileName);
    Storage::disk('s3')->put('UPLOAD_TARGET_DIR' . $fileName, $contents);
};

45ファイルをアップロードした時の
実行時間:1.089635848999秒
メモリ使用量:5501696バイト

Flysystem-aws-s3を利用したupload方法

S3Clientをnewする

$client = new S3Client([
    'credentials' => [
        'key' => 'AWS_ACCESS_KEY',
        'secret' => 'AWS_SECRET_ACCESS_KEY',
    ],
    'region' => 'ap-northeast-1',
    'version' => 'latest',
]);

upload方法その2

foreach (Storage::disk('public')->files('TARGET_DIR') as $fileName){
    $params = [
        'Bucket' => 'BUCKET_NAME',
        'Key' => $fileName,
        'SourceFile'   => storage_path('app/public/') . $fileName,
    ];
    $client->putObject($params);
}

45ファイルをアップロードした時の
実行時間:0.97133994102478秒
メモリ:1193024バイト

upload方法その3

$client->uploadDirectory(storage_path('app/public/') . 'TARGET_DIR', 'BUCKET_NAME' . 'UPLOAD_TARGET_DIR');

45ファイルをアップロードした時の
実行時間:0.68526291847229秒
メモリ使用量:1509568バイト

結果

laravel Strageを利用したupload方法だと時間がかかる上、メモリもだいぶ喰らう。
Flysystem-aws-s3やAWS SDK for PHPなどを利用してファイルをアップしたほうが良さそう。
実現したいことに合わせてupload方法その2と3を使い分けたほうがいいカモ?

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