Upload a file to S3
# S3 File Upload
php
error\\_reporting(0);
$accesskey='XXXXXXXXXXXXXXXX';
$secretkey='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
require\\_once 'AWSSDKforPHP/sdk.class.php';
if (!class\\_exists('CFRuntime')) die('No direct access allowed.');
CFCredentials::set(array(
$name = array(
'key' => $accesskey,
'secret' => $secretkey,
'certificate\\_authority' => false
),
'@default' => $name
));
$s3 = new AmazonS3();
if(!isset($\\_GET['bucket'])) {
echo "
Please select a bucket:
\n";
$buckets=$s3->get\\_bucket\\_list();
echo '';
foreach ($buckets as $b) { echo ''.$b."
\n"; }
echo '
'."\n";
} else {
$bucket = $\\_GET['bucket'];
$policy='
{"expiration": "2012-01-31T00:00:00Z",
"conditions": [
{"bucket": "'.$bucket.'"},
["starts-with", "$key", ""],
{"acl": "public-read"},
{"success\\_action\\_redirect": "success.php"},
["starts-with", "$Content-Type", ""]
]
}';
/\\*
\\* Calculate HMAC-SHA1 according to RFC2104
\\* See http://www.faqs.org/rfcs/rfc2104.html
\\*/
function hmacsha1($key,$data) {
$blocksize=64;
$hashfunc='sha1';
if (strlen($key)>$blocksize)
$key=pack('H\\*', $hashfunc($key));
$key=str\\_pad($key,$blocksize,chr(0x00));
$ipad=str\\_repeat(chr(0x36),$blocksize);
$opad=str\\_repeat(chr(0x5c),$blocksize);
$hmac = pack(
'H\\*',$hashfunc(
($key^$opad).pack(
'H\\*',$hashfunc(
($key^$ipad).$data
)
)
)
);
return bin2hex($hmac);
}
/\\*
\\* Used to encode a field for Amazon Auth
\\* (taken from the Amazon S3 PHP example library)
\\*/
function hex2b64($str)
{
$raw = '';
for ($i=0; $i < strlen($str); $i+=2)
{
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64\\_encode($raw);
}
/\\* Create the Amazon S3 Policy that needs to be signed \\*/
/\\*
\\* Base64 encode the Policy Document and then
\\* create HMAC SHA-1 signature of the base64 encoded policy
\\* using the secret key. Finally, encode it for Amazon Authentication.
\\*/
$base64\\_policy = base64\\_encode($policy);
$signature = hex2b64(hmacsha1($secretkey, $base64\\_policy));
?>
[Use a different bucket](.)
php
}
?