Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Glacier;
3
 
4
use Aws\Api\ApiProvider;
5
use Aws\Api\DocModel;
6
use Aws\Api\Service;
7
use Aws\AwsClient;
8
use Aws\CommandInterface;
9
use Aws\Exception\CouldNotCreateChecksumException;
10
use Aws\HashingStream;
11
use Aws\Middleware;
12
use Aws\PhpHash;
13
use Psr\Http\Message\RequestInterface;
14
 
15
/**
16
 * This client is used to interact with the **Amazon Glacier** service.
17
 *
18
 * @method \Aws\Result abortMultipartUpload(array $args = [])
19
 * @method \GuzzleHttp\Promise\Promise abortMultipartUploadAsync(array $args = [])
20
 * @method \Aws\Result abortVaultLock(array $args = [])
21
 * @method \GuzzleHttp\Promise\Promise abortVaultLockAsync(array $args = [])
22
 * @method \Aws\Result addTagsToVault(array $args = [])
23
 * @method \GuzzleHttp\Promise\Promise addTagsToVaultAsync(array $args = [])
24
 * @method \Aws\Result completeMultipartUpload(array $args = [])
25
 * @method \GuzzleHttp\Promise\Promise completeMultipartUploadAsync(array $args = [])
26
 * @method \Aws\Result completeVaultLock(array $args = [])
27
 * @method \GuzzleHttp\Promise\Promise completeVaultLockAsync(array $args = [])
28
 * @method \Aws\Result createVault(array $args = [])
29
 * @method \GuzzleHttp\Promise\Promise createVaultAsync(array $args = [])
30
 * @method \Aws\Result deleteArchive(array $args = [])
31
 * @method \GuzzleHttp\Promise\Promise deleteArchiveAsync(array $args = [])
32
 * @method \Aws\Result deleteVault(array $args = [])
33
 * @method \GuzzleHttp\Promise\Promise deleteVaultAsync(array $args = [])
34
 * @method \Aws\Result deleteVaultAccessPolicy(array $args = [])
35
 * @method \GuzzleHttp\Promise\Promise deleteVaultAccessPolicyAsync(array $args = [])
36
 * @method \Aws\Result deleteVaultNotifications(array $args = [])
37
 * @method \GuzzleHttp\Promise\Promise deleteVaultNotificationsAsync(array $args = [])
38
 * @method \Aws\Result describeJob(array $args = [])
39
 * @method \GuzzleHttp\Promise\Promise describeJobAsync(array $args = [])
40
 * @method \Aws\Result describeVault(array $args = [])
41
 * @method \GuzzleHttp\Promise\Promise describeVaultAsync(array $args = [])
42
 * @method \Aws\Result getDataRetrievalPolicy(array $args = [])
43
 * @method \GuzzleHttp\Promise\Promise getDataRetrievalPolicyAsync(array $args = [])
44
 * @method \Aws\Result getJobOutput(array $args = [])
45
 * @method \GuzzleHttp\Promise\Promise getJobOutputAsync(array $args = [])
46
 * @method \Aws\Result getVaultAccessPolicy(array $args = [])
47
 * @method \GuzzleHttp\Promise\Promise getVaultAccessPolicyAsync(array $args = [])
48
 * @method \Aws\Result getVaultLock(array $args = [])
49
 * @method \GuzzleHttp\Promise\Promise getVaultLockAsync(array $args = [])
50
 * @method \Aws\Result getVaultNotifications(array $args = [])
51
 * @method \GuzzleHttp\Promise\Promise getVaultNotificationsAsync(array $args = [])
52
 * @method \Aws\Result initiateJob(array $args = [])
53
 * @method \GuzzleHttp\Promise\Promise initiateJobAsync(array $args = [])
54
 * @method \Aws\Result initiateMultipartUpload(array $args = [])
55
 * @method \GuzzleHttp\Promise\Promise initiateMultipartUploadAsync(array $args = [])
56
 * @method \Aws\Result initiateVaultLock(array $args = [])
57
 * @method \GuzzleHttp\Promise\Promise initiateVaultLockAsync(array $args = [])
58
 * @method \Aws\Result listJobs(array $args = [])
59
 * @method \GuzzleHttp\Promise\Promise listJobsAsync(array $args = [])
60
 * @method \Aws\Result listMultipartUploads(array $args = [])
61
 * @method \GuzzleHttp\Promise\Promise listMultipartUploadsAsync(array $args = [])
62
 * @method \Aws\Result listParts(array $args = [])
63
 * @method \GuzzleHttp\Promise\Promise listPartsAsync(array $args = [])
64
 * @method \Aws\Result listProvisionedCapacity(array $args = [])
65
 * @method \GuzzleHttp\Promise\Promise listProvisionedCapacityAsync(array $args = [])
66
 * @method \Aws\Result listTagsForVault(array $args = [])
67
 * @method \GuzzleHttp\Promise\Promise listTagsForVaultAsync(array $args = [])
68
 * @method \Aws\Result listVaults(array $args = [])
69
 * @method \GuzzleHttp\Promise\Promise listVaultsAsync(array $args = [])
70
 * @method \Aws\Result purchaseProvisionedCapacity(array $args = [])
71
 * @method \GuzzleHttp\Promise\Promise purchaseProvisionedCapacityAsync(array $args = [])
72
 * @method \Aws\Result removeTagsFromVault(array $args = [])
73
 * @method \GuzzleHttp\Promise\Promise removeTagsFromVaultAsync(array $args = [])
74
 * @method \Aws\Result setDataRetrievalPolicy(array $args = [])
75
 * @method \GuzzleHttp\Promise\Promise setDataRetrievalPolicyAsync(array $args = [])
76
 * @method \Aws\Result setVaultAccessPolicy(array $args = [])
77
 * @method \GuzzleHttp\Promise\Promise setVaultAccessPolicyAsync(array $args = [])
78
 * @method \Aws\Result setVaultNotifications(array $args = [])
79
 * @method \GuzzleHttp\Promise\Promise setVaultNotificationsAsync(array $args = [])
80
 * @method \Aws\Result uploadArchive(array $args = [])
81
 * @method \GuzzleHttp\Promise\Promise uploadArchiveAsync(array $args = [])
82
 * @method \Aws\Result uploadMultipartPart(array $args = [])
83
 * @method \GuzzleHttp\Promise\Promise uploadMultipartPartAsync(array $args = [])
84
 */
