-
Notifications
You must be signed in to change notification settings - Fork 22
/
S3Client.php
162 lines (144 loc) · 5.02 KB
/
S3Client.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
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
157
158
159
160
161
162
<?php
namespace Drupal\amazons3;
/**
* @file
* A wrapper around S3Client::factory() using aws_key / aws_secret variables.
*/
use Aws\Common\Credentials\Credentials;
use Aws\S3\S3Client as AwsS3Client;
use Drupal\amazons3\Exception\S3ConnectValidationException;
use Guzzle\Common\Collection;
use Guzzle\Service\Command\Factory\AliasFactory;
/**
* A wrapper around S3Client::factory() using aws_key / aws_secret variables.
*
* @class S3Client
*
* @package Drupal\amazons3
*/
class S3Client {
use DrupalAdapter\Bootstrap;
/**
* This set of commandAliases is a protected static with no getter on the
* S3Client class.
*
* @var array
*/
protected static $commandAliases = array(
// REST API Docs Aliases
'GetService' => 'ListBuckets',
'GetBucket' => 'ListObjects',
'PutBucket' => 'CreateBucket',
// SDK 1.x Aliases
'GetBucketHeaders' => 'HeadBucket',
'GetObjectHeaders' => 'HeadObject',
'SetBucketAcl' => 'PutBucketAcl',
'CreateObject' => 'PutObject',
'DeleteObjects' => 'DeleteMultipleObjects',
'PutObjectCopy' => 'CopyObject',
'SetObjectAcl' => 'PutObjectAcl',
'GetLogs' => 'GetBucketLogging',
'GetVersioningStatus' => 'GetBucketVersioning',
'SetBucketPolicy' => 'PutBucketPolicy',
'CreateBucketNotification' => 'PutBucketNotification',
'GetBucketNotifications' => 'GetBucketNotification',
'CopyPart' => 'UploadPartCopy',
'CreateWebsiteConfig' => 'PutBucketWebsite',
'GetWebsiteConfig' => 'GetBucketWebsite',
'DeleteWebsiteConfig' => 'DeleteBucketWebsite',
'CreateObjectExpirationConfig' => 'PutBucketLifecycle',
'GetObjectExpirationConfig' => 'GetBucketLifecycle',
'DeleteObjectExpirationConfig' => 'DeleteBucketLifecycle',
);
/**
* Create a new S3Client using aws_key / aws_secret $conf variables.
*
* @param array|Collection $config
* An array of configuration options to pass to \Aws\S3\S3Client::factory().
* If 'credentials' are set they will be used instead of aws_key and
* aws_secret.
* @param string $bucket
* (optional) The bucket to associate this client with. If empty, the client
* will default to $config['region'], and then the us-east-1 region.
*
* @return \Aws\S3\S3Client
*/
public static function factory($config = array(), $bucket = NULL) {
if (!isset($config['credentials'])) {
$config['credentials'] = new Credentials(static::variable_get('amazons3_key'), static::variable_get('amazons3_secret'));
}
if (!isset($config['endpoint'])) {
$endpoint = static::variable_get('amazons3_hostname');
if(!empty($endpoint)){
$config['endpoint'] = $endpoint;
}
}
$curl_defaults = array(
CURLOPT_CONNECTTIMEOUT => 30,
);
if (!isset($config['curl.options'])) {
$config['curl.options'] = array();
}
$config['curl.options'] += $curl_defaults;
// Set the default client location to the associated bucket.
if (!isset($config['region'])) {
$region = static::variable_get('amazons3_region');
if (!empty($region)) {
$config['region'] = $region;
}
}
$client = AwsS3Client::factory($config);
static::setCommandFactory($client);
return $client;
}
/**
* Validate that a bucket exists.
*
* Since bucket names are global across all of S3, we can't determine if a
* bucket doesn't exist at all, or if it exists but is owned by another S3
* account.
*
* @param string $bucket
* The name of the bucket to test.
* @param \Aws\S3\S3Client $client
* The S3Client to use.
*
* @throws S3ConnectValidationException
* Thrown when credentials are invalid or the bucket does not exist.
*/
public static function validateBucketExists($bucket, AwsS3Client $client) {
if (!$client->doesBucketExist($bucket, FALSE)) {
throw new S3ConnectValidationException('The S3 access credentials are invalid or the bucket does not exist.');
}
}
/**
* Override the command factory on a client.
*
* @param \Aws\S3\S3Client $client
* The client to override.
*
* @codeCoverageIgnore
*/
protected static function setCommandFactory($client) {
$default = CompositeFactory::getDefaultChain($client);
$default->add(
new AliasFactory($client, static::$commandAliases),
'Guzzle\Service\Command\Factory\ServiceDescriptionFactory'
);
$client->setCommandFactory($default);
}
/**
* Get the region for an S3Client for a specific bucket.
*
* @param string $bucket
* The bucket to get the region for.
* @param \Aws\S3\S3Client $client
* The S3Client to use.
*
* @return string
* The region for the bucket.
*/
public static function getBucketLocation($bucket, AwsS3Client $client) {
return $client->getBucketLocation(array('Bucket' => $bucket))->get('Location');
}
}