AWS Code Sample
Catalog

MockHandler.php

MockHandler.php demonstrates how to list Objects in an new Amazon S3 bucket using a mock handler.

<?php /** * Copyright 2010-2019 Amazon.com, 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 * * http://aws.amazon.com/apache2.0/ * * 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 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_commands.html * */ use Aws\Result; use Aws\MockHandler; /** * Create a MockHandler to list all the files in an 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 */ // Create a mock handler $mock = new MockHandler(); // Enqueue a mock result to the handler $mock->append(new Result(['foo' => 'bar'])); // Create a "ListObjects" command $command = $s3Client->getCommand('ListObjects'); // Associate the mock handler with the command $command->getHandlerList()->setHandler($mock); // Executing the command will use the mock handler, which will return the // mocked result object $result = $client->execute($command); echo $result['foo']; // Outputs 'bar'

Sample Details

Service: s3

Last tested:

Author: jschwarzwalder (AWS)

Type: full-example

On this page: