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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