简体   繁体   English

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

[英]PHP - Zend Amazon AWS S3 - Loading image from bucket

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. 这称之为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);
?>

I literally started programming in PHP yesterday. 我昨天开始用PHP编程。 This code does not throw any errors, although it must do cos it's not working. 这段代码不会抛出任何错误,虽然它必须做cos它不起作用。 My S3 object is in my bucket, I have tested that and my access and secret key is correct. 我的S3对象在我的桶中,我测试了它,我的访问和密钥是正确的。 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. 如果我拼错require_once 'Zend/Service/Amazon/S3.php'作为require_once 'Zendddd/Serviceeee/Amazonnnn/S3.php'我收到错误,所以Zend在那里工作。 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?... ) 因为否则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"); 您没有看到错误消息的原因很可能是header("Content-Type: image/jpeg"); command. 命令。 This will lead the browser to interpret the result as an image instead of printing error messages. 这将导致浏览器将结果解释为图像而不是打印错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM