PHP SDK 的 S3 批量/并行上传错误

S3 Batch / Parallel Upload Error with PHP SDK

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

Argument 1 passed to Aws\AwsClient::execute() must implement interface Aws\CommandInterface, array given

$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);

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

$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 SDK 的版本 2.x 中正常工作。对于3.x,你需要使用CommandPool()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应该是一个命令结果数组。