| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 5 |
* Amazon S3 storage driver for filesystem. |
* Amazon S3 storage driver for filesystem. |
| 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 |
*/ |
*/ |
| 14 |
|
|
| 15 |
|
|
| 16 |
/** |
/** |
| 17 |
* 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. |
| 18 |
* @param $settings |
* @param $settings |
| 19 |
* settings array from database. |
* settings array from database. |
| 20 |
|
* @todo convert form to multistep to allow input of credentials, then bucket selection/creation. |
| 21 |
|
* @todo add bucket access control policy settings |
| 22 |
*/ |
*/ |
| 23 |
|
|
| 24 |
function fileapi_driver_s3_settings_form($settings = array()) { |
function fileapi_driver_s3_settings_form($settings = array()) { |
| 37 |
$form['s3 url'] = array( |
$form['s3 url'] = array( |
| 38 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 39 |
'#title' => t('Amazon S3 url'), |
'#title' => t('Amazon S3 url'), |
| 40 |
|
'#description' => t('The URL must not contain a trailing slash.'), |
| 41 |
'#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', |
| 42 |
); |
); |
| 43 |
|
|
| 53 |
'#default_value' => $settings['awsSecret'], |
'#default_value' => $settings['awsSecret'], |
| 54 |
); |
); |
| 55 |
|
|
| 56 |
|
$form['bucket'] = array( |
| 57 |
|
'#type' => 'textfield', |
| 58 |
|
'#title' => t('S3 Bucket'), |
| 59 |
|
'#default_value' => $settings['bucket'], |
| 60 |
|
); |
| 61 |
|
|
| 62 |
return $form; |
return $form; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
function fileapi_driver_s3_settings_form_validate() { |
function fileapi_driver_s3_settings_form_validate() { |
| 66 |
|
//@todo test for trailing slash in 's3 url'; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
function fileapi_driver_s3_settings_form_submit() { |
| 70 |
|
//@todo create bucket if it doesn't exist. |
| 71 |
} |
} |
| 72 |
|
|
| 73 |
|
|
| 74 |
/** |
/** |
| 75 |
* test if a path is a file. |
* test if a path is a file. |
| 76 |
|
* |
| 77 |
|
* for S3 we are going to operate on an object if an object |
| 78 |
|
* has the key we're requesting. |
| 79 |
|
* see: S3 Developers Guide pg 67. |
| 80 |
|
* |
| 81 |
* @param $path |
* @param $path |
| 82 |
* path to be tested |
* path to be tested |
| 83 |
|
* |
| 84 |
*/ |
*/ |
| 85 |
function fileapi_driver_s3_is_file($settings, $path) { |
function fileapi_driver_s3_is_file($settings, $path) { |
| 86 |
|
// request meta data for an object with the key $path. |
| 87 |
|
$result = s3_request('HEAD', $path, $settings); |
| 88 |
|
|
| 89 |
|
//parse results for request... |
| 90 |
} |
} |
| 91 |
|
|
| 92 |
/** |
/** |
| 125 |
* @param $path |
* @param $path |
| 126 |
*/ |
*/ |
| 127 |
function fileapi_driver_s3_remove($settings, $path) { |
function fileapi_driver_s3_remove($settings, $path) { |
|
clearstatcache(); |
|
| 128 |
|
|
| 129 |
} |
} |
| 130 |
|
|
| 177 |
* action results and array of directory contents. |
* action results and array of directory contents. |
| 178 |
*/ |
*/ |
| 179 |
function fileapi_driver_s3_readdir($settings, $path) { |
function fileapi_driver_s3_readdir($settings, $path) { |
| 180 |
|
$result = s3_request('GET', $settings['bucket'] .'?delimiter=/&key=$path', $settings); |
| 181 |
} |
} |
| 182 |
|
|
| 183 |
/** |
/** |
| 189 |
* and our support target is 4.3.0. |
* and our support target is 4.3.0. |
| 190 |
*/ |
*/ |
| 191 |
function fileapi_driver_s3_fileread($settings, $path) { |
function fileapi_driver_s3_fileread($settings, $path) { |
| 192 |
return s3_request('GET', $path, $settings); |
$result = s3_request('GET', $path, $settings); |
| 193 |
|
return $result; |
| 194 |
} |
} |
| 195 |
|
|
| 196 |
function fileapi_driver_s3_filewrite($settings, $path, $data) { |
function fileapi_driver_s3_filewrite($settings, $path, $data) { |
| 197 |
$headers = array('Content-Type' => 'text/plain'); |
$headers = array('Content-Type' => 'text/plain'); |
| 198 |
s3_request('PUT', $path, $settings, array(), $data); |
$result = s3_request('PUT', $settings['bucket'] .'/'. $path, $settings, array(), $data); |
| 199 |
|
return $result; |
| 200 |
} |
} |
| 201 |
|
|
| 202 |
|
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body=NULL) { |
| 203 |
function s3_request($method, $resource, $settings, $headers=array(), $amzheaders=array(), $body= '') { |
|
| 204 |
$url = $settings['s3 url'] . $settings['bucket']; |
$url = $settings['s3 url']; |
| 205 |
|
|
| 206 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 207 |
$headers['Date'] = gmdate(DATE_RFC822); |
$headers['Date'] = gmdate(DATE_RFC822); |
| 208 |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 209 |
|
|
| 210 |
// Build S3 authorization hash. |
// Build S3 authorization hash. |
| 211 |
$auth_string = $method ."\n\n". $headers['Content-type'] ."\n"; |
$auth_string = $method ."\n\n". $headers['Content-type'] ."\n" . $headers['Date'] ."\n"; |
| 212 |
$auth_string .= $headers['Date'] ."\n". implode("\n",$amzheaders) ."\n". $resource; |
if (count($amzheaders)) { |
| 213 |
|
$auth_string .= implode("\n",$amzheaders) ."\n"; |
| 214 |
|
} |
| 215 |
|
$auth_string .= '/'. $resource; |
| 216 |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
$signature = hex2b64(s3_hmac_sha1($settings['awsSecret'], $auth_string)); |
| 217 |
|
print "auth_string:\n$auth_string\n"; |
| 218 |
|
print "signature: $signature\n"; |
| 219 |
|
|
| 220 |
// Set date to proper format for S3. |
// Set date to proper format for S3. |
| 221 |
$headers['Date'] = gmdate(DATE_RFC822); |
$headers['Date'] = gmdate(DATE_RFC822); |
| 222 |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
$headers['Authorization'] = 'AWS '. $settings['awsID'] .':'. $signature; |
| 223 |
|
|
| 224 |
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
$result = drupal_http_request($url .'/'. $resource, array_merge($headers, $amzheaders), $method, $body); |
| 225 |
drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
return $result; |
| 226 |
|
// drupal_set_message('<pre>'. print_r($result, TRUE) .'</pre>'); |
| 227 |
|
// @todo error handling. |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
/** |
/** |
| 232 |
* Generate an sha1 signed hash of a string. |
* Generate an sha1 signed hash of a string. |
| 233 |
* @param K |
* @param K |
| 279 |
return $h($opad.$string); |
return $h($opad.$string); |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
|
// conversion. |
| 283 |
|
function hex2b64($str) |
| 284 |
|
{ |
| 285 |
|
$raw = ''; |
| 286 |
|
for ($i=0; $i < strlen($str); $i+=2) |
| 287 |
|
{ |
| 288 |
|
$raw .= chr(hexdec(substr($str, $i, 2))); |
| 289 |
|
} |
| 290 |
|
return base64_encode($raw); |
| 291 |
|
} |
| 292 |
|
|