Last active
September 3, 2024 03:34
-
-
Save ukautz/0925e658aa3ec4f09eac3780c5314bb6 to your computer and use it in GitHub Desktop.
Using S3 client directly in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
FYI this no longer works with Laravel 9.x
FYI this no longer works with Laravel 9.x
what works now for getting the bucket name programmatically?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks !
You can even have the bucket with $adapter->getBucket()