| 2 |
|
|
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* Amazon S3 storage driver for filesystem. |
* Amazon S3 storage driver for fileapi. |
| 6 |
|
* |
| 7 |
|
* We are treating S3 like a normal hierarchal filesystem. The bucket |
| 8 |
|
* is predetermined by the mountpoint settings. Except for list buckets |
| 9 |
|
* and put bucket functions. |
| 10 |
|
* |
| 11 |
|
* We use the keys with a delimiter of '/' to create our heirarchy. |
| 12 |
|
* |
| 13 |
|
* @todo examine adding caching to save some requests to S3. |
| 14 |
|
* |
| 15 |
*/ |
*/ |
| 16 |
|
|
| 17 |
|
|
| 18 |
/** |
/** |
| 19 |
* driver settings form. called by filesystem.module when creating a respository using this driver. |
* driver settings form. called by fileapi.module when creating a respository using this driver. |
| 20 |
* @param $settings |
* @param $settings |
| 21 |
* settings array from database. |
* settings array from database. |
| 22 |
|
* @todo convert form to multistep to allow input of credentials, then bucket selection/creation. |
| 23 |
|
* @todo add bucket access control policy settings |
| 24 |
*/ |
*/ |
| 25 |
|
|
| 26 |
function fileapi_driver_s3_settings_form($settings = array()) { |
function fileapi_driver_s3_settings_form($settings = array()) { |
| 39 |
$form['s3 url'] = array( |
$form['s3 url'] = array( |
| 40 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 41 |
'#title' => t('Amazon S3 url'), |
'#title' => t('Amazon S3 url'), |
| 42 |
|
'#description' => t('The URL must not contain a trailing slash.'), |
| 43 |
'#default_value' => strlen($settings['s3 url']) ? $settings['s3 url'] : 'http://s3.amazonaws.com', |
'#default_value' => strlen($settings['s3 url']) ? $settings['s3 url'] : 'http://s3.amazonaws.com', |
| 44 |
); |
); |
| 45 |
|
|
| 55 |
'#default_value' => $settings['awsSecret'], |
'#default_value' => $settings['awsSecret'], |
| 56 |
); |
); |
| 57 |
|
|
| 58 |
|
$form['bucket'] = array( |
| 59 |
|
'#type' => 'textfield', |
| 60 |
|
'#title' => t('S3 Bucket'), |
| 61 |
|
'#default_value' => $settings['bucket'], |
| 62 |
|
); |
| 63 |
|
|
| 64 |
return $form; |
return $form; |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
function fileapi_driver_s3_settings_form_validate() { |
function fileapi_driver_s3_settings_form_validate() { |
| 68 |
|
//@todo test for trailing slash in 's3 url'; |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
function fileapi_driver_s3_settings_form_submit() { |
| 72 |
|
//@todo create bucket if it doesn't exist. |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
|
|
| 76 |
/** |
/** |
| 77 |
* test if a path is a file. |
* test if a path is a file. |
| 78 |
|
* |
| 79 |
|
* for S3 we are going to operate on an object if an object |
| 80 |
|
* has the key we're requesting. |
| 81 |
|
* see: S3 Developers Guide pg 67. |
| 82 |
|
* |
| 83 |
* @param $path |
* @param $path |
| 84 |
* path to be tested |
* path to be tested |
| 85 |
|
* |
| 86 |
*/ |
*/ |
| 87 |
function fileapi_driver_s3_is_file($settings, $path) { |
function fileapi_driver_s3_is_file($settings, $path) { |
| 88 |
|
// request meta data for an object with the key $path. |
| 89 |
|
$result = s3_request('HEAD', $settings['bucket'] .'/'. $path, $settings); |
| 90 |
|
|
| 91 |
|
//parse results for request... |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
/** |
/** |
| 127 |
* @param $path |
* @param $path |
| 128 |
*/ |
*/ |
| 129 |
function fileapi_driver_s3_remove($settings, $path) { |
function fileapi_driver_s3_remove($settings, $path) { |
|
clearstatcache(); |
|
| 130 |
|
|
| 131 |
} |
} |
| 132 |
|
|
| 166 |
$headers = array(); |
$headers = array(); |
| 167 |
$headers['Content-Type'] = 'text/plain'; |
$headers['Content-Type'] = 'text/plain'; |
| 168 |
$body = file_get_contents($tmp); |
$body = file_get_contents($tmp); |
| 169 |
s3_request('PUT', $path, $settings, array(), $body); |
$result = s3_request('PUT', $path, $settings, array(), $body); |
| 170 |
|
$xml = SimpleXMLElement($result['data']); |
| 171 |
unlink($tmp); |
unlink($tmp); |
| 172 |
} |
} |
| 173 |
|
|
| 180 |
* action results and array of directory contents. |
* action results and array of directory contents. |
| 181 |
*/ |
*/ |
| 182 |
function fileapi_driver_s3_readdir($settings, $path) { |
function fileapi_driver_s3_readdir($settings, $path) { |
| 183 |
|
// $result = s3_request('GET', $settings['bucket'] .'?delimiter=/&key=$path', $settings); |
| 184 |
} |
} |
| 185 |
|
|
| 186 |
/** |
/** |
| 192 |
* and our support target is 4.3.0. |
* and our support target is 4.3.0. |
| 193 |
*/ |
*/ |
| 194 |
function fileapi_driver_s3_fileread($settings, $path) { |
function fileapi_driver_s3_fileread($settings, $path) { |
| 195 |
return s3_request('GET', $path, $settings); |
// $result = s3_request('GET', $path, $settings); |
| 196 |
|
return $result; |
| 197 |
} |
} |
| 198 |
|
|
| 199 |
function fileapi_driver_s3_filewrite($settings, $path, $data) { |
function fileapi_driver_s3_filewrite($settings, $path, $data) { |
| 200 |
$headers = array('Content-Type' => 'text/plain'); |
$headers = array('Content-Type' => 'text/plain'); |
| 201 |
s3_request('PUT', $path, $settings, array(), $data); |
// $result = s3_request('PUT', $settings['bucket'] .'/'. $path, $settings, array(), $data); |
| 202 |
|
return $result; |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
|
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body=NULL) { |
| 206 |
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body= '') { |
|
| 207 |
$url = $settings['s3 url'] . $settings['bucket']; |
$url = $settings['s3 url']; |
| 208 |
|
|
| 209 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 210 |
$headers['Date'] = gmdate(DATE_RFC822); |
$headers['Date'] = gmdate(DATE_RFC822); |
| 211 |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
if (!is_null($body)) { |
| 212 |
|
$headers['Content-MD5'] = hex2b64(md5($body)); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
// Build S3 authorization hash. |
// Build S3 authorization hash. |
| 216 |
$auth_string = $method ."\n\n". $headers['Content-type'] ."\n"; |
$auth_string = $method ."\n"; |
| 217 |
$auth_string .= $headers['Date'] ."\n". implode("\n",$amzheaders) ."\n". $resource; |
// Apparently we need a trailing \n whether we have a body or not... |
| 218 |
|
if (isset($headers['Content-MD5'])) { |
| 219 |
|
$auth_string .= $headers['Content-MD5']; |
| 220 |
|
} |
| 221 |
|
$auth_string .= "\n" . $headers['Content-Type'] ."\n" . $headers['Date'] ."\n"; |
| 222 |
|
// Apparently we DONT need a trailing \n here if its not present. |
| 223 |
|
if (count($amzheaders)) { |
| 224 |
|
$auth_string .= implode("\n",$amzheaders) ."\n"; |
| 225 |
|
} |
| 226 |
|
$auth_string .= '/'. $resource; |
| 227 |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
| 228 |
|
print "auth_string:\n$auth_string\n"; |
| 229 |
|
print "signature: $signature\n"; |
| 230 |
|
|
| 231 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 232 |
$headers['Date'] = gmdate(DATE_RFC822); |
$headers['Date'] = gmdate(DATE_RFC822); |
| 233 |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 234 |
|
|
| 235 |
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
| 236 |
drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
return $result; |
| 237 |
|
// drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
| 238 |
|
// @todo error handling. |
| 239 |
} |
} |
| 240 |
|
|
| 241 |
|
|
| 242 |
/** |
/** |
| 243 |
* Generate an sha1 signed hash of a string. |
* Generate an sha1 signed hash of a string. |
| 244 |
* @param K |
* @param K |
| 290 |
return $h($opad.$string); |
return $h($opad.$string); |
| 291 |
} |
} |
| 292 |
|
|
| 293 |
|
// conversion. |
| 294 |
|
function hex2b64($str) |
| 295 |
|
{ |
| 296 |
|
$raw = ''; |
| 297 |
|
for ($i=0; $i < strlen($str); $i+=2) |
| 298 |
|
{ |
| 299 |
|
$raw .= chr(hexdec(substr($str, $i, 2))); |
| 300 |
|
} |
| 301 |
|
return base64_encode($raw); |
| 302 |
|
} |
| 303 |
|
|