使用PHP SDK进行S3批处理/并行上载错误

twb*_*twb 6 amazon-s3 aws-php-sdk aws-sdk

我按照这些代码批量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);
Run Code Online (Sandbox Code Playgroud)

Wil*_*ill 5

如果您使用的是最新版的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.
}
Run Code Online (Sandbox Code Playgroud)

确保您使用的是最新版本的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.
}
Run Code Online (Sandbox Code Playgroud)

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