douzhangli9563 2017-03-17 04:57
浏览 162

使用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;
    }

?>

This is my code I have key and secret also.

I make this and try to upload Image File.

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

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

        }

but in postman, that returns this.

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

I don't know why this problems occur.

I just do it with aws s3 upload php API;

If anyone look at this code, and any wrong for that, please help me


yes I chagne this code,

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

but it occurs error with this.

{
  "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"
  }
}
  • 写回答

1条回答 默认 最新

  • drcomwc134525 2017-03-17 06:31
    关注

    Instead of reading the file, Try to upload a file using below code.

        $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

    评论

报告相同问题?

问题事件

  • 专家修改了标签 8月18日

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用