web services - Amazon S3 Signed URL in PHP -
this function pulled out of old wp plugin returning signed amazon s3 url, can't work! when visit signed url returns, this:
the request signature calculated not match signature provided. check key , signing method.
function s3url($text) { $aws_s3_key = 'key'; $aws_s3_secret = 'secret'; $tag_pattern = '/(\[s3 bucket\=(.*?)\ text\=(.*?)\](.*?)\[\/s3\])/i'; define("aws_s3_key", $aws_s3_key); // replace aws s3 key define("aws_s3_secret", $aws_s3_secret); // replace secret key. $expires = time()+get_option('expire_seconds'); if (preg_match_all ($tag_pattern, $text, $matches)) { ($m=0; $m<count($matches[0]); $m++) { $bucket = $matches[2][$m]; $link_text = $matches[3][$m]; $resource = $matches[4][$m]; $string_to_sign = "get\n\n\n$expires\n/".str_replace(".s3.amazonaws.com","",$bucket)."/$resource"; //$string_to_sign = "get\n\n\n{$expires}\n/{$bucket}/{$resource}"; $signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), aws_s3_secret, true)))); $authentication_params = "awsaccesskeyid=".aws_s3_key; $authentication_params.= "&expires={$expires}"; $authentication_params.= "&signature={$signature}"; $tag_pattern_match = "/(\[s3 bucket\=(.*?)\ text\={$link_text}\]{$resource}\[\/s3\])/i"; if(strlen($link_text) == 0) { $link = "http://{$bucket}/{$resource}?{$authentication_params}"; } else { $link = "<a href='http://{$bucket}/{$resource}?{$authentication_params}'>{$link_text}</a>"; } $text = preg_replace($tag_pattern_match,$link,$text); } } return $text; }
the example provided in amazon aws php sdk: sdk-latest\sdk-1.3.5\sdk-1.3.5\_samples\cli-s3_get_urls_for_uploads.php
following code works quite well:
/* execute our queue of batched requests. may take few seconds few minutes depending on size of files , how fast upload speeds are. */ $file_upload_response = $s3->batch()->send(); /* since batch of requests return multiple responses, let's make sure came using `areok()` (singular responses use `isok()`). */ if ($file_upload_response->areok()) { // loop through individual filenames foreach ($individual_filenames $filename) { /* display url each of files uploaded. since uploads default private (you can choose override setting when uploading), we'll pre-authenticate file url next 5 minutes. */ echo $s3->get_object_url($bucket, $filename, '5 minutes') . php_eol . php_eol; } }
Comments
Post a Comment