aws / aws-sdk-php-zf2

ZF2 module for using the AWS SDK for PHP to interact with AWS services like S3, DynamoDB, SQS, EC2, etc.
http://aws.amazon.com/sdkforphp/
Apache License 2.0
103 stars 63 forks source link

How to use Module? #12

Closed thinhpc closed 10 years ago

thinhpc commented 10 years ago

Hi everyone, i read and did follow ReadMe document:

  1. use composer to get module
  2. create table to save session from amazon console.
  3. copy and change config key, secret, region, table_name in aws.local.php
  4. in application.config.php i added: return array( 'modules' => array( 'Aws', ), (and so on)
  5. in Module.php: public function onBootstrap(MvcEvent $e) { $eventManager = $e->getApplication()->getEventManager(); $moduleRouteListener = new ModuleRouteListener(); $moduleRouteListener->attach($eventManager);

    $saveHandler = $e->getApplication()->getServiceManager()->get('Aws\Session\SaveHandler\DynamoDb');
    $sessionManager = new SessionManager();
    $sessionManager->setSaveHandler($saveHandler);
    $sessionManager->start();
    Container::setDefaultManager($sessionManager);

    } ......

  6. in Controller Action i use session: $this->userSession = new Container('default'); //set session $this->userSession->offsetSet('user','Tony');

//get session: return $this->userSession->offsetGet('user');

But it not work at all, session not be save. Please help me to discuss my mistake ? Thanks all !

jeremeamia commented 10 years ago

@fdewinne or @gwis, do you have any ideas on how to help @thinhpc with the Save Handler?

gws commented 10 years ago

Hey @thinhpc - can you post the contents of your aws_zf2.local.php and/or aws.local.php with any secret keys or private information removed? The config/aws_zf2.local.php.dist distributed with this project contains configuration information for ZF2-specific AWS configuration (the DynamoDB session save handler is one such part), and it is not clear from your report that you configured it.

Thanks!

hung12a1 commented 10 years ago

Hi @gwis , below is the config i and @thinhpc are using <?php

return array( /* * You can define global configuration settings for the SDK as an array. Typically, you will want to a provide your * credentials (key and secret key) and the region (e.g. us-west-2) in which you would like to use your services. / 'aws' => array( 'key' => 'xxxx', 'secret' => 'xxxx', 'region' => 'ap-southeast-2' ),
'aws_zf2' => array( 'session' => array( 'save_handler' => array( 'dynamodb' => array( // Locking strategy used for doing session locking. 'locking_strategy' => null,

                // DynamoDb client object used for performing DynamoDB
                // operations.
                //
                // Note: you most likely want to leave this alone and allow
                // the factory to fetch your configured instance of
                // DynamoDB. However, if you override it with an object, we
                // will respect that choice.
                'dynamodb_client' => null,

                // Name of the DynamoDB table in which to store the
                // sessions.
                'table_name' => 'sessions',

                // Name of the hash key in the DynamoDB sessions table.
                'hash_key' => 'id',

                // Lifetime of an inactive session before it should be
                // garbage collected. Similar to PHP's
                // session.gc_maxlifetime
                'session_lifetime' => 3600,

                // Whether or not to use DynamoDB consistent reads for
                // GetItem.
                'consistent_read' => true,

                // Whether or not to use PHP's session auto garbage
                // collection triggers.
                //
                // Note that you may want this to be false in production,
                // and use a separate process to garbage collect old
                // sessions.
                'automatic_gc' => false,

                // Batch size used for removing expired sessions during
                // garbage collection.
                'gc_batch_size' => 25,

                // Delay between service operations during garbage
                // collection.
                'gc_operation_delay' => 10,

                // Maximum time (in seconds) to wait to acquire a lock before giving up
                'max_lock_wait_time' => 10,

                // Minimum time (in microseconds) to wait between attempts to acquire a lock
                'min_lock_retry_microtime' => 10000,

                // Maximum time (in microseconds) to wait between attempts to acquire a lock
                'max_lock_retry_microtime' => 50000,
            )
        )
    )
),

/**
 * You can alternatively provide a path to an AWS SDK for PHP config file containing your configuration settings.
 * Config files can allow you to have finer-grained control over the configuration settings for each service client.
 */
// 'aws' => 'path/to/your/aws-config.php'

);

hung12a1 commented 10 years ago

The sessions is not saved to dynamodb

gws commented 10 years ago

I've seen silent failures like this when the DynamoDB table does not exist, or your credentials are input incorrectly.

Please do the following:

If it's still not working, I just grabbed the ZendSkeletonApplication and created a branch containing the minimal changes necessary to get a DynamoDB save handler working, and it did (for me). If you want, you can check out my branch here:

git clone https://github.com/gwis/ZendSkeletonApplication.git
cd ZendSkeletonApplication
git checkout aws-sdk-php-zf2-12
php composer.phar install

Next, you will need to modify config/autoload/local.config.php to contain your credentials and AWS region.

If you are on PHP 5.4, you can then start a webserver in public:

cd public
php -S localhost:8080

browse to localhost:8080. You should see a gray box near the top that says NULL - which is the data in the session. If you refresh again, you should see Tony.

hung12a1 commented 10 years ago

Thanks @gwis, I have found the problem is that table on dynamodb prevent write into its.

hung12a1 commented 10 years ago

Hi @gwis, Everything works now except i got many records in table have this prefix "_REQUEST_ACCESS_TIME". What's it?

gws commented 10 years ago

@hung12a1 Glad it's working! That is part of the Zend Framework 2 session metadata, _REQUEST_ACCESS_TIME is presumably used by higher level framework code to determine when the session was last accessed: https://github.com/zendframework/zf2/blob/master/library/Zend/Session/Storage/ArrayStorage.php

The DynamoDB session handler does not touch the ZF2 session data, so if you have further questions about the ZF2 session implementation, you should direct them at IRC or the mailing lists: http://framework.zend.com/participate/contributor-guide

hung12a1 commented 10 years ago

@gwis As now, we are running on 2 servers, and see that it write so many records on dynamodb(it has never occured if run on 1 server). Do you have any idea about this problem?

gws commented 10 years ago

@hung12a1 How many is so many? What, specifically, has never occurred when running on 1 server?

hung12a1 commented 10 years ago

@gwis As now, my site is running on 2 machines, we are testing, and only have 4-5 people testing, but when i look into dynamodb i see it has hundreds records (session time out is 1 hour and we only test in one hour)

jeremeamia commented 10 years ago

You should run garbage collection periodically to clean up expired sessions. Please see the Garbage Collection section of the DynamoDB Session Handler guide.

@gwis Thanks for offering your help.

gws commented 10 years ago

@hung12a1 Check that cookie settings are correct in your app and in the testing browsers. If they aren't, each request may create a new session, causing the behavior you see. I think further questions would be best directed at the ZF2 mailing list or IRC - these are not issues with DynamoDB or the AWS SDK module for ZF2 :)

hung12a1 commented 10 years ago

@gwis thank so much for your replies.

jeremeamia commented 10 years ago

Closing due to inactivity. Thanks again @gwis.