-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3library.php
executable file
·80 lines (55 loc) · 1.7 KB
/
s3library.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
require_once 'Crypt/HMAC.php';
require_once 'HTTP/Request.php';
function uploadFile($path_to_file, $store_file_as, $bucket, $debugmode = false) {
if($debugmode) {
ob_start();
}
$S3_URL = "http://s3.amazonaws.com/";
$filePath = $path_to_file;
$contentType = 'audio/mpeg';
$keyId = 'AKIAJ2KRB5UNOCE64OUA';
$secretKey = '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';
$key = $store_file_as;
$resource = $bucket . "/" . $key;
$acl = "public-read";
$verb = "PUT";
$httpDate = gmdate("D, d M Y H:i:s T");
$stringToSign = "$verb\n\n$contentType\n$httpDate\nx-amz-acl:$acl\n/$resource";
$hasher =& new Crypt_HMAC($secretKey, "sha1");
$signature = hex2b64($hasher->hash($stringToSign));
$req =& new HTTP_Request($S3_URL . $resource);
$req->setMethod($verb);
$req->addHeader("content-type", $contentType);
$req->addHeader("Date", $httpDate);
$req->addHeader("x-amz-acl", $acl);
$req->addHeader("Authorization", "AWS " . $keyId . ":" . $signature);
$req->setBody(file_get_contents($filePath));
$req->sendRequest();
$ct = $req->getResponseHeader("content-type");
if($debugmode) {
if ($ct == "application/xml") $ct = "text/xml";
header("content-type: $ct");
ob_end_flush();
if ($req->getResponseCode() >= 300) {
print $req->getResponseBody();
return;
}
if ($verb != "GET") {
print "$resource ${verb}ed successfully.";
return;
}
print $req->getResponseBody();
}
$returncode = $req->getResponseCode();
unset($req);
return $returncode;
}
function hex2b64($str) {
$raw = '';
for ($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
?>