Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/php_examples/S3examples/s3-downloading-object.php
4084 views
1
<?php
2
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE )
4
5
require 'vendor/autoload.php';
6
7
use Aws\S3\S3Client;
8
use Aws\S3\Exception\S3Exception;
9
10
$bucket = '*** Your Bucket Name ***';
11
$keyname = '*** Your Object Key ***';
12
13
$s3 = new S3Client([
14
'version' => 'latest',
15
'region' => 'us-east-1'
16
]);
17
18
try {
19
// Get the object.
20
$result = $s3->getObject([
21
'Bucket' => $bucket,
22
'Key' => $keyname
23
]);
24
25
// Display the object in the browser.
26
header("Content-Type: {$result['ContentType']}");
27
echo $result['Body'];
28
} catch (S3Exception $e) {
29
echo $e->getMessage() . PHP_EOL;
30
}
31
32