本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
文件操作
S3 传输管理器提供了上传和下载单个文件的方法。
上传本地文件
要将文件上传到 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方法接受的实例UploadRequest<add link>作为参数。
UploadRequest 参数
| 参数 | Type | 必需 | 说明 |
|---|---|---|---|
|
|
字符串| |
是 |
本地文件路径或包含待上传数据的流的路径。 |
|
|
array |
是 |
上传请求参数(必须包含存储桶和密钥)。 |
|
|
array |
否 |
特定于此上传的配置替换。有关配置选项的更多信息,请参阅以下部分。 |
|
|
array |
否 |
用于监视此上传的 |
|
|
TransferListener |
否 |
此次上传的进度跟踪器。 |
SDK 解析 S3 传输管理器配置中的默认$config值。
| Option | Type | 必需 | 说明 |
|---|---|---|---|
|
|
int |
否 |
覆盖触发分段上传时的默认阈值。 |
|
|
int |
否 |
覆盖默认的目标段大小(以字节为单位)。 |
|
|
布尔 |
否 |
覆盖默认选项以启用进度跟踪。如果此选项是,true而您未提供progressTracker参数,则 SDK 将使用默认实现。 |
|
|
int |
否 |
覆盖并发的默认值。 |
|
|
字符串 |
否 |
覆盖是否必须执行请求校验和计算的默认值。 |
当该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方法接受的实例DownloadRequest<add link>作为参数。
DownloadRequest 参数
| 参数 | Type | 必需 | 说明 |
|---|---|---|---|
|
|
字符串|数组| |
否 |
要从 S3 下载的对象。您可以将此参数作为 S3 URI 字符串 (” s3://amzn-s3-demo-bucket/key “)、包含存储桶和密钥密钥的数组或 |
|
|
array |
否 |
其他下载对象请求参数。如果 |
|
|
array |
否 |
此下载专用的配置替换。有关配置选项的更多信息,请参阅以下部分。 |
$downloadHandler |
|
否 |
接收每个对象块并管理其下载的处理程序。 该 该 您可以自己实现 |
|
|
|
否 |
此下载的进度跟踪器。 |
|
|
|
否 |
用于监视此下载的 |
SDK 解析 S3 传输管理器配置中的默认$config值。
| Option | Type | 必需 | 描述 |
|---|---|---|---|
|
|
字符串 |
否 |
覆盖默认的分段下载类型。有效值为 “部分”、“远程” |
|
|
字符串 |
否 |
覆盖传输管理器配置中的解析值,以确定是否应进行校验和验证。只有在中不存在此选项 |
|
|
布尔 |
否 |
覆盖启用进度跟踪的默认选项。如果此选项是, 当 |
|
|
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 参数
| Option | Type | 必需 | 描述 |
|---|---|---|---|
|
|
字符串 |
是 |
保存文件的本地路径。 |
|
|
布尔 |
是 |
如果目标文件已经存在,则是否失败。如果此选项为 |
|
|
|
是 |
配置的 |
当该downloadFile方法成功运行时,它将返回DownloadResult<add link>。