Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Aws Sdk Php Symfony Laravel Package

artox-lab/aws-sdk-php-symfony

View on GitHub
Deep Wiki
Context7
## 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],
];
  1. 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)%'
    
  2. 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');
    

Implementation Patterns

Dependency Injection Workflow

  1. 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
    
  2. Service Naming Convention: Clients follow aws.{service} naming (e.g., aws.s3, aws.dynamodb). Full list via:

    php bin/console debug:container aws
    
  3. Configuration Overrides: Override service-specific configs in YAML/PHP/XML:

    aws:
        S3:
            version: '2006-03-01'
            endpoint: 'https://custom-s3-endpoint.com'
    
  4. Dynamic Credentials: Use @service syntax for dynamic credentials (e.g., @aws_credentials):

    aws:
        Sqs:
            credentials: "@aws_credentials_service"
    

Common Use Cases

  • EC2 Instance Profiles: Set credentials: ~ in config to auto-fetch from EC2 metadata (no hardcoded keys).
  • Multi-Region Services: Override region per service (e.g., DynamoDB in us-west-2 while default is us-east-1).
  • Custom Endpoints: Useful for localstack or private AWS endpoints:
    aws:
        S3:
            endpoint: 'http://localhost:4566'
    

Integration Tips

  1. Event Dispatching: Bind AWS events to Symfony events (e.g., Aws\Event\BeforeCommand):

    $dispatcher->addListener('aws.command.before', function ($event) {
        // Pre-command logic
    });
    
  2. Middleware Stack: Extend the SDK’s middleware stack via config:

    aws:
        middleware:
            - Aws\Common\Middleware\RetryMiddleware
            - App\Middleware\CustomLoggingMiddleware
    
  3. Testing: Use Aws\Tests\MockHandler for unit tests:

    use Aws\Tests\MockHandler;
    use Aws\S3\S3Client;
    
    $mock = new MockHandler();
    $s3 = new S3Client([], ['handler' => $mock]);
    

Gotchas and Tips

Pitfalls

  1. Configuration Merging:

    • Enable AWS_MERGE_CONFIG=true in .env to validate/merge configs (fails on duplicates).
    • Without this, configs are merged silently (risk of typos).
  2. Lazy-Loading Quirks:

    • Clients are not available during container compilation (e.g., in onKernelRequest).
    • Use if ($container->has('aws.s3')) to check availability.
  3. Service Name Collisions:

    • Avoid naming custom services aws.* to prevent conflicts with auto-registered clients.
  4. Deprecated Methods:

    • SDK v3 uses putObject() instead of putObjectData() for S3 uploads.
    • Check AWS SDK Changelog for breaking changes.

Debugging

  1. Enable Debugging:

    aws:
        debug: true
        logging:
            stream: php://stdout
    

    Logs appear in Symfony’s debug toolbar or var/log/dev.log.

  2. 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.
  3. Configuration Validation: Use AWS_MERGE_CONFIG=true to catch misconfigurations early:

    AWS_MERGE_CONFIG=true php bin/console config:dump
    

Extension Points

  1. Custom Clients: Register additional clients via DI:

    services:
        aws.custom_client:
            class: Aws\Custom\CustomClient
            arguments:
                - '@aws_sdk'
                - '%aws.custom_config%'
    
  2. 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);
        }
    });
    
  3. 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
    

Performance Tips

  1. Reuse Clients: Inject the same client (e.g., S3Client) across services instead of recreating it.
  2. Batch Operations: Use SDK batch operations (e.g., batchWriteItem for DynamoDB) to reduce API calls.
  3. Connection Pooling: Configure HTTP client pooling (e.g., Guzzle’s pool option) for high-throughput apps.

Security

  1. Credentials:
    • Never commit .env to version control.
    • Use IAM roles for EC2/Lambda deployments (avoid hardcoded keys).
  2. Least Privilege: Restrict IAM policies to the minimum required permissions (e.g., s3:GetObject instead of s3:*).
  3. Logging: Avoid logging sensitive data (e.g., request payloads with credentials).
    aws:
        logging:
            stream: null # Disable logging in production
    

---
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware