2013-06-12 77 views
3

我使用s3庫的codeingiter,一切工作正常,單個文件上傳,但是當我嘗試上傳多張照片及其縮略圖時,它掛在第二個putObject(第一個縮略圖第一個文件)上傳多個文件到亞馬遜s3失敗

的代碼是這樣的:

function photosAddAction() 
{ 
    $files = array(); 
    $files_data = $_FILES["uploads"]; 

    if (is_array($files_data["name"])) 
    { 
     //This is the problem 
     for ($i = 0; $i < count($files_data['name']); ++$i) 
     { 
      $files[] = array(
         'name' => $files_data['name'][$i], 
         'tmp_name' => $files_data['tmp_name'][$i], 
         ); 
     } 
    } 
    else 
    { 
     $files[] = $files_data; 
    } 

    foreach ($files as $file) 
    {  
     //prepare data 
     $chars = 'abcdefghijklmnopqrstuvwxyz'; 
     $rand = substr(str_shuffle($chars), 0, 6); 

     //prepare file info 
     $file['tempDir'] = sys_get_temp_dir(); 
     $file['targetFile'] = $this->uri->segment(2).'_'.time().'_'.$rand; 
     $file['extension'] = pathinfo($file['name'], PATHINFO_EXTENSION); 

     //upload original 
     $this->load->library('s3'); 
     $this->s3->putObject($this->s3->inputFile($file['tmp_name'], false), 'mybucket', 'images/'.$file['targetFile'].'.'.$file['extension'], S3::ACL_PUBLIC_READ); 

     //create and upload thumbnails 
     $this->create_thumbnail(array('width' => 800, 'height' => 800, 'file' => $file)); 
     $this->create_thumbnail(array('width' => 100, 'height' => 100, 'file' => $file)); 
    } 

function create_thumbnail($data) 
{ 
    log_message('info', 'create thumb called'); 
    $new_image ='t'.$data['width'].'x'.$data['height'].'_'.$data['file']['targetFile'].'.'.$data['file']['extension']; 

    //resize config 
    $config['image_library'] = 'gd2'; 
    $config['source_image']= $data['file']['tmp_name']; 
    $config['create_thumb'] = FALSE; 
    $config['maintain_ratio'] = TRUE; 
    $config['width'] = $data['width']; 
    $config['height'] = $data['height']; 

    //resize image 
    $this->load->library('image_lib'); 
    log_message('info', 'image_lib'); 
    $this->image_lib->initialize($config); 
    $this->image_lib->resize(); 
    $this->load->library('s3'); 

    //upload image 
    log_message('info', 'put'); 
    $putCommand = $this->s3->putObject($this->s3->inputFile($config['source_image'], false), 'mybucket', 'images/'.$new_image, S3::ACL_PUBLIC_READ); 

    if($putCommand) 
    { 
     log_message('info', $data['width'].'x'.$data['height'].' uploaded'); 
     log_message('info', $putCommand->getRequest()->getUrl()); 
    } 
    else 
    { 
     log_message('error', $data['width'].'x'.$data['height'].' upload FAILED'); 
    } 

    log_message('info', 'put done'); 
    $this->image_lib->clear();    
} 

最後的日誌條目:

DEBUG - 2013-06-12 20:10:33 --> Model Class Initialized 
DEBUG - 2013-06-12 20:10:33 --> Controller Class Initialized 
DEBUG - 2013-06-12 20:10:33 --> Image Lib Class Initialized 
INFO - 2013-06-12 20:10:40 --> create thumb called 
INFO - 2013-06-12 20:10:40 --> image_lib 
INFO - 2013-06-12 20:10:41 --> put 

任何人都可以直接我在正確的方向?

感謝

+1

FIXED: 問題顯然是站在我這一邊。如果您嘗試多次再次上傳同一文件,則會崩潰。通過codeigniter,我創建了一個新的臨時文件,其中包含調整大小的圖像並上傳該文件。如果您嘗試上載被覆蓋/調整大小的圖像,則不起作用 希望這有助於 – gavu

回答

0

在這裏,我想我與亞馬遜多文件上傳項目。它的工作正常。 這是一個例子。

////////////////// AWS Code Begin //////////////////// 
/////////////////////////// Step 1 ///////////////////////////// 

$ufile = $_FILES['Filedata']; 
$filename = $ufile['tmp_name']; 
$filesize = $ufile['size']; 

/*  * ************ Calculating Number of Parts ******************* */ 
$number_of_parts = 0; 
$r = $filesize % PART; // Remainder 
$q = floor($filesize/PART); // Quotient 
if ($r != 0) { 
    $number_of_parts = $q + 1; 
} else { 
    $number_of_parts = $q; 
} 
$bucket = 'isource123'; 
$keyname = date("Y") . "/" . date("F") . "/" . date("d") . "/" . $ufile['name']; 

///////////////////////////// Step 2 ///////////////////////////// 
// Create a service builder using a configuration file 

$aws = Aws::factory('./aws/Aws/Common/Resources/aws-config.php'); 

// Get the client from the builder by namespace 
    $client = $aws->get('S3'); 
$uploader = \Aws\S3\Model\MultipartUpload\UploadBuilder::newInstance() 
     ->setClient($client) 
     ->setSource($filename) 
     ->setBucket($bucket) 
     ->setKey($keyname) 
     ->setOption('Metadata', array('Foo' => 'Bar')) 
     ->setOption('CacheControl', 'max-age=3600') 
     ->setConcurrency($number_of_parts) 
     ->build(); 

try { 
    $uploader->upload(); 
    echo "Upload complete.\n"; 
} catch (MultipartUploadException $e) { 
    $uploader->abort(); 
    echo "Upload failed.\n"; 
} 

試試這個,告訴我結果...

1

作品非常快:

use Aws\S3\S3Client; 
use Aws\CommandPool; 
use Guzzle\Service\Exception\CommandTransferException; 

$commands = array(); 
foreach ($objects as $key => $file) { 
    $fileContent = $file['body']; 
    $objParams = array (
     'ACL' => 'bucket-owner-full-control', 
     'Bucket' => 'bucket_name', 
     'Key' => 's3_path', 
     'Body' => $fileContent 
    ); 
    $commands[] = $clientS3->getCommand('PutObject', $objParams); 
} 

try { 
    $results = CommandPool::batch($clientS3, $commands); 
} catch (CommandTransferException $e) { 
    $succeeded = $e->getSuccessfulCommands(); 
    echo "Failed Commands:\n"; 
    foreach ($e->getFailedCommands() as $failedCommand) { 
     echo $e->getExceptionForFailedCommand($failedCommand)->getMessage() . "\n"; 
    } 
}