php - Laravel s3 multiple buckets -
my laravel application needs manipulate files present in multiple buckets simultaneously single session. so, couldn't find way change several times current bucket, since .env
file this:
s3_key='my-key' s3_secret='mysecret' s3_region='us-east-1' s3_bucket='my-first-used-bucket'
i found somewhere this:
config::set('filesystems.disks.s3.bucket', 'another-bucket');
but works once. need like:
storage::disk('s3')->put('/bucket-name/path/filename.jpg', $file, 'public');
where /bucket-name/
bucket create. can do? lot!
you correct in config::set();
works once per request. estimation done intentionally stop kind of thing attempting in code example.
in config/filesystems.php can list number of "disks". these locations of file repositories. looks so:
disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'ftp' => [ 'driver' => 'ftp', 'host' => 'ftp.example.com', 'username' => 'your-username', 'password' => 'your-password', // optional ftp settings... // 'port' => 21, // 'root' => '', // 'passive' => true, // 'ssl' => true, // 'timeout' => 30, ], 's3' => [ 'driver' => 's3', 'key' => env('s3_key',''), 'secret' => env('s3_secret',''), 'region' => env('s3_region',''), 'bucket' => env('s3_bucket',''), ], ]
the solution
the solution create new disk of buckets want use. treat buckets different disks.
note: user s3_key belongs needs have permissions perform required actions on s3 buckets setting additional 'disks'.
disks' => [ //all other 'disks' ... //my default bucket details. 's3' => [ 'driver' => 's3', 'key' => env('s3_key',''), 'secret' => env('s3_secret',''), 'region' => env('s3_region',''), 'bucket' => env('s3_bucket',''), ], 's3myotherbucketname' => [ 'driver' => 's3', 'key' => env('s3_key',''), 'secret' => env('s3_secret',''), 'region' => env('s3_region',''), 'bucket' => 'myotherbucketname', ], 's3yetanotherbucketname' => [ 'driver' => 's3', 'key' => env('s3_key',''), 'secret' => env('s3_secret',''), 'region' => env('s3_region',''), 'bucket' => 'yetanotherbucketname', ], ]
then whenever want access bucket of choice call so:
storage::disk('s3')->put($filename, $data); storage::disk('s3myotherbucketname')->put($anotherfilename, $moredata); storage::disk('s3yetanotherbucketname')->put($yetanotherfilename, $evenmoredata);
Comments
Post a Comment