innmind / s3
Manipulate a S3 bucket
5.0.0
2025-06-02 17:48 UTC
Requires
- php: ~8.2
- innmind/foundation: ~1.1
Requires (Dev)
- innmind/black-box: ~6.2
- innmind/coding-standard: ~2.0
- innmind/static-analysis: ^1.2.1
- symfony/dotenv: ^7.0
This package is auto-updated.
Last update: 2025-06-02 17:49:20 UTC
README
Minimalist abstraction to work with any S3 bucket.
Installation
composer require innmind/s3
Usage
use Innmind\S3\{ Factory, Region, }; use Innmind\OperatingSystem\Factory as OSFactory; use Innmind\Filesystem\File\Content; use Innmind\Url\{ Url, Path, }; $os = OSFactory::build(); $bucket = Factory::of($os)->build( Url::of('https://acces_key:acces_secret@bucket-name.s3.region-name.scw.cloud/'), Region::of('region-name'), ); $file = $bucket->get(Path::of('some-file.txt'))->match( static fn(Content $file) => $file, static fn() => throw new \RuntimeException('File does not exist'), ); $bucket->upload(Path::of('some-other-name.txt'), $file)->match( // essentially this will copy the file static fn() => null, // everything ok static fn(\Throwable $e) => throw $e, );
To simplify some usage you can use the filesystem adapter on top of the bucket interface. Here's an example to upload a directory to a bucket:
use Innmind\S3\Filesystem; use Innmind\Filesystem\Name; $data = $os->filsystem()->mount(Path::of('/var/data')); $s3 = Filesystem::of($bucket); $data ->get(Name::of('images')) ->match( static fn($file) => $s3->add($file)->unwrap(), static fn() => null, // do something if there is no images );
Warning
A bucket can contain a file and a directory with the same name. This is not supported by innmind/filesystem
adapters. This means that if you use Innmind\S3\Filesystem\Adapter
on a bucket where there is a file and a directory with the same name it will result in unexpected behaviour or an exception.