summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/Glacier/GlacierClient.php
blob: a9a335c3e16f43bc0eae6dba372eb75695979e2f (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace Aws\Glacier;

use Aws\Api\ApiProvider;
use Aws\Api\DocModel;
use Aws\Api\Service;
use Aws\AwsClient;
use Aws\CommandInterface;
use Aws\Exception\CouldNotCreateChecksumException;
use Aws\HashingStream;
use Aws\Middleware;
use Aws\PhpHash;
use Psr\Http\Message\RequestInterface;

/**
 * This client is used to interact with the **Amazon Glacier** service.
 *
 * @method \Aws\Result abortMultipartUpload(array $args = [])
 * @method \GuzzleHttp\Promise\Promise abortMultipartUploadAsync(array $args = [])
 * @method \Aws\Result abortVaultLock(array $args = [])
 * @method \GuzzleHttp\Promise\Promise abortVaultLockAsync(array $args = [])
 * @method \Aws\Result addTagsToVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise addTagsToVaultAsync(array $args = [])
 * @method \Aws\Result completeMultipartUpload(array $args = [])
 * @method \GuzzleHttp\Promise\Promise completeMultipartUploadAsync(array $args = [])
 * @method \Aws\Result completeVaultLock(array $args = [])
 * @method \GuzzleHttp\Promise\Promise completeVaultLockAsync(array $args = [])
 * @method \Aws\Result createVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise createVaultAsync(array $args = [])
 * @method \Aws\Result deleteArchive(array $args = [])
 * @method \GuzzleHttp\Promise\Promise deleteArchiveAsync(array $args = [])
 * @method \Aws\Result deleteVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise deleteVaultAsync(array $args = [])
 * @method \Aws\Result deleteVaultAccessPolicy(array $args = [])
 * @method \GuzzleHttp\Promise\Promise deleteVaultAccessPolicyAsync(array $args = [])
 * @method \Aws\Result deleteVaultNotifications(array $args = [])
 * @method \GuzzleHttp\Promise\Promise deleteVaultNotificationsAsync(array $args = [])
 * @method \Aws\Result describeJob(array $args = [])
 * @method \GuzzleHttp\Promise\Promise describeJobAsync(array $args = [])
 * @method \Aws\Result describeVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise describeVaultAsync(array $args = [])
 * @method \Aws\Result getDataRetrievalPolicy(array $args = [])
 * @method \GuzzleHttp\Promise\Promise getDataRetrievalPolicyAsync(array $args = [])
 * @method \Aws\Result getJobOutput(array $args = [])
 * @method \GuzzleHttp\Promise\Promise getJobOutputAsync(array $args = [])
 * @method \Aws\Result getVaultAccessPolicy(array $args = [])
 * @method \GuzzleHttp\Promise\Promise getVaultAccessPolicyAsync(array $args = [])
 * @method \Aws\Result getVaultLock(array $args = [])
 * @method \GuzzleHttp\Promise\Promise getVaultLockAsync(array $args = [])
 * @method \Aws\Result getVaultNotifications(array $args = [])
 * @method \GuzzleHttp\Promise\Promise getVaultNotificationsAsync(array $args = [])
 * @method \Aws\Result initiateJob(array $args = [])
 * @method \GuzzleHttp\Promise\Promise initiateJobAsync(array $args = [])
 * @method \Aws\Result initiateMultipartUpload(array $args = [])
 * @method \GuzzleHttp\Promise\Promise initiateMultipartUploadAsync(array $args = [])
 * @method \Aws\Result initiateVaultLock(array $args = [])
 * @method \GuzzleHttp\Promise\Promise initiateVaultLockAsync(array $args = [])
 * @method \Aws\Result listJobs(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listJobsAsync(array $args = [])
 * @method \Aws\Result listMultipartUploads(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listMultipartUploadsAsync(array $args = [])
 * @method \Aws\Result listParts(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listPartsAsync(array $args = [])
 * @method \Aws\Result listProvisionedCapacity(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listProvisionedCapacityAsync(array $args = [])
 * @method \Aws\Result listTagsForVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listTagsForVaultAsync(array $args = [])
 * @method \Aws\Result listVaults(array $args = [])
 * @method \GuzzleHttp\Promise\Promise listVaultsAsync(array $args = [])
 * @method \Aws\Result purchaseProvisionedCapacity(array $args = [])
 * @method \GuzzleHttp\Promise\Promise purchaseProvisionedCapacityAsync(array $args = [])
 * @method \Aws\Result removeTagsFromVault(array $args = [])
 * @method \GuzzleHttp\Promise\Promise removeTagsFromVaultAsync(array $args = [])
 * @method \Aws\Result setDataRetrievalPolicy(array $args = [])
 * @method \GuzzleHttp\Promise\Promise setDataRetrievalPolicyAsync(array $args = [])
 * @method \Aws\Result setVaultAccessPolicy(array $args = [])
 * @method \GuzzleHttp\Promise\Promise setVaultAccessPolicyAsync(array $args = [])
 * @method \Aws\Result setVaultNotifications(array $args = [])
 * @method \GuzzleHttp\Promise\Promise setVaultNotificationsAsync(array $args = [])
 * @method \Aws\Result uploadArchive(array $args = [])
 * @method \GuzzleHttp\Promise\Promise uploadArchiveAsync(array $args = [])
 * @method \Aws\Result uploadMultipartPart(array $args = [])
 * @method \GuzzleHttp\Promise\Promise uploadMultipartPartAsync(array $args = [])
 */
class GlacierClient extends AwsClient
{
    public function __construct(array $args)
    {
        parent::__construct($args);

        // Setup middleware.
        $stack = $this->getHandlerList();
        $stack->appendBuild($this->getApiVersionMiddleware(), 'glacier.api_version');
        $stack->appendBuild($this->getChecksumsMiddleware(), 'glacier.checksum');
        $stack->appendBuild(
            Middleware::contentType(['UploadArchive', 'UploadPart']),
            'glacier.content_type'
        );
        $stack->appendInit(
            Middleware::sourceFile($this->getApi(), 'body', 'sourceFile'),
            'glacier.source_file'
        );
    }

    /**
     * {@inheritdoc}
     *
     * Sets the default accountId to "-" for all operations.
     */
    public function getCommand($name, array $args = [])
    {
        return parent::getCommand($name, $args + ['accountId' => '-']);
    }

    /**
     * Creates a middleware that updates a command with the content and tree
     * hash headers for upload operations.
     *
     * @return callable
     * @throws CouldNotCreateChecksumException if the body is not seekable.
     */
    private function getChecksumsMiddleware()
    {
        return function (callable $handler) {
            return function (
                CommandInterface $command,
                RequestInterface $request = null
            ) use ($handler) {
                // Accept "ContentSHA256" with a lowercase "c" to match other Glacier params.
                if (!$command['ContentSHA256'] && $command['contentSHA256']) {
                    $command['ContentSHA256'] = $command['contentSHA256'];
                    unset($command['contentSHA256']);
                }

                // If uploading, then make sure checksums are added.
                $name = $command->getName();
                if (($name === 'UploadArchive' || $name === 'UploadMultipartPart')
                    && (!$command['checksum'] || !$command['ContentSHA256'])
                ) {
                    $body = $request->getBody();
                    if (!$body->isSeekable()) {
                        throw new CouldNotCreateChecksumException('sha256');
                    }

                    // Add a tree hash if not provided.
                    if (!$command['checksum']) {
                        $body = new HashingStream(
                            $body, new TreeHash(),
                            function ($result) use (&$request) {
                                $request = $request->withHeader(
                                    'x-amz-sha256-tree-hash',
                                    bin2hex($result)
                                );
                            }
                        );
                    }

                    // Add a linear content hash if not provided.
                    if (!$command['ContentSHA256']) {
                        $body = new HashingStream(
                            $body, new PhpHash('sha256'),
                            function ($result) use ($command) {
                                $command['ContentSHA256'] = bin2hex($result);
                            }
                        );
                    }

                    // Read the stream in order to calculate the hashes.
                    while (!$body->eof()) {
                        $body->read(1048576);
                    }
                    $body->seek(0);
                }

                // Set the content hash header if a value is in the command.
                if ($command['ContentSHA256']) {
                    $request = $request->withHeader(
                        'x-amz-content-sha256',
                        $command['ContentSHA256']
                    );
                }

                return $handler($command, $request);
            };
        };
    }

    /**
     * Creates a middleware that adds the API version header for all requests.
     *
     * @return callable
     */
    private function getApiVersionMiddleware()
    {
        return function (callable $handler) {
            return function (
                CommandInterface $command,
                RequestInterface $request = null
            ) use ($handler) {
                return $handler($command, $request->withHeader(
                    'x-amz-glacier-version',
                    $this->getApi()->getMetadata('apiVersion')
                ));
            };
        };
    }

    /**
     * @internal
     * @codeCoverageIgnore
     */
    public static function applyDocFilters(array $api, array $docs)
    {
        // Add the SourceFile parameter.
        $docs['shapes']['SourceFile']['base'] = 'The path to a file on disk to use instead of the body parameter.';
        $api['shapes']['SourceFile'] = ['type' => 'string'];
        $api['shapes']['UploadArchiveInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];
        $api['shapes']['UploadMultipartPartInput']['members']['sourceFile'] = ['shape' => 'SourceFile'];

        // Add the ContentSHA256 parameter.
        $docs['shapes']['ContentSHA256']['base'] = 'A SHA256 hash of the content of the request body';
        $api['shapes']['ContentSHA256'] = ['type' => 'string'];
        $api['shapes']['UploadArchiveInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];
        $api['shapes']['UploadMultipartPartInput']['members']['contentSHA256'] = ['shape' => 'ContentSHA256'];

        // Add information about "checksum" and "ContentSHA256" being optional.
        $optional = '<div class="alert alert-info">The SDK will compute this value '
            . 'for you on your behalf if it is not supplied.</div>';
        $docs['shapes']['checksum']['append'] = $optional;
        $docs['shapes']['ContentSHA256']['append'] = $optional;

        // Make "accountId" optional for all operations.
        foreach ($api['operations'] as $operation) {
            $inputShape =& $api['shapes'][$operation['input']['shape']];
            $accountIdIndex = array_search('accountId', $inputShape['required']);
            unset($inputShape['required'][$accountIdIndex]);
        }
        // Add information about the default value for "accountId".
        $optional = '<div class="alert alert-info">The SDK will set this value to "-" by default.</div>';
        foreach ($docs['shapes']['string']['refs'] as $name => &$ref) {
            if (strpos($name, 'accountId')) {
                $ref .= $optional;
            }
        }

        return [
            new Service($api, ApiProvider::defaultProvider()),
            new DocModel($docs)
        ];
    }
}