2015-07-10 60 views
5

我跟着这些代码批量PutObject到S3中。我正在使用最新的PHP SDK(3.x)。但我越来越:S3使用PHP SDK批量/并行上传错误

参数1传递给AWS \ AwsClient ::执行()必须实现接口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

SDK,请尝试以这种方式构建命令,而不是。直接从docs;它应该工作。这被称为“链接”方法。

$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() 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应该是命令结果的数组。

+0

这与我编码的不一样吗?该错误在$ s3-> execute($ commands)失败; – twb

+0

区别在于' - > set()'链接而不是参数的'array()'。你使用什么版本? – Will

+0

没有工作。使用## 3.0.4 - 2015-06-11。调用未定义的方法Aws \ Command :: set() – twb