简体   繁体   中英

Force Amazon S3 JPG Download - Attachment Headers URL

I'm simply (or not) trying to force a JPG download from my Amazon bucket, like this:

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}";
}

$url = get_s3_signed_url( $aws_bucketName, 'website-hires-photos/photo/'.$filename, $aws_access_key_id, $aws_s3_secret, 30);

header('Content-Type: images/jpg');
header("Content-Transfer-Encoding: Binary");
header('Content-Description: File Transfer'); 
header("Content-disposition: attachment; filename=\"".$url."\""); 
readfile($url);

But I'm getting a load of jargon, like this:

zóPcg” îZ~ØsÍêïJ#£Aq$ÓÒ†Nkèk Á+bÊ˛«∏k.≠ªÄ‡K©˜‰˚FπæÜ v§›†˘π#kGGº~{Ölüûˡ ¢¨Ü±S_π̧àfi˛[Ô fl€≤˛«ˆ/P˙{ß“Ì∫}_˚k'˝cg˙UF–Ò[2kK\\OÉá∂¥'_PÓı`oà˘N‰ï˚_ˇ“Ûzöw4∏í x©ì©”∫—Œ£üä~¸˜ŸÓk£√¯Ö•áıw¨Â“ÀÈ∆äüÓ≠÷9¨º—c∑=üÿYò˜ä^€

Which appears to be quite an interesting read but I haven't got time to sit down and read it. Now, the file "kind of" downloads, it says it's 748kb in size, and that's the correct size for this photo. However, the file type is unknown to my machine, so I open it in a text editor and it gives the above garbage.

Please tell me what I'm doing wrong. I'm not echoing anything to the screen before or after this point. I have two include requests at the top, this function and the headers and readfile function.

FIX UPDATE:

This was all because the pre-signed AWS S3 link has query string values after the proper .JPG extension .JPG?AWSAccessKeyID=345wrdwf . So when downloaded, it wouldn't recognise the extension. I got round this by creating a unique URL JUST for the header, like so:

$headerURL = array_shift(explode('?', basename($url)));

Which strips everything before and after the filename. So I ended up with something like this:

$url = get_s3_signed_url( $aws_bucketName, 'website-hires-photos/photo/'.$filename, $aws_access_key_id, $aws_s3_secret, 10);
$headerURL = array_shift(explode('?', basename($url)));
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/jpg");
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=".$headerURL);
readfile($url);

Answered my own question but took the knowledge of others to get me to this point, so thank-you.

This was all because the pre-signed AWS S3 URL has query string values after the proper .JPG extension /photo.jpg?AWSAccessKeyID=345wrdwf . So when downloaded, it wouldn't recognise the file extension. I got round this by creating a unique URL JUST for the header, like so:

$headerURL = array_shift(explode('?', basename($url)));

Which strips everything before and after the filename. So I ended up with something like this:

$url = get_s3_signed_url( $aws_bucketName, 'website-hires-photos/photo/'.$filename, $aws_access_key_id, $aws_s3_secret, 10);
$headerURL = array_shift(explode('?', basename($url)));
header("Content-Transfer-Encoding: binary");
header("Content-Type: image/jpg");
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=".$headerURL);
readfile($url);

And saves as a PROPER photo. Wooooo hooooooo!

And this taught me something valuable today. I didn't know that you can add any filename you like in the header attachment field. Whatever you add there is what the file saves as when the user accepts the download. So placing my company name and a download reference was a huuuge bonus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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