首页 > 解决方案 > AWS S3 文件上传但存储桶中的文件没有大小?

问题描述

大家好。我想将多个图像文件上传到我的 S3 存储桶,我能够做到这一点。将文件上传到我的 AWS S3 存储桶后,我遇到的唯一问题是文件存在,但所有图像文件的大小 = 0。什么可能导致此问题?我正在使用最新版本的 AWS S3 Php SDK,下面的代码将大致展示我的尝试: 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();

标签: phpamazon-s3file-uploadaws-php-sdkmultiple-file-upload

解决方案


如此荒谬的问题和解决方案......

在我的 $objParams 数组中,我用小写的“b”而不是使用大写的“B”声明了“Body”键,因此该数组的正确版本是这样的:

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

推荐阅读