進階用量 - 適用於 PHP 的 AWS SDK

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

進階用量

本節涵蓋 S3 Transfer Manager 的進階使用模式和技術。

分段上傳

S3 Transfer Manager 會自動針對大型檔案使用分段上傳。您可以使用組態選項自訂此行為。

<?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();

分段下載

您可以自訂分段下載行為。

<?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();

如需 AbstractMultipartDownloader類別其他成員的詳細資訊,請參閱 API 文件<新增連結>

自訂篩選條件

您可以使用篩選函數來選擇性地上傳或下載目錄操作的檔案。

上傳目錄篩選條件

如需參數資訊的詳細資訊,請參閱 uploadDirectory方法的篩選可呼叫選項

<?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();

下載目錄篩選條件

如需參數資訊的詳細資訊,請參閱 downloadDirectory方法的篩選可呼叫選項

<?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();

請求前回呼

您可以使用請求前回呼來修改每個檔案的請求參數。如需參數的詳細資訊,請參閱下列內容:

範例 uploadDirectory 方法的請求前回呼
<?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();