File operations
S3 Transfer Manager provides methods for uploading and downloading individual files.
Upload local files
To upload a file to Amazon S3, use the upload
<add link> method.
<?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 method
parameters
The upload method accepts an instance of
UploadRequest
<add link> as an argument.
UploadRequest
parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
|
|
string| |
Yes |
Path of local file path or stream containing data to upload. |
|
|
array |
Yes |
Upload request arguments (must include Bucket and Key). |
|
|
array |
No |
Configuration overrides specific to this upload. For more information about configuration options, see the following section. |
|
|
array |
No |
Array of |
|
|
TransferListener |
No |
A progress tracker for this upload. |
The SDK resolves default $config value from the configuration of the S3
Transfer Manager.
| Option | Type | Required | Description |
|---|---|---|---|
|
|
int |
No |
Overrides the default threshold for when a multipart upload is triggered. |
|
|
int |
No |
Overrides the default target part size in bytes. |
|
|
bool |
No |
Overrides the default option to enable progress
tracking. If this option is true and
you don't provide a progressTracker
parameter, the SDK uses a default
implementation. |
|
|
int |
No |
Overrides the default value for concurrency. |
|
|
string |
No |
Overrides the default value for whether a request checksum calculation must be performed. |
When the upload method runs successfully, it returns an
UploadResult
<add link>.
Download S3 objects
To download an object from Amazon S3, use the download
<add link> method.
<?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 method
parameters
The download method accepts an instance of
DownloadRequest
<add link> as an argument.
DownloadRequest parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
|
|
string|array| |
No |
The object to download from S3. You can provide this
parameter as an S3 URI string
("s3://amzn-s3-demo-bucket/key"), an array with Bucket
and Key keys, or |
|
|
array |
No |
Additional download object request arguments. If
|
|
|
array |
No |
Configuration overrides specific to this download. For more information about configuration options, see the following section. |
$downloadHandler |
|
No |
The handler that receives each object chunk and manages its download. The default handler for the The default handler for the You can implement your own
|
|
|
|
No |
A progress tracker for this download. |
|
|
|
No |
Array of |
The SDK resolves default $config value from the configuration of the S3
Transfer Manager.
| Option | Type | Required | Description |
|---|---|---|---|
|
|
string |
No |
Overrides the default multipart download type. Valid values are 'part', 'ranged' |
|
|
string |
No |
Overrides the resolved value from transfer
manager config for whether checksum validation
should be done. The SDK considers this option only
if |
|
|
bool |
No |
Overrides the default option for enabling
progress tracking. If this option is
Use this option to enable a default progress
tracker when the |
|
|
int |
No |
The part size in bytes to use in a range
multipart download. If you don't provide this
parameter, the download uses the
|
When the download method runs successfully, it returns an
DownloadResult
<add link>.
Download an S3 object to a local file
To download an S3 object directly to a local file, use the
downloadFile method:
<?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 method
parameters
The downloadFile method accepts an instance of
DownloadFileRequest as an argument.
DownloadFileRequest parameters
| Option | Type | Required | Description |
|---|---|---|---|
|
|
string |
Yes |
Local path where the file is saved. |
|
|
bool |
Yes |
Whether to fail if the destination file already
exists. If this option is |
|
|
|
Yes |
The configured |
When the downloadFile method runs successfully, it returns a
DownloadResult
<add link>.