AWS Code Sample


PutObjectServiceOperations.php demonstrates how to send a file (or object) to an Amazon S3 bucket with an actionable result object returned.

<?php /** * Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * * ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at * * */ require 'vendor/autoload.php'; use Aws\S3\S3Client; /** * Put an Object inside Amazon S3 Bucket. * * This code expects that you have AWS credentials set up per: * */ // Use the us-east-2 region and latest version of each client. $sharedConfig = [ 'profile' => 'default', 'region' => 'us-east-2', 'version' => 'latest' ]; // Create an SDK class used to share configuration across clients. $sdk = new Aws\Sdk($sharedConfig); // Use an Aws\Sdk class to create the S3Client object. $s3Client = $sdk->createS3(); // Send a PutObject request and get the result object. $result = $s3Client->putObject([ 'Bucket' => 'my-bucket', 'Key' => 'my-key', 'Body' => 'this is the body!' ]); // Download the contents of the object. $result = $s3Client->getObject([ 'Bucket' => 'my-bucket', 'Key' => 'my-key' ]); // Print the body of the result by indexing into the result object. echo $result['Body'];

Sample Details

Service: s3

Last tested: 2018-09-20

Author: jschwarzwalder (AWS)

Type: full-example

On this page: