上传多个文件到亚马逊s3失败

时间:2013-06-12 18:39:17

标签: php codeigniter file-upload amazon-s3

我正在使用带有s3库的编码器,一切都可以正常上传单个文件,但是当我尝试上传多张照片及其缩略图时,它会挂在第二个putObject(第一个文件的第一个缩略图)

代码是这样的:

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 = '0123456789abcdefghijklmnopqrstuvwxyz';
        $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

任何人都可以指引我朝着正确的方向前进吗?

感谢

2 个答案:

答案 0 :(得分: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";
    }
}

答案 1 :(得分:0)

在这里,我尝试使用亚马逊的多文件上传My Project。它的工作正常。 这是一个例子。

////////////////// 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";
}

试试这个并告诉我结果......