2015-07-10 61 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