ファイルオペレーション - AWS SDK for PHP

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ファイルオペレーション

S3 Transfer Manager には、個々のファイルをアップロードおよびダウンロードする方法が用意されています。

ローカルファイルをアップロードする

Amazon S3 にファイルをアップロードするには、upload<add link> メソッドを使用します。

<?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 メソッドは <UploadRequestadd link> のインスタンスを引数として受け入れます。

UploadRequest 個のパラメータ

パラメータ タイプ 必須 説明

$source

文字列|StreamInterface

あり

アップロードするデータを含むローカルファイルパスまたはストリームのパス。

$uploadRequestArgs

配列

あり

アップロードリクエスト引数 (バケットとキーを含める必要があります)。

$config

配列

不可

このアップロードに固有の設定オーバーライド。設定オプションの詳細については、次のセクションを参照してください。

$listeners

配列

不可

このアップロードをモニタリングするためのTransferListenerオブジェクトの配列。

$progressTracker

TransferListener

不可

このアップロードの進行状況トラッカー。

SDK は、S3 Transfer Manager の設定から$configデフォルト値を解決します。

オプション タイプ 必須 説明

multipart_upload_threshold_bytes

int

不可

マルチパートアップロードがトリガーされるときのデフォルトのしきい値を上書きします。

target_part_size_bytes

int

不可

デフォルトのターゲットパーツサイズをバイト単位で上書きします。

track_progress

ブール

不可

デフォルトのオプションを上書きして、進行状況の追跡を有効にします。このオプションが trueで、progressTrackerパラメータを指定しない場合、SDK はデフォルトの実装を使用します。

concurrency

int

不可

同時実行のデフォルト値を上書きします。

request_checksum_calculation

string

不可

リクエストチェックサム計算を実行する必要があるかどうかのデフォルト値を上書きします。

upload メソッドが正常に実行されると、UploadResult<add link> が返されます。

S3 オブジェクトをダウンロードする

Amazon S3 からオブジェクトをダウンロードするには、download<add link> メソッドを使用します。

<?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 メソッドは <DownloadRequestadd link> のインスタンスを引数として受け入れます。

DownloadRequest パラメータ

パラメータ タイプ 必須 説明

$source

string|array|null

不可

S3 からダウンロードするオブジェクト。このパラメータは、S3 URI 文字列 ("s3://amzn-s3-demo-bucket/key")、バケットキーとキーキーを持つ配列、または として指定できますnull。この値が の場合null、バケットとキーの値を $downloadRequestArgsパラメータに渡します。

$downloadRequestArgs

配列

不可

追加のダウンロードオブジェクトリクエスト引数。$source が の場合null、ここにバケットとキーの値を指定します。

$config

配列

不可

このダウンロードに固有の設定オーバーライド。設定オプションの詳細については、次のセクションを参照してください。

$downloadHandler

AbstractDownloadHandler<リンクの追加>|null

不可

各オブジェクトチャンクを受け取り、ダウンロードを管理するハンドラー。

download メソッドのデフォルトハンドラーは、ストリームベースのハンドラーを使用します。このハンドラーは、各オブジェクトチャンクをストリームにプッシュします。

downloadFile メソッドのデフォルトハンドラー (「」を参照S3 オブジェクトをローカルファイルにダウンロードする) は、ファイルベースのハンドラーを使用します。このハンドラーは、各チャンクをファイルに書き込みます。

独自の を実装DownloadHandlerして、SDK がダウンロードしたデータを保存する場所と方法をカスタマイズできます。たとえば、ファイルやストリームの代わりに、データベース、クラウドストレージ、カスタム処理パイプラインにデータを保存できます。

$progressTracker

TransferListener

不可

このダウンロードの進行状況トラッカー。

$listeners

array

不可

このダウンロードをモニタリングするためのAbstractTransferListenerオブジェクトの配列。

SDK は、S3 Transfer Manager の設定から$configデフォルト値を解決します。

オプション タイプ 必須 説明

multipart_download_type

string

不可

デフォルトのマルチパートダウンロードタイプを上書きします。有効な値は「part」、「ranged」です

response_checksum_validation

string

不可

チェックサムの検証を行うかどうかについて、Transfer Manager 設定から解決された値を上書きします。SDK は、 $getObjectRequestArgsの に ChecksumModeが存在しない場合にのみ、このオプションを考慮しますDownloadRequest

track_progress

ブール

不可

進行状況の追跡を有効にするためのデフォルトのオプションを上書きします。このオプションが trueで、DownloadRequest,SDK がデフォルトの実装を使用するprogressTrackerパラメータを指定しない場合。

パラメータが $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 個のパラメータ

オプション タイプ 必須 説明

$destination

文字列

あり

ファイルが保存されるローカルパス。

$failsWhenDestinationExists

ブール

あり

送信先ファイルが既に存在する場合に失敗するかどうか。このオプションが falseで、送信先ファイルが存在する場合、SDK は既存のファイルを削除または上書きします。

$downloadRequest

DownloadRequest

あり

設定されたDownloadRequestオブジェクト。詳細については、DownloadRequest「 オブジェクト」を参照してください。

downloadFile メソッドが正常に実行されると、DownloadResult<add link> が返されます。