dongyoudi1342 2015-11-09 13:50
浏览 53
已采纳

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

I'm saving images to my s3 bucket but although I see the files saved I can't open them. In firefox I get the error "image cannot be displayed because it contains erros". This code worked when saving images to the server so I just changed it to save accordingly to the 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']
    ));

As I said, I do see the file with it's name .png but whenever I try to get the link http://region.amazonaws.com/bucket/file it fails with 'cannot be displayed because contains errors`. Any idea on this one? TYVM for your help...

  • 写回答

1条回答 默认 最新

  • douzhi2988 2015-11-09 16:43
    关注

    I think if you opened one of those images right now you would find it is a text file containing the name of the file you were trying to upload.

    Try using 'SourceFile' instead of 'Body' in the putObject call:

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

    Or open the file and stream the contents:

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

    The SDK documentation provides further examples http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_putObject

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题