致命错误:尝试将文件上传到AWS时出现未捕获的InvalidArgumentException

时间:2020-08-11 03:37:22

标签: php amazon-s3

我正在尝试创建s3存储桶,然后将文件上传到其中。但是,当我运行代码时,会收到以下丑陋的错误消息:

致命错误:未捕获的InvalidArgumentException:时发现1个错误 验证为PutObject操作提供的输入:[Body]必须 是fopen资源,GuzzleHttp \ Stream \ StreamInterface对象,或者 可以转换为字符串的东西。在以下位置找到bool(false) /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/Api/Validator.php:65

堆栈跟踪:

#0 /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/Middleware.php(78): Aws\Api\Validator->validate('PutObject', Object(Aws\Api\StructureShape), Array) 
#1 /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/S3/S3Client.php(541): Aws\Middleware::Aws\{closure}(Object(Aws\Command), NULL) 
#2 /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/S3/S3Client.php(564): Aws\S3\S3Client::Aws\S3\{closure}(Object(Aws\Command), NULL) 
#3 /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/S3/S3Client.php(498): Aws\S3\S3Client::Aws\S3\{closure}(Object(Aws\Command), NULL) 
#4 /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/S3/S3Client.php(517) in /Users/ddripz/Downloads/vendor/aws/aws-sdk-php/src/Api/Validator.php on line 65

这是我的PHP代码:

<?php
// Require the Composer autoloader.
require '/Users/ddripz/Downloads/vendor/autoload.php';

use Aws\S3\S3Client;

// Instantiate an Amazon S3 client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-west-2',
    'credentials' => [
      'key' => 'API KEY',
      'secret' => 'SECRET KEY'
    ]
]);

$bucketname = 'we-sign-file-manager';
$file_path = '/Users/DennisWarfield/Desktop/wesign/uploads/5f31fc30410c17.68431957.jpg';
$key = basename($file_path);

try {
    $s3->putObject([
        'Bucket' => '',
        'Key'    => 'my-object',
        'Body'   => fopen('/path/to/file', 'r'),
        'ACL'    => 'public-read',
    ]);
} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
}

 ?>

知道为什么会这样吗?

此外,当我的php文件中的最大行数为32时,为什么在行65上也会发生错误,我感到困惑。

我的autoload.php路径是否不正确,因为文件正在下载中?

1 个答案:

答案 0 :(得分:0)

改变这一行

'Body'   => fopen('/path/to/file', 'r'),

'Body'   => fopen($file_path, 'r'),