简体   繁体   English

在Codeigniter中找不到类“ WriteRequestBatch”错误-AWS PHP PHP

[英]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. 抱歉,我是AWS SDK的新手,但是我需要使用WriteRequestBatch将25条记录的批量添加到一个表中。

I'm using Codeigniter and trying to do it with this code: 我正在使用Codeigniter并尝试使用以下代码进行操作:

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 :-( 我刚刚开始使用新的SDK,并且能够获取数据和更新表吞吐量设置等,仅此任务对我完全失败:-(

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. 顺便说一句-这是我在这里的第一篇文章,我尝试在Google等上搜索答案,但只找到了我已经使用过的相同示例代码。

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: 然后将其加载到其中模型new_save()为的模型中:

$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. 每次使用aws_sdk库时,都将执行此操作。

Hope this helps. 希望这可以帮助。

This is not a problem with the AWS SDK or with CI. AWS开发工具包或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). 在引用它们的同一文件中,在文件顶部/附近(在任何类或函数声明之外)添加use语句。

<?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. 如果您想更好地了解如何使用命名空间代码,应该考虑阅读PHP手册中命名空间部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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