Code Monkey home page Code Monkey logo

amazon-es-php's Introduction

AWS Auth Elasticsearch-PHP Handler

Apache 2 License Total Downloads Author Build Status

This package provides a signing handler for use with the official Elasticsearch-PHP (elasticsearch/elasticsearch) client. By default, the handler will load AWS credentials from the environment and send requests using a RingPHP cURL handler.

Basic Usage

Instances of Aws\ElasticsearchService\ElasticsearchPhpHandler are callables that fulfill Elasticsearch-PHP's handler contract. They can be passed to Elasticsearch\ClientBuilder's setHandler method:

use Aws\ElasticsearchService\ElasticsearchPhpHandler;
use Elasticsearch\ClientBuilder;

// Create a handler (with the region of your Amazon Elasticsearch Service domain)
$handler = new ElasticsearchPhpHandler('us-west-2');

// Use this handler to create an Elasticsearch-PHP client
$client = ClientBuilder::create()
    ->setHandler(new ElasticsearchPhpHandler('us-west-2'))
    ->setHosts(['https://search-foo-3gn4utxfus5cqpn89go4z5lbsm.us-west-2.es.amazonaws.com:443'])
    ->build();

// Use the client as you normally would
$client->index([
    'index' => $index,
    'type' => $type,
    'id' => $id,
    'body' => [$key => $value]
]);

Using custom credentials

By default, the handler will attempt to source credentials from the environment as described in the AWS SDK for PHP documentation. To use custom credentials, pass in a credential provider:

use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
use Aws\ElasticsearchService\ElasticsearchPhpHandler;

$provider = CredentialProvider::fromCredentials(
    new Credentials('foo', 'bar', 'baz')
);

$handler = new ElasticsearchPhpHandler('us-west-2', $provider);

Using a custom HTTP handler

By default, the handler will use Elasticsearch\ClientBuilder::defaultHandler() to dispatch HTTP requests, but this is customizable via an optional constructor parameter. For example, this repository's tests use a custom handler to mock network traffic:

class ElasticsearchPhpHandlerTest extends \PHPUnit_Framework_TestCase
{
    public function testSignsRequestsPassedToHandler()
    {
        $toWrap = function (array $ringRequest) {
            $this->assertArrayHasKey('X-Amz-Date', $ringRequest['headers']);
            $this->assertArrayHasKey('Authorization', $ringRequest['headers']);
            $this->assertStringStartsWith(
                'AWS4-HMAC-SHA256 Credential=',
                $ringRequest['headers']['Authorization'][0]
            );

            return $this->getGenericResponse();
        };
        $handler = new ElasticsearchPhpHandler('us-west-2', null, $toWrap);

        $client = \Elasticsearch\ClientBuilder::create()
            ->setHandler($handler)
            ->build();

        $client->get([
            'index' => 'index',
            'type' => 'type',
            'id' => 'id',
        ]);
    }
    ...
}

amazon-es-php's People

Contributors

jeskew avatar

Watchers

James Cloos avatar Geoffrey THENOT avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.