php - 使用 php 将文件上传到 aws s3 存储桶时出现错误

标签 php file amazon-web-services amazon-s3 upload

<?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;
    }

?>

这是我的代码 我也有 key 和 secret 。

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

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

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

        }

但在 postman 中,返回的是这个。

{
  "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 upload php API 来实现;

如果有人看到这段代码,发现有任何错误,请帮助我

<小时/>

是的,我修改了这段代码,

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

最佳答案

尝试使用以下代码上传文件,而不是读取文件。

    $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

关于php - 使用 php 将文件上传到 aws s3 存储桶时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849510/

相关文章:

php - 当用户提交登录表单时,数据会发生什么情况?

c - 如何为二维数组分配和释放堆内存?

java - Java中InputStream的FileInputStream

php - 将字符串分成几部分,返回所有字符

PHP:PDO rowCount() 有时会返回 0?

php - TCPDF 并插入一个 base64 编码的图像

java - 获取要从另一个包读取的文件

amazon-web-services - S3存储: Make a bucket folder private

android - AWS Device Farm 上用于 Cordova Android 应用程序的 Calabash,正确的 Web View 名称?

amazon-web-services - gocql.createSession : Consistency level ANY is not supported for this operation