Search code examples
phpamazon-s3file-uploadaws-php-sdkmultiple-file-upload

AWS S3 file uploads but files in bucket has no size?


Good Day, all. I want to upload multiple image files to my S3 bucket, I am able to do this. The only problem I encounter after uploading the files to my AWS S3 bucket is that the files are there but all of the image files have a size = 0. What could cause this problem? I am using the latest version of the AWS S3 Php SDK, the code below will roughly demonstrate my attempt: Amazon S3 Dashboard

//Get all our files
$allFiles = $_FILES['files'];

//create an array to store all of our commands
$commands = array();
$imgURLArray = array();
$temp_files_array=array();


for($i=0; $i<count($_FILES['files']['name']); $i++) 
{    
    //properties for creating nd placing the temporary file:
    $name = $allFiles['name'][$i]; // Get the origanal img name
    $tmp_name = $allFiles['tmp_name'][$i]; //get the original temp_name
    $extension = explode('.', $name);  // get the file extension
    $extension = strtolower(end($extension)); //make sure the extension is in lowercase
    $randomGeneratedName = md5(uniqid()); // Generate a new unique name for our temp file 
    $tmp_file_path = "../../tmp_files/{$randomGeneratedName}.{$extension}"; 

    //move our temporary to the temp_folder file
    move_uploaded_file($tmp_name, $tmp_file_path);

    //Bucket properties
    $bucket = $config["buckets"]['mybucket'];
    $path =  $config["product"]['path'];
    $imgName= 'product'. $productID.$i.'.'.$extension;
    $body = fopen($tmp_file_path, 'rb');
    $acl = 'public-read';

    //Command parameters
    $objParams = 
    [
        'Bucket' => $bucket,
        'Key' => $path.$imgName,
        'body' => $body,
        'ACL' => $acl
    ];

      $commands[] = $s3Client->getCommand('PutObject', $objParams);
      $url = $config['bucket']['url'].$path.$imgName;
      array_push($temp_files_array,$tmp_file_path);
      array_push($imgURLArray, $url);
}

 $pool = new CommandPool($s3Client,$commands);
 $promise = $pool->promise();
 $promise->wait();

Solution

  • Such a ridiculous problem and solution...

    In my $objParams array I declared the "Body" key with a lower-case "b" instead of using a capital "B" so the correct version of that array would be this:

    $objParams = 
    [
     'Bucket' => $bucket,
     'Key' => $path.$name,
     'Body' => $body,
      'ACL' => $acl
    ];