aws/aws-sdk-php
AWS SDK for PHP v3 provides a complete client library for Amazon Web Services (S3, DynamoDB, Glacier, and more). Install via Composer, authenticate with AWS credentials, and use service clients to build robust PHP apps with AWS APIs.
## Getting Started
### Minimal Setup in Laravel
1. **Install the SDK** via Composer (updated for latest features):
```bash
composer require aws/aws-sdk-php:^3.382.0
Laravel developers should use the aws/aws-sdk-php-laravel package for tighter integration:
composer require aws/aws-sdk-php-laravel
Configure AWS credentials in .env (unchanged):
AWS_ACCESS_KEY_ID=your-access-key
AWS_SECRET_ACCESS_KEY=your-secret-key
AWS_DEFAULT_REGION=us-east-1
First use case: S3 Multipart Copy with Metadata Directive (new feature):
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
public function copyWithMetadataDirective()
{
$s3 = new S3Client([
'version' => 'latest',
'region' => env('AWS_DEFAULT_REGION'),
]);
try {
$result = $s3->copyObject([
'Bucket' => 'destination-bucket',
'Key' => 'destination/path/file.jpg',
'CopySource' => 'source-bucket/source/path/file.jpg',
'MetadataDirective' => 'COPY', // New default: preserves source metadata
// OR set to 'REPLACE' to suppress automatic metadata copying
]);
return $result;
} catch (AwsException $e) {
\Log::error("S3 Copy Error: " . $e->getMessage());
throw $e;
}
}
Leverage Laravel’s service container (updated for new services):
$s3 = app('aws')->createS3();
$iotWireless = app('aws')->createIoTWireless();
$invoicing = app('aws')->createInvoicing();
$securityAgent = app('aws')->createSecurityAgent();
AppServiceProvider:
public function register()
{
$this->app->singleton(\Aws\IoTWireless\IoTWirelessClient::class, function ($app) {
return new \Aws\IoTWireless\IoTWirelessClient([
'version' => 'latest',
'region' => env('AWS_IOT_WIRELESS_REGION'),
'credentials' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
],
]);
});
$this->app->singleton(\Aws\Invoicing\InvoicingClient::class, function ($app) {
return new \Aws\Invoicing\InvoicingClient([
'version' => 'latest',
'region' => env('AWS_INVOICING_REGION'),
]);
});
}
$s3->copyObject([
'Bucket' => 'destination-bucket',
'Key' => 'destination/path/file.jpg',
'CopySource' => 'source-bucket/source/path/file.jpg',
'MetadataDirective' => 'COPY', // Default: preserves source metadata
// OR
'MetadataDirective' => 'REPLACE', // Suppresses automatic metadata copying
'Metadata' => ['custom-key' => 'custom-value'], // User-provided metadata
]);
$invoicing = new \Aws\Invoicing\InvoicingClient([...]);
$response = $invoicing->createInvoiceUnit([
'ClientToken' => 'unique-idempotency-token-123',
'InvoiceUnit' => [
'InvoiceId' => 'invoice-123',
'UnitType' => 'SUBSCRIPTION',
'Quantity' => 1,
],
]);
$securityAgent = new \Aws\SecurityAgent\SecurityAgentClient([...]);
$response = $securityAgent->getFinding([
'FindingId' => 'finding-123',
'IncludeVerificationScript' => true,
]);
// Save the verification script to a file
file_put_contents(
storage_path('app/verification_script.sh'),
$response['VerificationScript']['Script']
);
$pi = new \Aws\PI\PIClient([...]);
$response = $pi->listPerformanceAnalysisReportRecommendations([
'ReportId' => 'report-123',
'MaxResults' => 10,
]);
foreach ($response['Recommendations'] as $recommendation) {
\Log::info('Recommendation: ' . $recommendation['Recommendation']);
}
$qConnect = new \Aws\QConnect\QConnectClient([...]);
$response = $qConnect->listSpans([
'SpanId' => 'span-123',
'IncludeGuardrailAssessment' => true,
]);
foreach ($response['Spans'][0]['GuardrailAssessment']['Results'] as $result) {
\Log::info('Policy: ' . $result['PolicyName'] .
', Blocked: ' . ($result['Blocked'] ? 'Yes' : 'No'));
}
S3 Multipart Copy Metadata Directive:
metadata_directive: New option for copyObject to control metadata handling during multipart copy operations.'COPY' (preserves source metadata).'REPLACE' to suppress automatic metadata copying and provide custom metadata via the Metadata parameter.Idempotency in Invoicing:
ClientToken: Added support for idempotency in CreateInvoiceUnit, DeleteInvoiceUnit, UpdateInvoiceUnit, DeleteProcurementPortalPreference, PutProcurementPortalPreference, and UpdateProcurementPortalPreferenceStatus.Security Agent Verification Scripts:
Performance Insights (PI) Recommendations:
QConnect Guardrail Assessment:
Metadata Directive Conflicts:
MetadataDirective is explicitly set and that user-provided metadata in the Metadata parameter takes precedence.// User metadata overrides source metadata
$s3->copyObject([
'MetadataDirective' => 'COPY', // Source metadata is copied
'Metadata' => ['custom-key' => 'overrides-source'], // Overrides any source metadata
]);
Idempotency Token Generation:
ClientToken to ensure uniqueness:
$clientToken = Str::uuid()->toString();
Verification Script Execution:
VerificationScript response for required variables and instructions.Region Configuration:
AWS_IOT_WIRELESS_REGION, AWS_INVOICING_REGION) to avoid default region assumptions.Error Handling:
try {
$response = $
How can I help you explore Laravel packages today?