AWS S3上传的图像已损坏

时间:2015-08-20 11:55:08

标签: php cakephp amazon-web-services amazon-s3 aws-php-sdk

我正在使用AWS ec2 ubuntu机器。我的代码是在cakephp中。当我尝试将任何图像上传到AWS S3时,它将被破坏。

虽然它在核心PHP代码中工作正常。

这是我的控制器代码

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

如果我没有使用body参数,那么还有一件事就是它向我显示以下错误

  

您必须为Body或SourceFile指定非null值   参数。

虽然以下代码在核心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";
}

我创建了一个自定义组件,用于访问SDK的所有功能。引用https://github.com/Ali1/cakephp-amazon-aws-sdk

检查这个

enter image description here

图像在我的ec2存储上正确保存。在ec2服务器上上传图片我正在使用这个插件https://github.com/josegonzalez/cakephp-upload

我尝试putobject使用简单的表单上传方法,这对我也有用 这是代码

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>

和saveimg.php是

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

当我尝试在显示消息后打开该文件时。

enter image description here

1 个答案:

答案 0 :(得分:3)

尝试阅读您的文件 - 您传递的是路径,而不是文件内容:

'Body' => $filepath,

应该是

'Body' => file_get_contents($filepath),