Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/Models/DownloadRequest.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Models;
4
5 use Aws\S3\S3Transfer\Exception\S3TransferException;
6 use Aws\S3\S3Transfer\Progress\AbstractTransferListener;
7 use Aws\S3\S3Transfer\S3TransferManager;
8 use Aws\S3\S3Transfer\Utils\AbstractDownloadHandler;
9 use Aws\S3\S3Transfer\Utils\FileDownloadHandler;
10 use Aws\S3\S3Transfer\Utils\StreamDownloadHandler;
11
12 final class DownloadRequest extends AbstractTransferRequest
13 {
14 public static array $configKeys = [
15 'response_checksum_validation' => 'string',
16 'multipart_download_type' => 'string',
17 'track_progress' => 'bool',
18 'concurrency' => 'int',
19 'resume_enabled' => 'bool',
20 'resume_file_path' => 'string',
21 'target_part_size_bytes' => 'int',
22 ];
23
24 /** @var string|array|null */
25 private string|array|null $source;
26
27 /** @var array */
28 private array $downloadRequestArgs;
29
30 /** @var AbstractDownloadHandler|null */
31 private ?AbstractDownloadHandler $downloadHandler;
32
33 /**
34 * @param string|array|null $source The object to be downloaded from S3.
35 * It can be either a string with a S3 URI or an array with a Bucket and Key
36 * properties set.
37 * @param array $downloadRequestArgs
38 * @param array $config The configuration to be used for this operation:
39 * - multipart_download_type: (string, optional)
40 * Overrides the resolved value from the transfer manager config.
41 * - response_checksum_validation: (string, optional) Overrides the resolved
42 * value from transfer manager config for whether checksum validation
43 * should be done. This option will be considered just if ChecksumMode
44 * is not present in the request args.
45 * - track_progress: (bool) Overrides the config option set in the transfer
46 * manager instantiation to decide whether transfer progress should be
47 * tracked.
48 * - target_part_size_bytes: (int) The part size in bytes to be used
49 * in a range multipart download. If this parameter is not provided
50 * then it fallbacks to the transfer manager `target_part_size_bytes`
51 * config value.
52 * - resume_enabled: (bool): To enable resuming a multipart download when a
53 * failure occurs.
54 * - resume_file_path (string, optional): To override the default resume file
55 * location to be generated. If specified the file name must end in `.resume`
56 * otherwise it will be added automatically.
57 * @param AbstractDownloadHandler|null $downloadHandler
58 * @param AbstractTransferListener[]|null $listeners
59 * @param AbstractTransferListener|null $progressTracker
60 */
61 public function __construct(
62 string|array|null $source,
63 array $downloadRequestArgs = [],
64 array $config = [],
65 ?AbstractDownloadHandler $downloadHandler = null,
66 array $listeners = [],
67 ?AbstractTransferListener $progressTracker = null
68 ) {
69 parent::__construct($listeners, $progressTracker, $config);
70 $this->source = $source;
71 $this->downloadRequestArgs = $downloadRequestArgs;
72 $this->config = $config;
73 if ($downloadHandler === null) {
74 $downloadHandler = new StreamDownloadHandler();
75 }
76 $this->downloadHandler = $downloadHandler;
77 }
78
79 /**
80 * @param DownloadRequest $downloadRequest
81 * @param FileDownloadHandler $downloadHandler
82 *
83 * @return self
84 */
85 public static function fromDownloadRequestAndDownloadHandler(
86 DownloadRequest $downloadRequest,
87 FileDownloadHandler $downloadHandler
88 ): self
89 {
90 return new self(
91 $downloadRequest->getSource(),
92 $downloadRequest->getObjectRequestArgs(),
93 $downloadRequest->getConfig(),
94 $downloadHandler,
95 $downloadRequest->getListeners(),
96 $downloadRequest->getProgressTracker()
97 );
98 }
99
100 /**
101 * @return array|string|null
102 */
103 public function getSource(): array|string|null
104 {
105 return $this->source;
106 }
107
108 /**
109 * @return array
110 */
111 public function getObjectRequestArgs(): array
112 {
113 return $this->downloadRequestArgs;
114 }
115
116 /**
117 * @return AbstractDownloadHandler
118 */
119 public function getDownloadHandler(): AbstractDownloadHandler
120 {
121 return $this->downloadHandler;
122 }
123
124 /**
125 * Helper method to normalize the source as an array with:
126 * - Bucket
127 * - Key
128 *
129 * @return array
130 */
131 public function normalizeSourceAsArray(): array
132 {
133 // If source is null then fall back to getObjectRequest.
134 $source = $this->getSource() ?? [
135 'Bucket' => $this->downloadRequestArgs['Bucket'] ?? null,
136 'Key' => $this->downloadRequestArgs['Key'] ?? null,
137 ];
138 if (is_string($source)) {
139 $sourceAsArray = S3TransferManager::s3UriAsBucketAndKey($source);
140 } elseif (is_array($source)) {
141 $sourceAsArray = $source;
142 } else {
143 throw new S3TransferException(
144 "Unsupported source type `" . gettype($source) . "`"
145 );
146 }
147
148 foreach (['Bucket', 'Key'] as $reqKey) {
149 if (empty($sourceAsArray[$reqKey])) {
150 throw new \InvalidArgumentException(
151 "`$reqKey` is required but not provided in "
152 . implode(', ', array_keys($sourceAsArray)) . "."
153 );
154 }
155 }
156
157 return $sourceAsArray;
158 }
159 }