使用php将文件上传到aws s3存储桶时发生错误

使用php将文件上传到aws s3存储桶时发生错误

问题描述:

<?php 

require_once(APPPATH.'libraries/REST_Controller.php'); 

require 'vendor/autoload.php'; 

use Aws\S3\S3Client; 
use Aws\S3\Exception\S3Exception; 

    $s3_config = array(
     'key' => '*************', 
     'secret' => '*****************************'); 

    $s3 = S3Client::factory([ 
      'credentials' => [ 
       'key' => $s3_config['key'], 
       'secret' => $s3_config['secret'],], 
      'region' => 'ap-northeast-2', 
      'version' => 'latest']); 

    function uploadS3($bucket, $dir, $file) { 

     $key = md5(uniqid()); 
     $type = mime_content_type($file['tmp_name']); 
     $ext = explode('/', $type); 
     $ext = $ext[sizeof($ext) - 1]; 
     $file_name = "{$key}.{$ext}"; 
     $file_path = "./files/{$file_name}"; 

     move_uploaded_file($file['tmp_name'], $file_path); 

     $save_path = null; 

     try { 

      $result = $GLOBALS['s3']->putObject([ 
       'Bucket' => $bucket, 
       'Key' => "{$dir}/{$file_name}", 
       'Body' => fopen($file_path, 'rb'), 
       'ACL' => 'public-read']); 
      $save_path = $result['ObjectURL']; 
     } catch (S3Exception $e) { 
      // return null; 
     } 

     unlink($file_path); 

     return $save_path; 
    } 

?> 

这是我的代码 我也有密钥和秘密。使用php将文件上传到aws s3存储桶时发生错误

我做这个并尝试上传图像文件。

if (isset($_FILES['ImageOneAdd'])) { 

      $ImageOneAdd = uploadS3('testbucket','image',$_FILES['ImageOneAdd']); 

     } 

但在邮递员,返回这个。

{ 
    "status": false, 
    "error": { 
    "classname": "InvalidArgumentException", 
    "message": "Found 1 error while validating the input provided for the PutObject operation:\n[Body] must be an fopen resource, a GuzzleHttp\\Stream\\StreamInterface object, or something that can be cast to a string. Found bool(false)" 
    } 
} 

我不知道为什么会出现这种问题。

我刚刚用aws s3上传php API;

如果有人看这个代码,任何错误的是,请大家帮我


是我chagne这个代码,

try { 

      $result = $GLOBALS['s3']->putObject([ 
       'Bucket' => $bucket, 
       'Key' => "{$dir}/{$file_name}", 
       'SourceFile' => $file_path, 
       'ACL' => 'public-read']); 
      $save_path = $result['ObjectURL']; 
     } catch (S3Exception $e) { 
      // return null; 
     } 

,但这个错误发生。

{ 
    "status": false, 
    "error": { 
    "classname": "RuntimeException", 
    "message": "Unable to open ./files/40c0a29b0599204c147745116554af59.jpeg using mode r: fopen(./files/40c0a29b0599204c147745116554af59.jpeg): failed to open stream: No such file or directory" 
    } 
} 
+0

@ Polaris.please尝试从目录而不是输入上传图片file.so您可以直接上传或不检查的问题。 – iCoders

+0

@iCoders我不明白你的意思是从目录上传图像是文件路径> –

+0

尝试从上传目录上传一个图像而不是表单输入文件,所以你可以检查是否有效 – iCoders

而不是阅读文件,尝试使用下面的代码上传文件。

$result = $GLOBALS['s3']->putObject(array(
     'Bucket'  => $bucket, 
     'Key'  => "{$dir}/{$file_name}", 
     'SourceFile' => $file_path 
    )); 

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_putObject

+0

谢谢,但.. 我改变了代码,但我发生错误 –

+0

该文件在给定路径上不可用...这就是发生此错误的原因。 使用move_uploaded_file后,文件必须在给定路径上可用。 –

+0

你的意思是$ file_path是表达绝对路径的路径吗? 我开发的应用程序,我希望,当我发布的文件路径,功能uploadS3将上传文件。 –