-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathawssdk.module
More file actions
156 lines (141 loc) · 3.66 KB
/
awssdk.module
File metadata and controls
156 lines (141 loc) · 3.66 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* @file
* Provides primary Drupal hook implementations.
*
* @author Jimmy Berry ("boombatower", http://drupal.org/user/214218)
* @author Tobias Bähr ("tobiasb", http://drupal.org/user/183956)
*/
/**
* Denotes "AWS SDK for PHP 2" minimum version number.
*/
define('AWSSDK_MINIMUM_VERSION', '2.1.2');
/**
* Implements hook_libraries_info().
*/
function awssdk_libraries_info() {
return array(
'awssdk' => array(
'title' => 'AWS SDK for PHP 2',
'vendor url' => 'http://aws.amazon.com/sdkforphp2/',
'download url' => 'http://aws.amazon.com/sdkforphp2/',
'version callback' => 'awssdk_get_version',
'files' => array(
'php' => array(
'vendor/autoload.php',
),
),
'callbacks' => array(
'post-load' => array(
'awssdk_set_credentials',
),
),
),
);
}
/**
* Load the default AWSSDK settings and apply variable overrides.
*
* @return array
* An associative array containing AWSSDK setting values.
*/
function awssdk_config_load() {
if (!($config = &drupal_static(__FUNCTION__))) {
$config = ($cache = cache_get(__FUNCTION__)) ? $cache->data : array();
}
if (!$config) {
$keys = array(
// Required keys.
'key',
'secret',
'account_id',
'canonical_id',
// Optional keys.
'canonical_name',
'certificate_authority',
'default_cache_config',
'mfa_serial',
'cloudfront_keypair',
'cloudfront_pem',
);
// Look for variables for each key and pull in the values.
foreach ($keys as $key) {
if (($value = variable_get('aws_' . $key)) !== NULL) {
$config[$key] = $value;
}
}
// Merge required defaults.
$config += array(
'default_cache_config' => '',
'certificate_authority' => FALSE,
);
cache_set(__FUNCTION__, $config);
}
return $config;
}
/**
* Set configuration via Credentials::factory during library load.
*/
function awssdk_set_credentials() {
if (class_exists('Aws\Common\Credentials\Credentials')) {
try{
return Aws\Common\Credentials\Credentials::factory(awssdk_config_load());
}
catch (Exception $e) {
watchdog_exception('awssdk', $e);
}
}
else {
watchdog('awssdk', 'AWS SDK 2 not found');
}
return;
}
function awssdk_get_credentials() {
return awssdk_set_credentials();
}
/**
* Returns the AWS SDK for PHP 2 Version.
*
* @return NULL|Aws\Common\Aws::VERSION
* The version of AWS SDK or NULL.
*/
function awssdk_get_version() {
$lib = libraries_get_path('awssdk');
$autoload_file = $lib . '/vendor/autoload.php';
$version = NULL;
if (file_exists($autoload_file)) {
require_once $autoload_file;
$version = Aws\Common\Aws::VERSION;
}
return $version;
}
function awssdk_get_client($service, $region = NULL) {
$service_client = awssdk_get_service_client($region);
if (empty($service_client)) {
return NULL;
}
return $service_client->get($service);
}
function awssdk_get_service_client($region) {
$aws_region = variable_get('aws_region', $region);
$service_client = &drupal_static(__FUNCTION__);
if (empty($service_client)) {
libraries_load('awssdk');
$Credentials = awssdk_get_credentials();
if ($Credentials) {
$config = array(
'region' => constant('Aws\Common\Enum\Region::'.$aws_region),
'key' => $Credentials->getAccessKeyId(),
'secret' => $Credentials->getSecretKey(),
'credentials' => $Credentials,
);
try {
$service_client = Aws\Common\Aws::factory($config);
}
catch (Exception $e) {
watchdog_exception('awssdk', $e);
}
}
}
return $service_client;
}