翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
ファイルオペレーション
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 個のパラメータ
| パラメータ | タイプ | 必須 | 説明 |
|---|---|---|---|
|
|
文字列| |
あり |
アップロードするデータを含むローカルファイルパスまたはストリームのパス。 |
|
|
配列 |
あり |
アップロードリクエスト引数 (バケットとキーを含める必要があります)。 |
|
|
配列 |
不可 |
このアップロードに固有の設定オーバーライド。設定オプションの詳細については、次のセクションを参照してください。 |
|
|
配列 |
不可 |
このアップロードをモニタリングするための |
|
|
TransferListener |
不可 |
このアップロードの進行状況トラッカー。 |
SDK は、S3 Transfer Manager の設定から$configデフォルト値を解決します。
| オプション | タイプ | 必須 | 説明 |
|---|---|---|---|
|
|
int |
不可 |
マルチパートアップロードがトリガーされるときのデフォルトのしきい値を上書きします。 |
|
|
int |
不可 |
デフォルトのターゲットパーツサイズをバイト単位で上書きします。 |
|
|
ブール |
不可 |
デフォルトのオプションを上書きして、進行状況の追跡を有効にします。このオプションが trueで、progressTrackerパラメータを指定しない場合、SDK はデフォルトの実装を使用します。 |
|
|
int |
不可 |
同時実行のデフォルト値を上書きします。 |
|
|
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 パラメータ
| パラメータ | タイプ | 必須 | 説明 |
|---|---|---|---|
|
|
string|array| |
不可 |
S3 からダウンロードするオブジェクト。このパラメータは、S3 URI 文字列 ("s3://amzn-s3-demo-bucket/key")、バケットキーとキーキーを持つ配列、または として指定できます |
|
|
配列 |
不可 |
追加のダウンロードオブジェクトリクエスト引数。 |
|
|
配列 |
不可 |
このダウンロードに固有の設定オーバーライド。設定オプションの詳細については、次のセクションを参照してください。 |
$downloadHandler |
|
不可 |
各オブジェクトチャンクを受け取り、ダウンロードを管理するハンドラー。
独自の を実装 |
|
|
|
不可 |
このダウンロードの進行状況トラッカー。 |
|
|
|
不可 |
このダウンロードをモニタリングするための |
SDK は、S3 Transfer Manager の設定から$configデフォルト値を解決します。
| オプション | タイプ | 必須 | 説明 |
|---|---|---|---|
|
|
string |
不可 |
デフォルトのマルチパートダウンロードタイプを上書きします。有効な値は「part」、「ranged」です |
|
|
string |
不可 |
チェックサムの検証を行うかどうかについて、Transfer Manager 設定から解決された値を上書きします。SDK は、 |
|
|
ブール |
不可 |
進行状況の追跡を有効にするためのデフォルトのオプションを上書きします。このオプションが パラメータが |
|
|
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 個のパラメータ
| オプション | タイプ | 必須 | 説明 |
|---|---|---|---|
|
|
文字列 |
あり |
ファイルが保存されるローカルパス。 |
|
|
ブール |
あり |
送信先ファイルが既に存在する場合に失敗するかどうか。このオプションが |
|
|
|
あり |
設定された |
downloadFile メソッドが正常に実行されると、DownloadResult<add link> が返されます。