本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
檔案操作
S3 Transfer Manager 提供上傳和下載個別檔案的方法。
上傳本機檔案
若要將檔案上傳至 Amazon S3,請使用 upload <新增連結> 方法。
<?php use Aws\S3\S3Transfer\Models\UploadRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, [ 'default_region' => 'us-west-2' ]); // Source can be from a local path to a file. $source = '/path/to/local/file.txt'; // Or the source can be an instance of StreamInterface. $source = GuzzleHttp\Psr7\Utils::streamFor('Hello World!'); $uploadPromise = $transferManager->upload( new UploadRequest( $source, [ 'Bucket' => 'amzn-s3-demo-bucket', 'Key' => 'path/to/s3/file.txt', // Additional `putObject` parameters as needed. ], [ // Optional configuration overrides. 'multipart_upload_threshold_bytes' => 100 * 1024 * 1024, // 100MB 'target_part_size_bytes' => 10 * 1024 * 1024, // 10MB 'track_progress' => true, 'request_checksum_calculation' => 'when_required', ] ) ); // The upload is asynchronous, you can wait for it to complete. $result = $uploadPromise->wait(); // Or you can use the promise for more complex workflows. $result = $uploadPromise->then( function ($result) { echo "Upload succeeded!"; }, function ($error) { echo "Upload failed: " . $error->getMessage(); } )->wait();
upload 方法參數
upload 方法接受 UploadRequest <add link> 執行個體做為引數。
UploadRequest 參數
| 參數 | Type | 必要 | Description |
|---|---|---|---|
|
|
string| |
是 |
包含要上傳之資料的本機檔案路徑或串流路徑。 |
|
|
陣列 |
是 |
上傳請求引數 (必須包含儲存貯體和金鑰)。 |
|
|
陣列 |
否 |
組態會覆寫此上傳的特定 。如需組態選項的詳細資訊,請參閱下一節。 |
|
|
陣列 |
否 |
用於監控此上傳的 |
|
|
TransferListener |
否 |
此上傳的進度追蹤器。 |
SDK 會從 S3 Transfer Manager 的組態解析$config預設值。
| 選項 | Type | 必要 | Description |
|---|---|---|---|
|
|
int |
否 |
在觸發分段上傳時覆寫 的預設閾值。 |
|
|
int |
否 |
以位元組為單位覆寫預設目標部分大小。 |
|
|
bool |
否 |
覆寫預設選項以啟用進度追蹤。如果此選項是 true且您未提供 progressTracker 參數,則 SDK 會使用預設實作。 |
|
|
int |
否 |
覆寫並行的預設值。 |
|
|
string |
否 |
覆寫是否必須執行請求檢查總和計算的預設值。 |
upload 方法成功執行時,會傳回 UploadResult <add link>。
下載 S3 物件
若要從 Amazon S3 下載物件,請使用 download <新增連結> 方法。
<?php use Aws\S3\S3Transfer\Models\DownloadRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, [ 'default_region' => 'us-west-2' ]); // Download using an S3 URI. $downloadPromise = $transferManager->download( new DownloadRequest( 's3://amzn-s3-demo-bucket/path/to/s3/file.txt', [ // Additional `getObject` parameters as needed. ], [ // Optional configuration overrides. 'response_checksum_validation' => 'when_required', 'track_progress' => true, 'target_part_size_bytes' => 10 * 1024 * 1024, // 10MB ] ) ); // Wait for the download to complete. $result = $downloadPromise->wait(); // The SDK uses a stream-based download handler by default. $stream = $result->getDownloadDataResult(); // You can either get the content. $content = $stream->getContents(); // Or write the stream to a file. file_put_contents('/path/to/local/file.txt', $stream); // Don't forget to close the stream. $stream->close();
download 方法參數
download 方法接受 DownloadRequest <add link> 執行個體做為引數。
DownloadRequest 參數
| 參數 | Type | 必要 | Description |
|---|---|---|---|
|
|
string|array| |
否 |
要從 S3 下載的物件。您可以將此參數提供為 S3 URI 字串 ("s3://amzn-s3-demo-bucket/key")、具有儲存貯體和金鑰的陣列,或 |
|
|
陣列 |
否 |
其他下載物件請求引數。如果 |
|
|
陣列 |
否 |
組態會覆寫此下載的特定組態。如需組態選項的詳細資訊,請參閱下一節。 |
$downloadHandler |
|
否 |
接收每個物件區塊並管理其下載的處理常式。
您可以實作自己的 |
|
|
|
否 |
此下載的進度追蹤器。 |
|
|
|
否 |
用於監控此下載的 |
SDK 會從 S3 Transfer Manager 的組態解析$config預設值。
| 選項 | Type | 必要 | 描述 |
|---|---|---|---|
|
|
string |
否 |
覆寫預設分段下載類型。有效值為 'part'、'ranged' |
|
|
string |
否 |
覆寫 Transfer Manager 組態中解析的值,決定是否應完成檢查總和驗證。只有當 |
|
|
bool |
否 |
覆寫啟用進度追蹤的預設選項。如果此選項為 , 使用此選項可在 |
|
|
int |
否 |
要在範圍分段下載中使用的分段大小,以位元組為單位。如果您未提供此參數,下載會使用在傳輸管理員上 |
當download方法成功執行時,它會傳回 DownloadResult <add link>。
將 S3 物件下載至本機檔案
若要將 S3 物件直接下載至本機檔案,請使用 downloadFile方法:
<?php use Aws\S3\S3Transfer\Models\DownloadFileRequest; use Aws\S3\S3Transfer\Models\DownloadRequest; use Aws\S3\S3Transfer\S3TransferManager; require __DIR__ . '/../vendor/autoload.php'; $transferManager = new S3TransferManager(null, [ 'default_region' => 'us-west-2' ]); $downloadFilePromise = $transferManager->downloadFile( new DownloadFileRequest( '/path/to/local/file.txt', // Destination file path. false, // Fail when destination exists. new DownloadRequest( 's3://amzn-s3-demo-bucket/path/to/s3/file.txt', [], // `getObject` request args [ 'track_progress' => true, 'target_part_size_bytes' => 10 * 1024 * 1024, // 10MB parts ] ) ) ); // Wait for download to complete. $result = $downloadFilePromise->wait(); // `getDownloadDataResult()` returns the string for the file path of the downloaded content because // the default `DownloadHandler` used by `downloadFile()` is a file-based handler. echo "File downloaded successfully at {$result->getDownloadDataResult()}!\n";
downloadFile 方法參數
downloadFile 方法接受 執行個體DownloadFileRequest做為 引數。
DownloadFileRequest 參數
| 選項 | Type | 必要 | 描述 |
|---|---|---|---|
|
|
string |
是 |
儲存檔案的本機路徑。 |
|
|
bool |
是 |
如果目的地檔案已存在,是否失敗。如果此選項為 |
|
|
|
是 |
設定的 |
當downloadFile方法成功執行時,它會傳回 DownloadResult <add link>。