2018-10-11 15:00:23 -07:00
< ? php
2018-12-28 10:35:52 -08:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2018-10-11 15:00:23 -07:00
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
/*
* ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-examples-creating-buckets.html
*
*/
2019-02-28 14:20:42 -08:00
// snippet-start:[s3.php.put_object.complete]
// snippet-start:[s3.php.put_object.import]
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
use Aws\S3\Exception\S3Exception ;
use Aws\S3\S3Client ;
2024-02-05 10:49:20 -07:00
2019-02-28 14:20:42 -08:00
// snippet-end:[s3.php.put_object.import]
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
/**
* Put an Object inside Amazon S3 Bucket.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
2024-02-05 10:49:20 -07:00
2019-02-28 14:20:42 -08:00
// snippet-start:[s3.php.put_object.main]
2018-10-11 15:00:23 -07:00
$USAGE = " \n " .
" To run this example, supply the name of an S3 bucket and a file to \n " .
" upload to it. \n " .
" \n " .
" Ex: php PutObject.php <bucketname> <filename> \n " ;
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
if ( count ( $argv ) <= 2 ) {
echo $USAGE ;
exit ();
}
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
$bucket = $argv [ 1 ];
$file_Path = $argv [ 2 ];
$key = basename ( $argv [ 2 ]);
2024-02-05 10:49:20 -07:00
2018-10-11 15:00:23 -07:00
try {
//Create a S3Client
$s3Client = new S3Client ([
'profile' => 'default' ,
'region' => 'us-west-2' ,
'version' => '2006-03-01'
]);
$result = $s3Client -> putObject ([
'Bucket' => $bucket ,
'Key' => $key ,
'SourceFile' => $file_Path ,
]);
} catch ( S3Exception $e ) {
echo $e -> getMessage () . " \n " ;
}
2024-02-05 10:49:20 -07:00
2019-02-28 14:20:42 -08:00
// snippet-end:[s3.php.put_object.main]
// snippet-end:[s3.php.put_object.complete]