를 제출 양식을 통해 작동하지 않는 URL을 체결 아마존 S3 URL의 아래 :S3 내가 서명 생성하는 PHP 기능이
if(!function_exists('el_crypto_hmacSHA1')){
/**
* Calculate the HMAC SHA1 hash of a string.
*
* @param string $key The key to hash against
* @param string $data The data to hash
* @param int $blocksize Optional blocksize
* @return string HMAC SHA1
*/
function el_crypto_hmacSHA1($key, $data, $blocksize = 64) {
if (strlen($key) > $blocksize) $key = pack('H*', sha1($key));
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hmac = pack('H*', sha1(
($key^$opad) . pack('H*', sha1(
($key^$ipad) . $data
))
));
return base64_encode($hmac);
}
}
if(!function_exists('el_s3_getTemporaryLink')){
/**
* Create temporary URLs to your protected Amazon S3 files.
*
* @param string $accessKey Your Amazon S3 access key
* @param string $secretKey Your Amazon S3 secret key
* @param string $bucket The bucket (bucket.s3.amazonaws.com)
* @param string $path The target file path
* @param int $expires In minutes
* @return string Temporary Amazon S3 URL
* @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf
*/
function el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) {
// Calculate expiry time
$expires = time() + intval(floatval($expires) * 60);
// Fix the path; encode and sanitize
$path = str_replace('%2F', '/', rawurlencode($path = ltrim($path, '/')));
// Path for signature starts with the bucket
$signpath = '/'. $bucket .'/'. $path;
// S3 friendly string to sign
$signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
// Calculate the hash
$signature = el_crypto_hmacSHA1($secretKey, $signsz);
// Glue the URL ...
$url = sprintf('https://%s/%s', $bucket, $path);
// ... to the query string ...
$qs = http_build_query($pieces = array(
'AWSAccessKeyId' => $accessKey,
'Expires' => $expires,
'Signature' => $signature,
));
// ... and return the URL!
return $url.'?'.$qs;
}
}
내가 zip 파일을 다운로드하는 데 사용하는 형태의 버튼으로 한 페이지가 :
<?php
// Grab the file url
$file_url = get_post_meta($post->ID, 'file_url', true);
// Grab just the filename with extension
$file_name = basename($file_url);
// AWS details
$accessKey = "AKIAJJLX2F7GUDTQ23AA";
$secretKey = "<REMOVED>";
$bucket_name = "media.themixtapesite.com";
// Create new S3 Expiry URL
$download_url = el_s3_getTemporaryLink($accessKey, $secretKey, $bucket_name, $file_name);
if (is_user_logged_in()) { ?>
<div class="download_button_div">
<?php echo '<form action="'.$download_url.'" class="download_button_div">'; ?>
<!--Download counter-->
<input type="hidden" name="download_counter" value="<?php (int)$download_count = get_post_meta($post->ID, 'download_counter', true);
$download_count++;
update_post_meta($post->ID, 'download_counter', $download_count); ?>">
<button type='submit' class='download_button'>Download</button>
</form>
여기서 볼 수 있듯이 다운로드하려는 파일의 URL에 양식 작업을 설정하기 만하면됩니다. 이 작동합니다. 서명 된 만료 S3 URL을 생성하고 파일을 다운로드합니다. 다른 페이지에서
, 내가 같은 기능을하지만 약간 다른 양식을 사용하고이 하나의 MP3 파일을 다운로드하는 것입니다 :<?php
// Grab the file url
$file_url = $mp3s;
// Grab just the filename with extension
$file_name = basename($file_url);
// AWS details
$accessKey = "AKIAJJLX2F7GUDTQ23AA";
$secretKey = "<REMOVED>";
$bucket_name = "mixtape2.s3.amazonaws.com";
// Create new S3 Expiry URL
$download_url = el_s3_getTemporaryLink($accessKey, $secretKey, $bucket_name, $file_name);
?>
<!--Download individual MP3 file direct from player-->
<span style="right:27px; position:absolute;">
<form action="download_file.php" method="post" name="downloadform">
<input name="file_name" value="<?php echo basename($mp3s); ?>" type="hidden">
<input name="real_file" value="<?php echo $download_url; ?>" type="hidden">
<input type="image" src="download.jpg" border="0" width="17" alt="Download the MP3" />
</form>
를이 양식에서 볼 수 있듯이 난에 POST를 사용하여 양식을 제출 처리를 위해 'download_file.php'라는 또 다른 파일. 'download_file.php은'단순히 오히려 브라우저에서 열어보다 MP3 파일의 다운로드를 강제하는 파일입니다 :
<?php
if(isset($_POST['file_name'])){
$player_file = $_POST['file_name'];
header('Content-type: audio/mpeg3');
header('Content-Disposition: attachment; filename="themixtapesite_'.$player_file.'"');
readfile($_POST['real_file']);
exit();
}
?>
내가 가지고있는 문제, 내가 됨 Download_file '제출 두 번째 페이지 (에. PHP는) 생성 된 URL이 작동하지 않습니다. 양식을 제출하면 단순히 0KB의 파일 만 다운로드됩니다. 소스를보고 브라우저에 링크를 복사하여 붙여 넣으면 서명이 일치하지 않는다는 S3 오류 메시지가 나타납니다.
제 첫 페이지가 작동하는 이유를 모르지만 두 번째 페이지는 작동하지 않습니다. URL은 모두 동일한 함수를 사용하여 생성됩니다.
도움을 주시면 감사하겠습니다.
두 번째 버킷 이름이 의심 스럽습니다. 단순히 'mixtape2'를 해봤습니까? – orique
@orique - 고마워요! 나는 그것이 내가 놓쳤던 것이 어리 석다라고 알았다! –