简体   繁体   中英

Class 'WriteRequestBatch' not found error in Codeigniter - AWS php SDK

Sorry everyone, I'm kind of new to AWS SDK but I need to use WriteRequestBatch to add bulks of 25 records to one table.

I'm using Codeigniter and trying to do it with this code:

function new_save($data_set)
    {
    $tableName = 'my-table';
    $dynamodb = $this->aws_sdk->dynamo_db();
    $data_to_save = $this->create_dynamo_data($data_set);
    $putBatch = WriteRequestBatch::factory($dynamodb);
    foreach ($data_to_save as $record)
    {
        $record = Item::fromArray($item);
        $putBatch->add(new PutRequest($record, $tableName));
    }
    $putBatch->flush();
}

but it stops with this error:

PHP Fatal error:  Class 'WriteRequestBatch' not found in (...)

I've just started to use new SDK and I am able to get data and update table throughput settings etc, only this task fails on me totally :-(

BTW - this is my first post here, and I tried to search on Google etc for answers but found only the same sample code I use already.

I created a library like this:

require('/var/www/xx-aslan/aws_sdk_ver2/aws-autoloader.php');
use Aws\Common\Aws;

class Aws_sdk
{
// Create a service locator using a configuration file
private static $aws = array(
        'key'    => '***********',
        'secret' => '******',
        'region' => '****'
);

function aws()
{
    return Aws::factory(self::$aws);
}

function dynamo_db()
{
    $aws = $this->aws();
    return $aws->get('DynamoDb');
}

and I am loading it in the model where the function new_save() is:

$this->load->library('aws_sdk');

Anyone could help me out here? I know it's probably some really newbie question, sorry :-(

Thanks in advance! Kasia

If it does not work to change:

$this->load->library('aws_sdk');

to

$this->load->library('Aws_sdk');

as I suggested in the comments, try calling it like:

$putBatch = aws_sdk->WriteRequestBatch::factory($dynamodb);

You would do this every time you use your aws_sdk library.

Hope this helps.

This is not a problem with the AWS SDK or with CI. The classes are namespaced, so you must import them to make them available.

In the same file that you are referencing them, add use statements at/near the top of the file (outside of any class or function declarations).

<?php

// ...

use Aws\DynamoDb\Model\BatchRequest\PutRequest;
use Aws\DynamoDb\Model\BatchRequest\WriteRequestBatch;
use Aws\DynamoDb\Model\Item;

// ...

function new_save($data_set)
{
    // ...
    $putBatch = WriteRequestBatch::factory($dynamodb);
    // ...
}

// ...

You should consider giving the namespaces section in the PHP manual a read if you want to get a better understanding of how to work with namespaced code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM