Advanced usage - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Advanced usage

This section covers advanced usage patterns and techniques for the S3 Transfer Manager.

Multipart uploads

S3 Transfer Manager automatically uses multipart uploads for large files. You can customize this behavior with configuration options.

<?php use Aws\S3\S3Transfer\Models\UploadRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, []); $uploadPromise = $transferManager->upload( new UploadRequest( '/path/to/large/file.mp4', [ 'Bucket' => 'amzn-s3-demo-bucket', 'Key' => 'videos/large-file.mp4', ], [ // Use multipart upload for files larger than 100MB. 'multipart_upload_threshold_bytes' => 100 * 1024 * 1024, // Use 25MB parts for multipart uploads. 'target_part_size_bytes' => 25 * 1024 * 1024, ] ) ); $uploadPromise->wait();

Multipart downloads

You can customize multipart download behavior.

<?php use Aws\S3\S3Transfer\AbstractMultipartDownloader; use Aws\S3\S3Transfer\Models\DownloadRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, []); $downloadPromise = $transferManager->download( new DownloadRequest( 's3://amzn-s3-demo-bucket/large-file.mp4', [], [ // Use 25MB parts for multipart downloads. 'target_part_size_bytes' => 25 * 1024 * 1024, // Use ranged-based download instead of part-based. 'multipart_download_type' => AbstractMultipartDownloader::RANGED_GET_MULTIPART_DOWNLOADER, ] ) ); $downloadPromise->wait();

For more information about other members of the AbstractMultipartDownloader class, see the API documentation<add link>.

Custom filters

You can use filter functions to selectively upload or download files for directory operations.

Upload directory filter

For more information about parameter information, see the filter callable option of the uploadDirectory method.

<?php use Aws\S3\S3Transfer\Models\UploadDirectoryRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, []); // Upload files modified in the last 24 hours. $uploadDirPromise = $transferManager->uploadDirectory( new UploadDirectoryRequest( '/path/to/directory', 'amzn-s3-demo-bucket', [], [ 'filter' => function ($file) { $modTime = filemtime($file); return (time() - $modTime) < 86400; // 24 hours }, ] ) ); $uploadDirPromise->wait();

Download directory filter

For more information about parameter information, see the filter callable option of the downloadDirectory method.

<?php use Aws\S3\S3Transfer\Models\DownloadDirectoryRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, []); // Download files with a specific prefix. $downloadDirPromise = $transferManager->downloadDirectory( new DownloadDirectoryRequest( 'amzn-s3-demo-bucket', '/path/to/directory', [], [ 'filter' => function ($key) { return strpos($key, 'reports/2023/') === 0; }, ] ) ); $downloadDirPromise->wait();

Pre-request callbacks

You can modify request parameters for each file using pre-request callbacks. For more information about parameters, see the following:

Example of a pre-request callback for the uploadDirectory method
<?php use Aws\S3\S3Transfer\Models\UploadDirectoryRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, []); // Set custom metadata for each uploaded file. $uploadDirPromise = $transferManager->uploadDirectory( new UploadDirectoryRequest( '/path/to/directory', 'amzn-s3-demo-bucket', [], [ 'upload_object_request_modifier' => function ($args) { $extension = pathinfo($args['Key'], PATHINFO_EXTENSION); switch ($extension) { case 'jpg': case 'jpeg': $args['ContentType'] = 'image/jpeg'; break; case 'png': $args['ContentType'] = 'image/png'; break; case 'pdf': $args['ContentType'] = 'application/pdf'; break; } $args['Metadata'] = [ 'uploaded_at' => date('Y-m-d H:i:s'), ]; }, ] ) ); $uploadDirPromise->wait();