## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require artox-lab/aws-sdk-php-symfony
Add the bundle to config/bundles.php:
return [
// ...
Aws\Symfony\AwsBundle::class => ['all' => true],
];
Basic Configuration:
Define AWS credentials in .env (recommended) or config/packages/aws.yaml:
aws:
version: latest
region: us-east-1
credentials:
key: '%env(AWS_ACCESS_KEY_ID)%'
secret: '%env(AWS_SECRET_ACCESS_KEY)%'
First Use Case: Inject the SDK or a client directly into a service/controller:
use Aws\S3\S3Client;
class MyService {
public function __construct(private S3Client $s3) {}
public function uploadFile() {
$this->s3->putObject([...]);
}
}
Or fetch via Symfony's container:
$s3 = $container->get('aws.s3');
Lazy-Loaded Clients: All AWS clients are lazy-loaded (since v2.4.0), improving startup performance. Example:
$dynamodb = $container->get('aws.dynamodb'); // Loaded on first use
Service Naming Convention:
Clients follow aws.{service} naming (e.g., aws.s3, aws.dynamodb).
Full list via:
php bin/console debug:container aws
Configuration Overrides: Override service-specific configs in YAML/PHP/XML:
aws:
S3:
version: '2006-03-01'
endpoint: 'https://custom-s3-endpoint.com'
Dynamic Credentials:
Use @service syntax for dynamic credentials (e.g., @aws_credentials):
aws:
Sqs:
credentials: "@aws_credentials_service"
credentials: ~ in config to auto-fetch from EC2 metadata (no hardcoded keys).us-west-2 while default is us-east-1).aws:
S3:
endpoint: 'http://localhost:4566'
Event Dispatching:
Bind AWS events to Symfony events (e.g., Aws\Event\BeforeCommand):
$dispatcher->addListener('aws.command.before', function ($event) {
// Pre-command logic
});
Middleware Stack: Extend the SDK’s middleware stack via config:
aws:
middleware:
- Aws\Common\Middleware\RetryMiddleware
- App\Middleware\CustomLoggingMiddleware
Testing:
Use Aws\Tests\MockHandler for unit tests:
use Aws\Tests\MockHandler;
use Aws\S3\S3Client;
$mock = new MockHandler();
$s3 = new S3Client([], ['handler' => $mock]);
Configuration Merging:
AWS_MERGE_CONFIG=true in .env to validate/merge configs (fails on duplicates).Lazy-Loading Quirks:
onKernelRequest).if ($container->has('aws.s3')) to check availability.Service Name Collisions:
aws.* to prevent conflicts with auto-registered clients.Deprecated Methods:
putObject() instead of putObjectData() for S3 uploads.Enable Debugging:
aws:
debug: true
logging:
stream: php://stdout
Logs appear in Symfony’s debug toolbar or var/log/dev.log.
Common Errors:
No such service: Missing bundle registration or typo in service name.
Fix: Run php bin/console debug:container aws to verify available services.Invalid credential: Check .env or IAM roles (for EC2).Region not found: Ensure the region exists in AWS and is spelled correctly.Configuration Validation:
Use AWS_MERGE_CONFIG=true to catch misconfigurations early:
AWS_MERGE_CONFIG=true php bin/console config:dump
Custom Clients: Register additional clients via DI:
services:
aws.custom_client:
class: Aws\Custom\CustomClient
arguments:
- '@aws_sdk'
- '%aws.custom_config%'
Middleware: Add custom middleware to the SDK stack:
$sdk->registerMiddleware(new class implements Aws\Middleware {
public function __invoke($request, $handler) {
// Modify request/response
return $handler($request);
}
});
Environment-Specific Configs:
Use Symfony’s environment-aware configs (e.g., config/packages/aws_dev.yaml):
# config/packages/aws_dev.yaml
aws:
debug: true
region: us-west-2
S3Client) across services instead of recreating it.batchWriteItem for DynamoDB) to reduce API calls.pool option) for high-throughput apps..env to version control.s3:GetObject instead of s3:*).aws:
logging:
stream: null # Disable logging in production
---
How can I help you explore Laravel packages today?