问题描述:

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\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);

?>

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.

网友答案:

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.

相关阅读:
Top