在AWS S3 Bucket中将图像上载为0字节

时间:2018-05-21 09:54:45

标签: php amazon-web-services amazon-s3

我正在尝试在AWS s3存储桶中上传图像,但所有时间都会在存储桶中上传0个字节。

我真的不知道为什么会这样。

任何人都遇到过同样的问题

这是图像和代码的快照 https://www.awesomescreenshot.com/image/3369121/5ac921c28f569f45d1b51ac2ce7b6da1

use Aws\S3\S3Client;



public function aws_upload(){

            require 'vendor/autoload.php';
            if(isset($_FILES["left_front"]) && $_FILES["left_front"]["error"] == 0){
                        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
                        $filename = $_FILES["left_front"]["name"];
                        $filetype = $_FILES["left_front"]["type"];
                        $filesize = $_FILES["left_front"]["size"];
            }

            // Instantiate an Amazon S3 client.

            $s3 = new S3Client([
                'version' => 'latest',
                'region'  => AWS_REGION,
                'credentials' => [
                    'key'    => ACCESS_KEY_ID,
                    'secret' => SECRET_ACCESS_KEY
                ]
            ]);
            $bucketName = 'demokrishna';
            $key = basename($filename);
            try {
                $result = $s3->putObject([
                    'Bucket' => $bucketName,
                    'Key'    => $key,
                    'acl'   => 'public-read'
                  //  'ACL'    => 'public-read',
                ]);
                echo $result->get('ObjectURL');

            } catch (Aws\S3\Exception\S3Exception $e) {
                echo "There was an error uploading the file.\n";
                echo $e->getMessage();
            }
        }

2 个答案:

答案 0 :(得分:1)

您提供了存储桶,密钥和ACL,但尚未提供文件内容。您可以使用mv: rename (standard input) to ~/example/directory(standard input) : No such file or directoryBody

执行此操作

身体示例:

SourceFile

SourceFile示例:

$result = $s3Client->putObject([
    'Bucket'     => $bucket,
    'Key'        => $key,
    'Body'       => 'Hello world!',
    'ACL'        => 'public-read'
]);

另外,请注意它是'ACL',而不是'acl'。请阅读putObject documentation

答案 1 :(得分:0)

$key_bucket = basename($filename);
    $result = $s3->putObject([
                                'Bucket' => $bucketName, // bucketname
                                'Key'    => $key_bucket, 
                                'StorageClass' => 'REDUCED_REDUNDANCY',
                                'SourceFile' => $value['tmp_name'] //temporary path

                            ]);

它对我有用