85
class GlacierClient extends AwsClient
86
{
87
    public function __construct(array $args)
88
    {
89
        parent::__construct($args);
90
 
91
        // Setup middleware.
92
        $stack = $this->getHandlerList();
93
        $stack->appendBuild($this->getApiVersionMiddleware(), 'glacier.api_version');
94
        $stack->appendBuild($this->getChecksumsMiddleware(), 'glacier.checksum');
95
        $stack->appendBuild(
96
            Middleware::contentType(['UploadArchive', 'UploadPart']),
97
            'glacier.content_type'
98
        );
99
        $stack->appendInit(
100
            Middleware::sourceFile($this->getApi(), 'body', 'sourceFile'),
101
            'glacier.source_file'
102
        );
103
    }
104
 
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * Sets the default accountId to "-" for all operations.
109
     */
110
    public function getCommand($name, array $args = [])
111
    {
112
        return parent::getCommand($name, $args + ['accountId' => '-']);
113
    }
114
 
115
    /**
116
     * Creates a middleware that updates a command with the content and tree
117
     * hash headers for upload operations.
118
     *
119
     * @return callable
120
     * @throws CouldNotCreateChecksumException if the body is not seekable.
121
     */
122
    private function getChecksumsMiddleware()
123
    {
124
        return function (callable $handler) {
125
            return function (
126
                CommandInterface $command,
127
                RequestInterface $request = null
128
            ) use ($handler) {
129
                // Accept "ContentSHA256" with a lowercase "c" to match other Glacier params.
130
                if (!$command['ContentSHA256'] && $command['contentSHA256']) {
131
                    $command['ContentSHA256'] = $command['contentSHA256'];
132
                    unset($command['contentSHA256']);
133
                }
134
 
135
                // If uploading, then make sure checksums are added.
136
                $name = $command->getName();
137
                if (($name === 'UploadArchive' || $name === 'UploadMultipartPart')
138
                    && (!$command['checksum'] || !$command['ContentSHA256'])
139
                ) {
140
                    $body = $request->getBody();
141
                    if (!$body->isSeekable()) {
142
                        throw new CouldNotCreateChecksumException('sha256');
143
                    }
144
 
145
                    // Add a tree hash if not provided.
146
                    if (!$command['checksum']) {
147
                        $body = new HashingStream(
148
                            $body, new TreeHash(),
149
                            function ($result) use (&$request) {
150
                                $request = $request->withHeader(
151
                                    'x-amz-sha256-tree-hash',
152
                                    bin2hex($result)
153
                                );
154
                            }
155
                        );
156
                    }
157
 
158
                    // Add a linear content hash if not provided.
159
                    if (!$command['ContentSHA256']) {
160
                        $body = new HashingStream(
161
                            $body, new PhpHash('sha256'),
162
                            function ($result) use ($command) {
163
                                $command['ContentSHA256'] = bin2hex($result);
164
                            }
165
                        );
166
                    }
167
 
168
                    // Read the stream in order to calculate the hashes.
169
                    while (!$body->eof()) {
170
                        $body->read(1048576);
171
                    }
172
                    $body->seek(0);
173
                }
174
 
175
                // Set the content hash header if a value is in the command.
176
                if ($command['ContentSHA256']) {
177
                    $request = $request->withHeader(
178
                        'x-amz-content-sha256',
179
                        $command['ContentSHA256']
180
                    );
181
                }
182
 
183
                return $handler($command, $request);
184
            };
185
        };
186
    }
187
 
188
    /**
189
     * Creates a middleware that adds the API version header for all requests.
190
     *
191
     * @return callable
192
     */
193
    private function getApiVersionMiddleware()
194
    {
195
        return function (callable $handler) {
196
            return function (
197
                CommandInterface $command,
198
                RequestInterface $request = null
199
            ) use ($handler) {
200
                return $handler($command, $request->withHeader(
201
                    'x-amz-glacier-version',
202
                    $this->getApi()->getMetadata('apiVersion')
203
                ));
204
            };
205
        };
206
    }
207
 
208
    /**
209
     * @internal
210
     * @codeCoverageIgnore
211
     */
212
    public static function applyDocFilters(array $api, array $docs)
213
    {
214
        // Add the SourceFile parameter.
215
        $docs['shapes']['SourceFile']['base'] = 'The path to a file on disk to use instead of the body parameter.';
216
        $api['shapes']['SourceFile'] = ['type' => 'string'];
217
        $api['shapes']['UploadArchiveInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];
218
        $api['shapes']['UploadMultipartPartInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];
219
 
220
        // Add the ContentSHA256 parameter.
221
        $docs['shapes']['ContentSHA256']['base'] = 'A SHA256 hash of the content of the request body';
222
        $api['shapes']['ContentSHA256'] = ['type' => 'string'];
223
        $api['shapes']['UploadArchiveInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];
224
        $api['shapes']['UploadMultipartPartInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];
225
 
226
        // Add information about "checksum" and "ContentSHA256" being optional.
227
        $optional = '<div class="alert alert-info">The SDK will compute this value '
228
            . 'for you on your behalf if it is not supplied.</div>';
229
        $docs['shapes']['checksum']['append'] = $optional;
230
        $docs['shapes']['ContentSHA256']['append'] = $optional;
231
 
232
        // Make "accountId" optional for all operations.
233
        foreach ($api['operations'] as $operation) {
234
            $inputShape =& $api['shapes'][$operation['input']['shape']];
235
            $accountIdIndex = array_search('accountId', $inputShape['required']);
236
            unset($inputShape['required'][$accountIdIndex]);
237
        }
238
        // Add information about the default value for "accountId".
239
        $optional = '<div class="alert alert-info">The SDK will set this value to "-" by default.</div>';
240
        foreach ($docs['shapes']['string']['refs'] as $name => &$ref) {
241
            if (strpos($name, 'accountId')) {
242
                $ref .= $optional;
243
            }
244
        }
245
 
246
        return [
247
            new Service($api, ApiProvider::defaultProvider()),
248
            new DocModel($docs)
249
        ];
250
    }
251
}