7

我按照这些代码批量PutObject进入 S3。我正在使用最新的 PHP SDK (3.x)。但我得到:

传递给 Aws\AwsClient::execute() 的参数 1 必须实现接口 Aws\CommandInterface,给定数组

$commands = array();
$commands[] = $s3->getCommand('PutObject', array(
    'Bucket' => $bucket,
    'Key'    => 'images/1.jpg',
    'Body' => base64_decode( 'xxx' ),
    'ACL' => 'public-read',
    'ContentType' => 'image/jpeg'
));

$commands[] = $s3->getCommand('PutObject', array(
    'Bucket' => $bucket,
    'Key'    => 'images/2.jpg',
    'Body' => base64_decode( 'xxx' ),
    'ACL' => 'public-read',
    'ContentType' => 'image/jpeg'
));

// Execute the commands in parallel
$s3->execute($commands);
4

1 回答 1

6

如果您使用的是现代版本的 SDK,请尝试以这种方式构建命令。直接取自文档;它应该工作。这称为“链接”方法。

$commands = array();


$commands[] = $s3->getCommand('PutObject')
                ->set('Bucket', $bucket)
                ->set('Key', 'images/1.jpg')
                ->set('Body', base64_decode('xxx'))
                ->set('ACL', 'public-read')
                ->set('ContentType', 'image/jpeg');

$commands[] = $s3->getCommand('PutObject')
                ->set('Bucket', $bucket)
                ->set('Key', 'images/2.jpg')
                ->set('Body', base64_decode('xxx'))
                ->set('ACL', 'public-read')
                ->set('ContentType', 'image/jpeg');

// Execute the commands in parallel
$s3->execute($commands);

// Loop over the commands, which have now all been executed
foreach ($commands as $command)
{
    $result = $command->getResult();

    // Use the result.
}

确保您使用的是最新版本的 SDK。

编辑

似乎 SDK 的 API 在版本 3.x 中发生了显着变化。上面的示例应该在 AWS 开发工具包的 2.x 版中正常工作。对于 3.x,您需要使用CommandPool()s 和promise()

$commands = array();

$commands[] = $s3->getCommand('PutObject', array(
    'Bucket' => $bucket,
    'Key'    => 'images/1.jpg',
    'Body' => base64_decode ( 'xxx' ),
    'ACL' => 'public-read',
    'ContentType' => 'image/jpeg'
));
$commands[] = $s3->getCommand('PutObject', array(
    'Bucket' => $bucket,
    'Key'    => 'images/2.jpg',
    'Body' => base64_decode ( 'xxx' ),
    'ACL' => 'public-read',
    'ContentType' => 'image/jpeg'
));


$pool = new CommandPool($s3, $commands);

// Initiate the pool transfers
$promise = $pool->promise();

// Force the pool to complete synchronously
try {
    $result = $promise->wait();
} catch (AwsException $e) {
    // handle the error.
}

然后,$result应该是一个命令结果数组。

于 2015-07-10T07:24:17.180 回答