AWS S3 uploaded images are getting corrupted

假装没事ソ 提交于 2019-12-08 00:33:28

问题


I am working on AWS ec2 ubuntu machine. My code is in cakephp. When I try to upload any image to AWS S3 it will get corrupted.

while it is working fine in core php code.

here is my controller code

 if ($this->User->saveAll($this->request->data)) {

                    // upload on s3
                    //create file name
                    // echo "<pre>"; print_r($_FILES); die;
                    $temp = explode(".", $_FILES["data"]["name"]["User"]['image']);
                    $newfilename = round(microtime(true)) . '.' . end($temp);

                    $filepath = $_FILES['data']['tmp_name']['User']['image'];
                    $id = $this->request->data['User']['id'];

                    try {
                        $result = $this->Amazon->S3->putObject(array(
                            'Signature' => 'v4',
                            'Bucket' => 'abc.sample',
                        'ACL' => 'authenticated-read',
                        'Key' => 'files/user/image/' . $id . "/" . $newfilename,
                        'ServerSideEncryption' => 'aws:kms',
                        'SourceFile' => $filepath,
                        'Body' => $filepath,
                        'ContentType' => $_FILES['data']['type']['User']['image'],

                    ));
                } catch (S3Exception $e) {
                    echo $e->getMessage() . "\n";
                }
}

One more thing if I didn't use body parameter then it is showing me following error

You must specify a non-null value for the Body or SourceFile parameters.

While following code is working fine for test in core php

$filepath = "/var/www/html/for_testing_aws/assets/img/avtar.png";

try {
    $result = $s3->putObject(array(
        'Bucket' => $bucketName,
        'ACL' => 'authenticated-read',
        'Key' => "avtar-auth.png",
        'ServerSideEncryption' => 'aws:kms',
        'SourceFile' => $filepath,
        'ContentType' => mime_content_type($filepath),
        'debug' => [
            'logfn' => function ($msg) {
                echo $msg . "\n";
            },
            'stream_size' => 0,
            'scrub_auth' => true,
            'http' => true,
        ],
    ));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

I create a custom component for accessing all features of SDK. with the reference of https://github.com/Ali1/cakephp-amazon-aws-sdk.

check this

Images saving properly on my ec2 storage. for image uploading on ec2 server I am using this plugin https://github.com/josegonzalez/cakephp-upload

I try putobject with simple form uploading approach which also work for me here is the code

require 'aws-autoloader.php';

$credentials = new Aws\Credentials\Credentials('key 1', 'key2');
$bucketName = "";
$s3 = new Aws\S3\S3Client([
   // 'signature' => 'v4',
    'version' => 'latest',
    'region' => 'ap-southeast-1',
    'credentials' => $credentials,
    'http' => [
        'verify' => '/home/ubuntu/cacert.pem'
    ],
    'Statement' =>[
        'Action '=> "*",
    ],
//    'debug' => [
//        'logfn' => function ($msg) {
//            echo $msg . "\n";
//        },
//        'stream_size' => 0,
//        'scrub_auth' => true,
//        'http' => true,
//    ]
        ]);

$result = $s3->listBuckets();
foreach ($result['Buckets'] as $bucket) {
    // Each Bucket value will contain a Name and CreationDate

     $bucketName = $bucket['Name'];
}

 <form name="uploadimage" id="uploadimage" method="post" action="saveimg.php" enctype="multipart/form-data">
    <input type="file" name="file" value="file"/>
    <input type="submit" name="submit" value="submit" />
</form>

and saveimg.php is

$filepath = $_FILES['file']['tmp_name'];
try {
    $result = $s3->putObject(array(
        'Bucket' => $bucketName,
        'ACL' => 'authenticated-read',
        'Key' => $_FILES['file']['name'],
        'ServerSideEncryption' => 'aws:kms',
        'SourceFile' => $filepath,
        'ContentType' => mime_content_type($filepath),
        'debug' => [
            'logfn' => function ($msg) {
                echo $msg . "\n";
            },
            'stream_size' => 0,
            'scrub_auth' => true,
            'http' => true,
        ],
    ));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

When I try to open that file following message has shown.


回答1:


Try reading your file - you are passing the path and not the file contents:

'Body' => $filepath,

Should be

'Body' => file_get_contents($filepath),


来源:https://stackoverflow.com/questions/32117596/aws-s3-uploaded-images-are-getting-corrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!