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-uploading-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
// Upload data.
20
$result = $s3->putObject([
21
'Bucket' => $bucket,
22
'Key' => $keyname,
23
'Body' => 'Hello, world!',
24
'ACL' => 'public-read'
25
]);
26
27
// Print the URL to the object.
28
echo $result['ObjectURL'] . PHP_EOL;
29
} catch (S3Exception $e) {
30
echo $e->getMessage() . PHP_EOL;
31
}
32
33