保存的图像';无法显示,因为它包含错误';


Saved image 'cannot be displayed because it contains errors'

我正在将图像保存到我的s3存储桶中,但尽管我看到了保存的文件,但我无法打开它们。在firefox中,我得到错误"图像无法显示,因为它包含错误"将图像保存到服务器时,此代码有效,所以我只是将其更改为相应地保存到bucket:

$s3 = S3Client::factory(array(
    'region' => $region,
    'version' => $version
));
$bucket = "test";
$file_path = $bucket . "/this/is_working/";
try {
    $content_type = "image/" . $extension;
    // Upload a file.
    $result = $s3->putObject(array(
            'Bucket'       => $bucket,
            'Key'          => $file_path,
            'ACL'          => 'public-read',
            'ContentType'  => $content_type,
            'Body'   => $_FILES['picture']['tmp_name']
    ));

正如我所说,我确实看到了名为.png的文件,但每当我尝试获取链接http://region.amazonaws.com/bucket/file时,它都会以"由于包含错误而无法显示"失败。你知道这个吗?TYVM感谢您的帮助。。。

我想如果你现在打开其中一个图像,你会发现它是一个文本文件,包含你试图上传的文件的名称。

尝试在putObject调用中使用"SourceFile"而不是"Body":

   $result = $s3->putObject(array(
            'Bucket'       => $bucket,
            'Key'          => $file_path,
            'ACL'          => 'public-read',
            'ContentType'  => $content_type,
            'SourceFile'   => $_FILES['picture']['tmp_name']
    ));

或者打开文件并流式传输内容:

   $result = $s3->putObject(array(
            'Bucket'       => $bucket,
            'Key'          => $file_path,
            'ACL'          => 'public-read',
            'ContentType'  => $content_type,
            'Body'   => fopen($_FILES['picture']['tmp_name'], 'r+')
    ));

SDK文档提供了更多示例http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_putObject

相关文章: