doue8385 2012-07-22 07:47
浏览 52
已采纳

PHP - Zend Amazon AWS S3 - 从桶中加载图像

I have this:

<img src="loadImage.php?p=sdfdf7s8sf9sf">

Which calls this, a Zend Framework S3 thingy, which should create a signed URL to show my image in the browser.

<?php
    require_once 'Zend/Service/Amazon/S3.php';

    function get_s3_signed_url($bucket, $resource, $AWS_S3_KEY, $AWS_s3_secret_key, $expire_seconds) {
        $expires = time()+$expire_seconds;

        $string_to_sign = "GET


{$expires}
/".str_replace(".s3-eu-west-1.amazonAWS.com","", $bucket)."/$resource";
        $signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), $AWS_s3_secret_key, TRUE))));

        $authentication_params = "AWSAccessKeyId=".$AWS_S3_KEY;
        $authentication_params.= "&Expires={$expires}";
        $authentication_params.= "&Signature={$signature}";

        return $link = "https://s3-eu-west-1.amazonAWS.com/{$bucket}/{$resource}?{$authentication_params}";
    }

    $aws_access_key_id = "xxx";
    $aws_s3_secret = "xxx";

    $myBucket = "myBucketName.s3-eu-west-1.amazonAWS.com";
    $myObject = "website-normal-thumbs/100001.JPG";
    $url = get_s3_signed_url( $myBucket, $myObject, $aws_access_key_id, $aws_s3_secret, 3800);

    header("Content-Type: image/jpeg"); 
    readfile($url);
?>

I literally started programming in PHP yesterday. This code does not throw any errors, although it must do cos it's not working. My S3 object is in my bucket, I have tested that and my access and secret key is correct. If I misspell require_once 'Zend/Service/Amazon/S3.php' as require_once 'Zendddd/Serviceeee/Amazonnnn/S3.php' I get an error, so Zend is there and working. I just get a little image icon, with no image!

Maybe I could work it out if it showed me an error message!

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dsj2222222 2012-07-22 10:32
    关注

    It's quite simple. All you need to do is to change

    $myBucket = "myBucketName.s3-eu-west-1.amazonAWS.com";
    

    to

    $myBucket = "myBucketName";
    

    because otherwise the link assembled in get_s3_signed_url will be faulty (https://s3-eu-west-1.amazonAWS.com/myBucketName.s3-eu-west-1.amazonAWS.com/website-normal-thumbs/100001.JPG?... instead of https://s3-eu-west-1.amazonAWS.com/myBucketName/website-normal-thumbs/100001.JPG?...)

    And the reason why you don't see an error message most probably is the header("Content-Type: image/jpeg"); command. This will lead the browser to interpret the result as an image instead of printing error messages.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题