Giter Club home page Giter Club logo

aws-sdk-php-zf2's Introduction

AWS SDK ZF2 Module

Introduction

This module provides a simple wrapper for the AWS SDK for PHP. It registers the AWS service builder as a service in the ZF2 service manager, making it easily accessible anywhere in your application.

Installation

Install the module using Composer into your application's vendor directory. Add the following line to your composer.json. This will also install the AWS SDK for PHP.

{
    "require": {
        "aws/aws-sdk-php-zf2": "1.0.*"
    }
}

Configuration

Enable the module in your application.config.php file.

return array(
    'modules' => array(
        'Aws'
    )
);

Copy and paste the aws.local.php.dist file to your config/autoload folder and customize it with your credentials and other configuration settings. Make sure to remove .dist from your file. Your aws.local.php might look something like the following:

<?php

return array(
    'aws' => array(
        'key'    => '<your-aws-access-key-id>',
        'secret' => '<your-aws-secret-access-key>',
        'region' => 'us-west-2'
    )
);

NOTE: If you are using IAM Instance Profile credentials (also referred to as IAM Roles for instances), you can omit your key and secret parameters since they will be fetched from the Amazon EC2 instance automatically.

Usage

You can get the AWS service builder object from anywhere that the ZF2 service locator is available (e.g. controller classes). The following example instantiates an Amazon DynamoDB client and creates a table in DynamoDB.

public function indexAction()
{
    $aws    = $this->getServiceLocator()->get('aws');
    $client = $aws->get('dynamodb');

    $table = 'posts';

    // Create a "posts" table
    $result = $client->createTable(array(
        'TableName' => $table,
        'KeySchema' => array(
            'HashKeyElement' => array(
                'AttributeName' => 'slug',
                'AttributeType' => 'S'
            )
        ),
        'ProvisionedThroughput' => array(
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 5
        )
    ));

    // Wait until the table is created and active
    $client->waitUntilTableExists(array('TableName' => $table));

    echo "The {$table} table has been created.\n";
}

View Helpers

Starting from version 1.0.2, the AWS SDK ZF2 Module now provides two view helpers to generate links for Amazon S3 and Amazon CloudFront resources.

Note: Both of the view helpers generate URLs with an HTTPS scheme by default. This is ideal for security, but please keep in mind that Amazon CloudFront charges more for HTTPS requests. You can turn SSL off by calling the setUseSsl method on both helpers.

S3Link View Helper

To create a S3 link in your view:

<?php echo $this->s3Link('my-object', 'my-bucket');

The default bucket can be set globally by using the setDefaultBucket method:

<?php
    $this->s3Link->setDefaultBucket('my-bucket');
    echo $this->s3Link('my-object');

You can also create signed URLs for private content by passing a third argument which is the expiration date:

<?php echo $this->s3Link('my-object', 'my-bucket', '+10 minutes');

CloudFrontLink View Helper

To create CloudFront link in your view:

<?php echo $this->cloudFrontLink('my-object', 'my-domain');

The default domain can be set globally by using the setDefaultDomain method:

<?php
    $this->cloudFrontLink->setDefaultDomain('my-domain');
    echo $this->cloudFrontLink('my-object');

You can also create signed URLs for private content by passing a third argument which is the expiration date:

<?php echo $this->cloudFrontLink('my-object', 'my-bucket', time() + 60);

Related Modules

The following are some ZF2 modules that use the AWS SDK for PHP by including this module:

  • SlmMail - Module that allow to send emails with various providers (including Amazon SES)
  • SlmQueueSqs โ€“ Module that simplifies the use of Amazon SQS

Links

aws-sdk-php-zf2's People

Contributors

bakura10 avatar jeremeamia avatar evandotpro avatar

Stargazers

Victor Guedes avatar

Watchers

Victor Guedes avatar James Cloos 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.