Last active
November 22, 2024 00:16
-
-
Save ukautz/0925e658aa3ec4f09eac3780c5314bb6 to your computer and use it in GitHub Desktop.
Using S3 client directly in Laravel
This file contains hidden or 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?
@DustinCai Why do you need the bucket name programmatically? You don't have it through some other variable or config that you used to setup the S3 disk connection?
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()