2013-08-05 3 views
2

현재 클라이언트 측 업로드를 blueimp 라이브러리를 통해 내 버킷에 업로드하고 설정을 위해 this 최신 튜토리얼을 설정하려고합니다.AWS 403 클라이언트 측 게시물을 게시 할 때 금지됨

나는 시그니처를 잘못 제작하고있는 것처럼 보이지만 내가하는 일은 나보다 앞서있다. 누군가가 신선한 눈을 빌려 주면 대단히 감사 할 것입니다.

또한 정확한 응답은 "우리가 계산 SignatureDoesNotMatchThe 요청 서명을 제공 한 서명과 일치하지 않습니다. 당신의 키와 서명 방법을 확인"입니다 서명 JS

 $policy = base64_encode(
    preg_replace("/\n|\r/", "", 
     json_encode(

       array(
        "expiration" => $expires, 
        "bucket" => $S3_BUCKET, 
        "acl" => "public-read", 
        "starts-with" => $object_name, 
        "success_action_status" => "201" 
       ) 
      ) 
     ) 
    ); 

    //$policy = preg_replace("/\n|\r/", "", $policy); 

    $signature = base64_encode(
      hash_hmac(
       'sha1', 
       $config->aws_secret, 
       $policy 
      ) 

    ); 

    $signature = preg_replace("/\n/", "", $signature); 

    $awsAccessInfo = array(
     "signature" => $signature, 
     "aws_key" => $AWS_ACCESS_KEY, 
     "policy" => $policy, 
     "bucket" => $S3_BUCKET, 
      "key" => $AWS_ACCESS_KEY 
    ); 

    return $this->getResponse()->json($awsAccessInfo); 

생성

PHP의 API

$('.direct-upload').each(function() { 

    var form = $(this); 
    $(this).fileupload({ 
     url: form.attr('action'), 
     type: 'POST', 
     autoUpload: true, 
     dataType: 'xml', // This is really important as s3 gives us back the url of the file in a XML document 
     add: function (event, data) { 
       console.log(data.files[0].name); 
     $.ajax({ 
      url: "http://api/sign_request_s3?allowOrigin=1", 
      type: 'GET', 
      dataType: 'json', 
      data: { s3_object_name: data.files[0].name}, // send the file name to the server so it can generate the key param 
      async: false, 
      success: function(data) { 

      // Now that we have our data, we update the form so it contains all 
      // the needed data to sign the request 
         console.log("Key: " + data.aws_key + " Signature: " + data.signature); 
      form.find('input[name=key]').val(data.aws_key); 
         form.find('input[name=AWSAccessKeyId]').val(data.aws_key); 
      form.find('input[name=policy]').val(data.policy); 
      form.find('input[name=signature]').val(data.signature); 
      } 
     }); 
     data.submit(); 
     }, 
     send: function(e, data) { 
     $('.progress').fadeIn(); 
       console.log("sending..."); 
     }, 
     progress: function(e, data){ 
     // This is what makes everything really cool, thanks to that callback 
     // you can now update the progress bar based on the upload progress 
     var percent = Math.round((e.loaded/e.total) * 100) 
     $('.bar').css('width', percent + '%') 
     }, 
     fail: function(e, data) { 
     console.log('failed'); 

     }, 
     success: function(data) { 
     // Here we get the file url on s3 in an xml doc 
     var url = $(data).find('Location').text() 
       console.log('success'); 
     $('#real_file_url').val(url) // Update the real input in the other form 
     }, 
     done: function (event, data) { 
     $('.progress').fadeOut(300, function() { 
      $('.bar').css('width', 0) 
     }) 
     }, 
    }) 
    }) 

답변

1

403의 원인이 제대로 생성되지 않았 음을 알리는 메시지가 표시됩니다. 인수의 순서는 약간 벗어 났으며 AWS docs에 명시된 순서와 정확히 일치해야합니다.

POST http://iam.amazonaws.com/ HTTP/1.1 
Authorization: AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20110909/us-east- 1/iam/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=ced6826de92d2bdeed8f846f0bf508e8559e98e4b0199114b84c54174deb456c 
host: iam.amazonaws.com 
Content-type: application/x-www-form-urlencoded; charset=utf-8 
x-amz-date: 20110909T233600Z 
Action=ListUsers&Version=2010-05-08 
+0

내가이 내 문제였다 (! 그 결과로 내 정책을 청소)하지만 나를 위해이 누락 된 버킷 정책 인 경우, 궁금 여기에 대답 http://stackoverflow.com/a/10884964/963195 –

1

심지어 AWS SDK for PHP 공식은 hard stuff을 처리합니다.

<?php 
error_reporting(-1); 
header('Content-type: text/html; charset=utf-8'); 
require_once __DIR__ . '/vendor/autoload.php'; 
#--------------------------------------------- 

define('INDENT', ' '); 

// Import namespaces 
use Aws\S3\S3Client; 
use Aws\S3\Enum\CannedAcl; 
use Aws\S3\Model\PostObject; 

// Instantiate S3 client 
$s3 = S3Client::factory(array(
    'key' => '...', 
    'secret' => '...', 
)); 

// Instantiate and prepare PostObject 
$post = new PostObject($s3, 'my-test-bucket', array(
    'acl' => CannedAcl::PUBLIC_READ, 
)); 
$post->prepareData(); 

// Get the attributes for the <form> tag 
$attributes = array(); 
foreach ($post->getFormAttributes() as $attr => $value) 
{ 
    $attributes[] = "${attr}=\"${value}\""; 
} 
$attributes = implode(' ', $attributes); 

// Write some HTML via PHP. This is for learning. Never do this in real life. 
echo "<form ${attributes}>" . PHP_EOL; 
foreach ($post->getFormInputs() as $name => $value) 
{ 
    // Write hidden fields 
    echo INDENT . "<input type=\"hidden\" name=\"${name}\" value=\"${value}\">" . PHP_EOL; 
} 

// Upload and submit 
echo INDENT . "<input type=\"file\" name=\"file\">" . PHP_EOL; 
echo INDENT . "<input type=\"submit\" name=\"upload\" value=\"Upload\">" . PHP_EOL; 

echo "</form>" . PHP_EOL;