0
我目前正在嘗試允許瀏覽器上傳到S3桶(後端是PHP)經過大量的研究,我編輯了代碼以正確創建GET請求的簽名,但我仍然有POST和PUT請求存在問題。亞馬遜S3簽名生成POST上傳不起作用
後端代碼:
$S3_KEY=$this->config->access_id;
$S3_SECRET=$this->config->secret_key;
$S3_BUCKET=$this->config->bucket_name;
$EXPIRE_TIME=(60 * 5); // 5 minutes
$S3_URL='https://'.$S3_BUCKET.'.s3.amazonaws.com';
$expires = time() + $EXPIRE_TIME;
$stringToSign = "POST\n\n\nmultipart/form-data;\n$expires\n/$S3_BUCKET/$objectName";
$sig = urlencode(base64_encode(hash_hmac('sha1', $stringToSign, $S3_SECRET,TRUE)));
$url = ($S3_URL."/".$objectName."?AWSAccessKeyId=".$S3_KEY."&Expires=".$expires."&Signature=".$sig);
return $url;
響應:
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 4f 53 54 0a 0a 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d 42 6f 75 6e 64 61 72 79 74 4d 41 6c 68 69 42 44 36 62 45 61 57 74 4f 37 0a 31 33 39 30 36 38 35 31 39 37 0a 2f 6e 61 64 69 74 65 73 74 2f 75 70 6c 6f 61 64 73 2f 37 38 35 33 32 32 2f 69 6d 61 67 65 73 2f 34 30 31 36 39 39 5f 31 30 31 35 31 31 37 30 38 37 32 31 31 35 33 33 38 5f 35 38 38 32 34 30 33 36 37 5f 6e 25 32 30 2d 25 32 30 43 6f 70 79 2e 6a 70 67</StringToSignBytes><RequestId>DB782E9EB8228AF3</RequestId><HostId>JOXpHXL8A/hiwYKmG7Qe8BZyjDXpipXgCUAcR6PghYydq4eYS01FHu23fiIN8t7k</HostId><SignatureProvided>A/Hq9w6+oekrBGKNxyPwiBZBna0=</SignatureProvided><StringToSign>POSTmultipart/form-data; boundary=----WebKitFormBoundarytMAlhiBD6bEaWtO71390685197/naditest/uploads/785322/images/401699_10151170872115338_588240367_n%20-%20Copy.jpg</StringToSign><AWSAccessKeyId>AKIAJTMMOZVOQUWZ33AQ</AWSAccessKeyId></Error>
我差不多在現在的損失.......
此外,官方適用於PHP的AWS開發工具包處理您的所有簽名。如果你利用它,它可能會爲你節省大量的麻煩。 –