0

我有这个:

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

这称为 Zend Framework S3 的东西,它应该创建一个签名 URL 以在浏览器中显示我的图像。

<?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\n\n\n{$expires}\n/".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);
?>

我昨天真的开始用 PHP 编程了。此代码不会引发任何错误,尽管它必须这样做,因为它不起作用。我的 S3 对象在我的存储桶中,我已经测试过了,我的访问和密钥是正确的。require_once 'Zend/Service/Amazon/S3.php'如果我在require_once 'Zendddd/Serviceeee/Amazonnnn/S3.php'遇到错误时拼写错误,那么 Zend 就在那里并且正在工作。我只是得到一个小图像图标,没有图像!

如果它向我显示错误消息,也许我可以解决!

任何帮助将不胜感激。

4

1 回答 1

2

这很简单。你需要做的就是改变

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

$myBucket = "myBucketName";

因为否则组装的链接get_s3_signed_url将出现故障(https://s3-eu-west-1.amazonAWS.com/myBucketName.s3-eu-west-1.amazonAWS.com/website-normal-thumbs/100001.JPG?...而不是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.

于 2012-07-22T10:32:07.063 回答