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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。