Skip to content

Instantly share code, notes, and snippets.

@ukautz
Last active August 26, 2022 14:44
Show Gist options
  • Save ukautz/0925e658aa3ec4f09eac3780c5314bb6 to your computer and use it in GitHub Desktop.
Save ukautz/0925e658aa3ec4f09eac3780c5314bb6 to your computer and use it in GitHub Desktop.
Using S3 client directly in Laravel
<?php
/** @var \Illuminate\Filesystem\FilesystemAdapter $fs */
$fs = \Storage::disk('object_storage');
/** @var \League\Flysystem\Filesystem $driver */
$driver = $fs->getDriver();
/** @var \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter */
$adapter = $driver->getAdapter();
/** @var \Aws\S3\S3Client $client */
$client = $adapter->getClient();
// upload
$source = fopen("/source/file.typ", "r");
try {
$client->putObject([
'Bucket' => 'name-of-the-app',
'Key' => 'destination/path',
'Body' => $source,
]);
} catch (\Aws\S3\Exception\S3Exception $e) {
// handle exception
} finally {
fclose($source);
}
@pierrocknroll
Copy link

Thanks !
You can even have the bucket with $adapter->getBucket()

@roberttolton
Copy link

FYI this no longer works with Laravel 9.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment