檔案操作 - 適用於 PHP 的 AWS SDK

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

檔案操作

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

$source

string|StreamInterface

包含要上傳之資料的本機檔案路徑或串流路徑。

$uploadRequestArgs

陣列

上傳請求引數 (必須包含儲存貯體和金鑰)。

$config

陣列

組態會覆寫此上傳的特定 。如需組態選項的詳細資訊,請參閱下一節

$listeners

陣列

用於監控此上傳的TransferListener物件陣列。

$progressTracker

TransferListener

此上傳的進度追蹤器。

SDK 會從 S3 Transfer Manager 的組態解析$config預設值。

選項 Type 必要 Description

multipart_upload_threshold_bytes

int

在觸發分段上傳時覆寫 的預設閾值。

target_part_size_bytes

int

以位元組為單位覆寫預設目標部分大小。

track_progress

bool

覆寫預設選項以啟用進度追蹤。如果此選項是 true且您未提供 progressTracker 參數,則 SDK 會使用預設實作。

concurrency

int

覆寫並行的預設值。

request_checksum_calculation

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

$source

string|array|null

要從 S3 下載的物件。您可以將此參數提供為 S3 URI 字串 ("s3://amzn-s3-demo-bucket/key")、具有儲存貯體和金鑰的陣列,或 null。當此值為 時null,請在 $downloadRequestArgs 參數中傳遞儲存貯體和金鑰值。

$downloadRequestArgs

陣列

其他下載物件請求引數。如果 $sourcenull,請在此處提供儲存貯體和金鑰值。

$config

陣列

組態會覆寫此下載的特定組態。如需組態選項的詳細資訊,請參閱下一節

$downloadHandler

AbstractDownloadHandler<新增連結>|null

接收每個物件區塊並管理其下載的處理常式。

download 方法的預設處理常式會使用串流型處理常式。此處理常式會將每個物件區塊推送至串流。

downloadFile 方法的預設處理常式 (請參閱 將 S3 物件下載至本機檔案) 使用以檔案為基礎的處理常式。此處理常式會將每個區塊寫入檔案。

您可以實作自己的 DownloadHandler來自訂 SDK 存放下載資料的位置和方式。例如,您可以將資料存放在資料庫、雲端儲存或自訂處理管道中,而不是檔案或串流。

$progressTracker

TransferListener

此下載的進度追蹤器。

$listeners

array

用於監控此下載的AbstractTransferListener物件陣列。

SDK 會從 S3 Transfer Manager 的組態解析$config預設值。

選項 Type 必要 描述

multipart_download_type

string

覆寫預設分段下載類型。有效值為 'part'、'ranged'

response_checksum_validation

string

覆寫 Transfer Manager 組態中解析的值,決定是否應完成檢查總和驗證。只有當 $getObjectRequestArgs的 中ChecksumMode不存在 時,軟體開發套件才會考慮此選項DownloadRequest

track_progress

bool

覆寫啟用進度追蹤的預設選項。如果此選項為 ,true且您未搭配 SDK 提供progressTracker參數,DownloadRequest,則 會使用預設實作。

使用此選項可在 $progressTracker 參數為 時啟用預設進度追蹤器null

target_part_size_bytes

int

要在範圍分段下載中使用的分段大小,以位元組為單位。如果您未提供此參數,下載會使用在傳輸管理員上target_part_size_bytes設定的 。

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 必要 描述

$destination

string

儲存檔案的本機路徑。

$failsWhenDestinationExists

bool

如果目的地檔案已存在,是否失敗。如果此選項為 false且目的地檔案存在,則 SDK 會刪除或覆寫現有的檔案。

$downloadRequest

DownloadRequest

設定的DownloadRequest物件。如需詳細資訊,請參閱 DownloadRequest 物件

downloadFile方法成功執行時,它會傳回 DownloadResult <add link>