nzo / s3-aws-bundle
A minimal Symfony service for storing files and signing download URLs on Amazon S3 and compatible storage
Requires
- php: >=8.1
- aws/aws-sdk-php: ^3.371.4
- guzzlehttp/psr7: ^2.4.5
- psr/http-message: ^1.0 || ^2.0
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- phpstan/phpstan: ^1.12 || ^2.0
- phpunit/phpunit: ^9.6
- rector/rector: ^1.2 || ^2.0
- symfony/var-dumper: ^6.4 || ^7.0 || ^8.0
Suggests
- symfony/yaml: Required to configure the bundle using YAML files
Provides
None
Conflicts
None
Replaces
None
README
A Symfony bundle for storing files in Amazon S3 or compatible storage, and giving users temporary download links without making your bucket public.
Inject one service to upload, check or delete objects and sign download URLs for a configured bucket.
Which version do I need?
| Your application | Bundle version | Install |
|---|---|---|
| Symfony 6.4, 7 or 8 | 3 | composer require nzo/s3-aws-bundle:^3.0 |
| Symfony 4.4 to 6.3 | 2 | composer require nzo/s3-aws-bundle:^2.1 |
Version 3 requires PHP 8.1 or later. Symfony 7 and 8 have their own PHP requirements. The documentation for version 2 is on the 2.x branch. Coming from version 2? See UPGRADE-3.0.md.
Installation
composer require nzo/s3-aws-bundle
Symfony Flex can register the bundle for you. If it is not listed in config/bundles.php, register it yourself:
// config/bundles.php return [ // ... Nzo\S3AwsBundle\NzoS3AwsBundle::class => ['all' => true], ];
Set the bucket and its region:
# config/packages/nzo_s3_aws.yaml nzo_s3_aws: aws_region: '%env(AWS_REGION)%' aws_bucket: '%env(AWS_BUCKET)%'
Without explicit credentials, the AWS SDK uses its default credential chain. It can use your workload's IAM role, the process environment (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN when applicable), or a shared AWS profile. Use an IAM role in production where possible.
Usage
Store a file
Inject Nzo\S3AwsBundle\S3\S3AwsHandler into your service. Pass the object key, including any prefix, and a readable local file path:
use Nzo\S3AwsBundle\S3\S3AwsHandler; final class ReportStorage { public function __construct(private readonly S3AwsHandler $storage) { } public function store(string $reportId, string $csvPath): void { $this->storage->uploadFile( 'reports/' . $reportId . '.csv', $csvPath, acl: null, contentType: 'text/csv', metadata: ['report-id' => $reportId], ); } }
Use uploadStream() with a readable PHP stream resource or PSR-7 stream when you already have one. Rewind a seekable stream before uploading its whole contents, and close it when you are done. Both upload methods return an Aws\Result with fields such as ETag.
Check or delete an object
if ($storage->exists('reports/42.csv')) { $storage->delete('reports/42.csv'); }
exists() returns false on HTTP 404. A 403 is an error, not proof that the object is missing. On AWS S3, a missing object can return 403 when the caller lacks s3:ListBucket. delete() returns nothing; S3 can accept a delete for a key that does not exist. In a versioned bucket a delete normally adds a delete marker, leaving older versions in place.
Give a user a temporary download link
After checking that the user is allowed to download the report, redirect to a signed GET URL:
use Symfony\Component\HttpFoundation\RedirectResponse; return new RedirectResponse($storage->presignedUrl('reports/42.csv', expiresIn: 120));
The default lifetime is 900 seconds; allowed values are 1 to 604,800 seconds (7 days). A URL signed with temporary credentials stops working when those credentials expire, even if its requested lifetime is longer. Signing does not fetch the object or check that it exists, though the SDK may need to fetch or refresh credentials.
Handle errors
Catch Nzo\S3AwsBundle\Exception\S3Exception for SDK failures. Its code is the HTTP status when available, or 0 otherwise. It does not include the SDK exception, signed request, or response body. Invalid arguments such as empty keys, unreadable files, or out-of-range lifetimes throw InvalidArgumentException before the request.
Configuration reference
nzo_s3_aws: aws_region: '%env(AWS_REGION)%' # required, for example eu-west-1 aws_bucket: '%env(AWS_BUCKET)%' # required, the bucket name aws_endpoint: null # optional, an absolute HTTP(S) URL use_path_style_endpoint: false # true for MinIO or other path-style endpoints aws_access_key: null # optional, set both credentials or neither aws_secret: null
For MinIO or another compatible service, set aws_endpoint (for example https://s3.example.com) and enable use_path_style_endpoint if the service expects /bucket/key URLs. The configured endpoint must be reachable by the browsers that will use the signed URLs; rewriting the host after signing invalidates the signature.
If you need explicit static credentials, set both to non-empty values provided by your environment or Symfony secrets:
nzo_s3_aws: aws_region: '%env(AWS_REGION)%' aws_bucket: '%env(AWS_BUCKET)%' aws_access_key: '%env(NZO_S3_ACCESS_KEY)%' aws_secret: '%env(NZO_S3_SECRET)%'
Security
- Limit the IAM role or user to the bucket and prefixes your application needs. Only grant write or delete permissions if the application uses those operations.
- Keep S3 Block Public Access enabled. Recent buckets block public ACLs by default, and buckets with Object Ownership set to Bucket owner enforced reject most explicit ACLs, including
private. The default upload ACL remainsprivatefor compatibility. For ACL-disabled buckets, passacl: nullto omit the ACL header and let the bucket policy control access. - Treat signed URLs as bearer links. Anyone with the URL can use it until it expires. Authorize the user before signing, keep lifetimes short, use HTTPS, and do not log the URL's query string.
- Use
%env(...)%for explicit keys: literal values can be written to Symfony's compiled container. Do not put credentials in the endpoint URL or run debugging commands that resolve secret environment variables into plaintext.
When to use Flysystem instead
Use this bundle if your application needs a handful of S3 operations against one bucket, with direct access to the SDK's upload result. Choose Flysystem if you want a filesystem API across storage backends, multiple adapters, directory listings, moves, or a local adapter for development. Use the AWS SDK directly for S3 features beyond this bundle's API, such as multipart uploads or bucket administration.
License
This bundle is under the MIT license. See LICENSE.