| 1 | <?php |
||
| 2 | |||
| 3 | namespace Freyo\Flysystem\QcloudCOSv5; |
||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use DateTimeInterface; |
||
| 7 | use League\Flysystem\Adapter\AbstractAdapter; |
||
| 8 | use League\Flysystem\Adapter\CanOverwriteFiles; |
||
| 9 | use League\Flysystem\AdapterInterface; |
||
| 10 | use League\Flysystem\Config; |
||
| 11 | use Qcloud\Cos\Client; |
||
| 12 | use Qcloud\Cos\Exception\ServiceResponseException; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class Adapter. |
||
| 16 | */ |
||
| 17 | class Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Client |
||
| 21 | */ |
||
| 22 | protected $client; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $config = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $regionMap = [ |
||
| 33 | 'cn-east' => 'ap-shanghai', |
||
| 34 | 'cn-sorth' => 'ap-guangzhou', |
||
| 35 | 'cn-north' => 'ap-beijing-1', |
||
| 36 | 'cn-south-2' => 'ap-guangzhou-2', |
||
| 37 | 'cn-southwest' => 'ap-chengdu', |
||
| 38 | 'sg' => 'ap-singapore', |
||
| 39 | 'tj' => 'ap-beijing-1', |
||
| 40 | 'bj' => 'ap-beijing', |
||
| 41 | 'sh' => 'ap-shanghai', |
||
| 42 | 'gz' => 'ap-guangzhou', |
||
| 43 | 'cd' => 'ap-chengdu', |
||
| 44 | 'sgp' => 'ap-singapore', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Adapter constructor. |
||
| 49 | * |
||
| 50 | * @param Client $client |
||
| 51 | * @param array $config |
||
| 52 | */ |
||
| 53 | public function __construct(Client $client, array $config) |
||
| 54 | { |
||
| 55 | $this->client = $client; |
||
| 56 | $this->config = $config; |
||
| 57 | |||
| 58 | $this->setPathPrefix($config['cdn']); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | 20 | public function getBucketWithAppId() |
|
| 65 | { |
||
| 66 | 20 | return $this->getBucket().'-'.$this->getAppId(); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 20 | public function getBucket() |
|
| 73 | { |
||
| 74 | 20 | return preg_replace( |
|
| 75 | 20 | "/-{$this->getAppId()}$/", |
|
| 76 | 20 | '', |
|
| 77 | 20 | $this->config['bucket'] |
|
| 78 | 20 | ); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 20 | public function getAppId() |
|
| 85 | { |
||
| 86 | 20 | return $this->config['credentials']['appId']; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 2 | public function getRegion() |
|
| 93 | { |
||
| 94 | 2 | return array_key_exists($this->config['region'], $this->regionMap) |
|
| 95 | 2 | ? $this->regionMap[$this->config['region']] : $this->config['region']; |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $path |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 2 | public function getSourcePath($path) |
|
| 104 | { |
||
| 105 | 2 | return sprintf('%s.cos.%s.myqcloud.com/%s', |
|
| 106 | 2 | $this->getBucketWithAppId(), $this->getRegion(), $path |
|
| 107 | 2 | ); |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $path |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getPicturePath($path) |
||
| 116 | { |
||
| 117 | return sprintf('%s.pic.%s.myqcloud.com/%s', |
||
| 118 | $this->getBucketWithAppId(), $this->getRegion(), $path |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $path |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 1 | public function getUrl($path) |
|
| 128 | { |
||
| 129 | 1 | if ($this->config['cdn']) { |
|
| 130 | return $this->applyPathPrefix($path); |
||
| 131 | } |
||
| 132 | |||
| 133 | $options = [ |
||
| 134 | 1 | 'Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http', |
|
| 135 | 1 | ]; |
|
| 136 | |||
| 137 | /** @var \GuzzleHttp\Psr7\Uri $objectUrl */ |
||
| 138 | 1 | $objectUrl = $this->client->getObjectUrl( |
|
| 139 | 1 | $this->getBucketWithAppId(), $path, "+30 minutes", $options |
|
| 140 | 1 | ); |
|
| 141 | |||
| 142 | 1 | return (string) $objectUrl; |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $path |
||
| 147 | * @param \DateTimeInterface $expiration |
||
| 148 | * @param array $options |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | 2 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
|
| 153 | { |
||
| 154 | 2 | $options = array_merge( |
|
| 155 | 2 | $options, |
|
| 156 | 2 | ['Scheme' => isset($this->config['scheme']) ? $this->config['scheme'] : 'http'] |
|
| 157 | 2 | ); |
|
| 158 | |||
| 159 | /** @var \GuzzleHttp\Psr7\Uri $objectUrl */ |
||
| 160 | 2 | $objectUrl = $this->client->getObjectUrl( |
|
| 161 | 2 | $this->getBucketWithAppId(), $path, $expiration->format('c'), $options |
|
| 162 | 2 | ); |
|
| 163 | |||
| 164 | 2 | return (string) $objectUrl; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $path |
||
| 169 | * @param string $contents |
||
| 170 | * @param Config $config |
||
| 171 | * |
||
| 172 | * @return array|false |
||
| 173 | */ |
||
| 174 | 2 | public function write($path, $contents, Config $config) |
|
| 175 | { |
||
| 176 | try { |
||
| 177 | 2 | return $this->client->upload( |
|
| 178 | 2 | $this->getBucketWithAppId(), |
|
| 179 | 2 | $path, |
|
| 180 | 2 | $contents, |
|
| 181 | 2 | $this->prepareUploadConfig($config) |
|
| 182 | 2 | ); |
|
| 183 | } catch (ServiceResponseException $e) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $path |
||
| 190 | * @param resource $resource |
||
| 191 | * @param Config $config |
||
| 192 | * |
||
| 193 | * @return array|false |
||
| 194 | */ |
||
| 195 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 196 | { |
||
| 197 | try { |
||
| 198 | 2 | return $this->client->upload( |
|
| 199 | 2 | $this->getBucketWithAppId(), |
|
| 200 | 2 | $path, |
|
| 201 | 2 | stream_get_contents($resource, -1, 0), |
|
| 202 | 2 | $this->prepareUploadConfig($config) |
|
| 203 | 2 | ); |
|
| 204 | } catch (ServiceResponseException $e) { |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $path |
||
| 211 | * @param string $contents |
||
| 212 | * @param Config $config |
||
| 213 | * |
||
| 214 | * @return array|false |
||
| 215 | */ |
||
| 216 | 1 | public function update($path, $contents, Config $config) |
|
| 217 | { |
||
| 218 | 1 | return $this->write($path, $contents, $config); |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $path |
||
| 223 | * @param resource $resource |
||
| 224 | * @param Config $config |
||
| 225 | * |
||
| 226 | * @return array|false |
||
| 227 | */ |
||
| 228 | 1 | public function updateStream($path, $resource, Config $config) |
|
| 229 | { |
||
| 230 | 1 | return $this->writeStream($path, $resource, $config); |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $path |
||
| 235 | * @param string $newpath |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 1 | public function rename($path, $newpath) |
|
| 240 | { |
||
| 241 | try { |
||
| 242 | 1 | if ($result = $this->copy($path, $newpath)) { |
|
| 243 | 1 | $this->delete($path); |
|
| 244 | 1 | } |
|
| 245 | |||
| 246 | 1 | return $result; |
|
| 247 | } catch (ServiceResponseException $e) { |
||
| 248 | return false; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $path |
||
| 254 | * @param string $newpath |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 2 | public function copy($path, $newpath) |
|
| 259 | { |
||
| 260 | try { |
||
| 261 | 2 | return (bool) $this->client->copyObject([ |
|
| 262 | 2 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 263 | 2 | 'Key' => $newpath, |
|
| 264 | 2 | 'CopySource' => $this->getSourcePath($path), |
|
| 265 | 2 | ]); |
|
| 266 | } catch (ServiceResponseException $e) { |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param string $path |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | 2 | public function delete($path) |
|
| 277 | { |
||
| 278 | try { |
||
| 279 | 2 | return (bool) $this->client->deleteObject([ |
|
| 280 | 2 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 281 | 2 | 'Key' => $path, |
|
| 282 | 2 | ]); |
|
| 283 | } catch (ServiceResponseException $e) { |
||
| 284 | return false; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param string $dirname |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | 1 | public function deleteDir($dirname) |
|
| 294 | { |
||
| 295 | try { |
||
| 296 | 1 | return (bool) $this->client->deleteObject([ |
|
| 297 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 298 | 1 | 'Key' => $dirname.'/', |
|
| 299 | 1 | ]); |
|
| 300 | } catch (ServiceResponseException $e) { |
||
| 301 | return false; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $dirname |
||
| 307 | * @param Config $config |
||
| 308 | * |
||
| 309 | * @return array|false |
||
| 310 | */ |
||
| 311 | 1 | public function createDir($dirname, Config $config) |
|
| 312 | { |
||
| 313 | try { |
||
| 314 | 1 | return $this->client->putObject([ |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 315 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 316 | 1 | 'Key' => $dirname.'/', |
|
| 317 | 1 | 'Body' => '', |
|
| 318 | 1 | ]); |
|
| 319 | } catch (ServiceResponseException $e) { |
||
| 320 | return false; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $path |
||
| 326 | * @param string $visibility |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | 1 | public function setVisibility($path, $visibility) |
|
| 331 | { |
||
| 332 | try { |
||
| 333 | 1 | return (bool) $this->client->putObjectAcl([ |
|
|
0 ignored issues
–
show
The expression
return (bool)$this->clie...sibility($visibility))) returns the type boolean which is incompatible with the return type mandated by League\Flysystem\AdapterInterface::setVisibility() of array|false.
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||
| 334 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 335 | 1 | 'Key' => $path, |
|
| 336 | 1 | 'ACL' => $this->normalizeVisibility($visibility), |
|
| 337 | 1 | ]); |
|
| 338 | } catch (ServiceResponseException $e) { |
||
| 339 | return false; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $path |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | 1 | public function has($path) |
|
| 349 | { |
||
| 350 | try { |
||
| 351 | 1 | return (bool) $this->getMetadata($path); |
|
| 352 | } catch (ServiceResponseException $e) { |
||
| 353 | return false; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $path |
||
| 359 | * |
||
| 360 | * @return array|bool |
||
| 361 | */ |
||
| 362 | 1 | public function read($path) |
|
| 363 | { |
||
| 364 | try { |
||
| 365 | 1 | $response = $this->forceReadFromCDN() |
|
| 366 | 1 | ? $this->readFromCDN($path) |
|
| 367 | 1 | : $this->readFromSource($path); |
|
| 368 | |||
| 369 | 1 | return ['contents' => (string) $response]; |
|
| 370 | } catch (ServiceResponseException $e) { |
||
| 371 | return false; |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 1 | protected function forceReadFromCDN() |
|
| 379 | { |
||
| 380 | 1 | return $this->config['cdn'] |
|
| 381 | 1 | && isset($this->config['read_from_cdn']) |
|
| 382 | 1 | && $this->config['read_from_cdn']; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param $path |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | protected function readFromCDN($path) |
||
| 391 | { |
||
| 392 | return $this->getHttpClient() |
||
| 393 | ->get($this->applyPathPrefix($path)) |
||
| 394 | ->getBody() |
||
| 395 | ->getContents(); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param $path |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | 1 | protected function readFromSource($path) |
|
| 404 | { |
||
| 405 | try { |
||
| 406 | 1 | $response = $this->client->getObject([ |
|
| 407 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 408 | 1 | 'Key' => $path, |
|
| 409 | 1 | ]); |
|
| 410 | |||
| 411 | 1 | return $response['Body']; |
|
| 412 | } catch (ServiceResponseException $e) { |
||
| 413 | return false; |
||
|
0 ignored issues
–
show
|
|||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return \GuzzleHttp\Client |
||
| 419 | */ |
||
| 420 | 1 | public function getHttpClient() |
|
| 421 | { |
||
| 422 | 1 | return new \GuzzleHttp\Client([ |
|
| 423 | 1 | 'timeout' => $this->config['timeout'], |
|
| 424 | 1 | 'connect_timeout' => $this->config['connect_timeout'], |
|
| 425 | 1 | ]); |
|
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $path |
||
| 430 | * |
||
| 431 | * @return array|bool |
||
| 432 | */ |
||
| 433 | 1 | public function readStream($path) |
|
| 434 | { |
||
| 435 | try { |
||
| 436 | 1 | $temporaryUrl = $this->getTemporaryUrl($path, Carbon::now()->addMinutes(5)); |
|
| 437 | |||
| 438 | 1 | $stream = $this->getHttpClient() |
|
| 439 | 1 | ->get($temporaryUrl, ['stream' => true]) |
|
| 440 | 1 | ->getBody() |
|
| 441 | 1 | ->detach(); |
|
| 442 | |||
| 443 | 1 | return ['stream' => $stream]; |
|
| 444 | } catch (ServiceResponseException $e) { |
||
| 445 | return false; |
||
| 446 | } |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $directory |
||
| 451 | * @param bool $recursive |
||
| 452 | * |
||
| 453 | * @return array|bool |
||
| 454 | */ |
||
| 455 | 1 | public function listContents($directory = '', $recursive = false) |
|
| 456 | { |
||
| 457 | 1 | $list = []; |
|
| 458 | |||
| 459 | 1 | $marker = ''; |
|
| 460 | 1 | while (true) { |
|
| 461 | 1 | $response = $this->listObjects($directory, $recursive, $marker); |
|
| 462 | |||
| 463 | 1 | foreach ((array) $response['Contents'] as $content) { |
|
| 464 | 1 | $list[] = $this->normalizeFileInfo($content); |
|
| 465 | 1 | } |
|
| 466 | |||
| 467 | 1 | if (!$response['IsTruncated']) { |
|
| 468 | 1 | break; |
|
| 469 | } |
||
| 470 | $marker = $response['NextMarker'] ?: ''; |
||
| 471 | } |
||
| 472 | |||
| 473 | 1 | return $list; |
|
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $path |
||
| 478 | * |
||
| 479 | * @return array|bool |
||
| 480 | */ |
||
| 481 | 5 | public function getMetadata($path) |
|
| 482 | { |
||
| 483 | try { |
||
| 484 | 5 | return $this->client->headObject([ |
|
|
0 ignored issues
–
show
|
|||
| 485 | 5 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 486 | 5 | 'Key' => $path, |
|
| 487 | 5 | ]); |
|
| 488 | } catch (ServiceResponseException $e) { |
||
| 489 | return false; |
||
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param string $path |
||
| 495 | * |
||
| 496 | * @return array|bool |
||
| 497 | */ |
||
| 498 | 1 | public function getSize($path) |
|
| 499 | { |
||
| 500 | 1 | $meta = $this->getMetadata($path); |
|
| 501 | |||
| 502 | 1 | return isset($meta['ContentLength']) |
|
| 503 | 1 | ? ['size' => $meta['ContentLength']] : false; |
|
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $path |
||
| 508 | * |
||
| 509 | * @return array|bool |
||
| 510 | */ |
||
| 511 | 1 | public function getMimetype($path) |
|
| 512 | { |
||
| 513 | 1 | $meta = $this->getMetadata($path); |
|
| 514 | |||
| 515 | 1 | return isset($meta['ContentType']) |
|
| 516 | 1 | ? ['mimetype' => $meta['ContentType']] : false; |
|
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $path |
||
| 521 | * |
||
| 522 | * @return array|bool |
||
| 523 | */ |
||
| 524 | 1 | public function getTimestamp($path) |
|
| 525 | { |
||
| 526 | 1 | $meta = $this->getMetadata($path); |
|
| 527 | |||
| 528 | 1 | return isset($meta['LastModified']) |
|
| 529 | 1 | ? ['timestamp' => strtotime($meta['LastModified'])] : false; |
|
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $path |
||
| 534 | * |
||
| 535 | * @return array|bool |
||
| 536 | */ |
||
| 537 | 1 | public function getVisibility($path) |
|
| 538 | { |
||
| 539 | try { |
||
| 540 | 1 | $meta = $this->client->getObjectAcl([ |
|
| 541 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 542 | 1 | 'Key' => $path, |
|
| 543 | 1 | ]); |
|
| 544 | |||
| 545 | 1 | foreach ($meta['Grants'] as $grant) { |
|
| 546 | 1 | if (isset($grant['Grantee']['URI']) |
|
| 547 | 1 | && $grant['Permission'] === 'READ' |
|
| 548 | 1 | && strpos($grant['Grantee']['URI'], 'global/AllUsers') !== false |
|
| 549 | 1 | ) { |
|
| 550 | return ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]; |
||
| 551 | } |
||
| 552 | 1 | } |
|
| 553 | |||
| 554 | 1 | return ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]; |
|
| 555 | } catch (ServiceResponseException $e) { |
||
| 556 | return false; |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @param array $content |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | 1 | private function normalizeFileInfo(array $content) |
|
| 566 | { |
||
| 567 | 1 | $path = pathinfo($content['Key']); |
|
| 568 | |||
| 569 | return [ |
||
| 570 | 1 | 'type' => substr($content['Key'], -1) === '/' ? 'dir' : 'file', |
|
| 571 | 1 | 'path' => $content['Key'], |
|
| 572 | 1 | 'timestamp' => Carbon::parse($content['LastModified'])->getTimestamp(), |
|
| 573 | 1 | 'size' => (int) $content['Size'], |
|
| 574 | 1 | 'dirname' => $path['dirname'] === '.' ? '' : (string) $path['dirname'], |
|
| 575 | 1 | 'basename' => (string) $path['basename'], |
|
| 576 | 1 | 'extension' => isset($path['extension']) ? $path['extension'] : '', |
|
| 577 | 1 | 'filename' => (string) $path['filename'], |
|
| 578 | 1 | ]; |
|
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param string $directory |
||
| 583 | * @param bool $recursive |
||
| 584 | * @param string $marker max return 1000 record, if record greater than 1000 |
||
| 585 | * you should set the next marker to get the full list |
||
| 586 | * |
||
| 587 | * @return \GuzzleHttp\Command\Result|array |
||
| 588 | */ |
||
| 589 | 1 | private function listObjects($directory = '', $recursive = false, $marker = '') |
|
| 590 | { |
||
| 591 | try { |
||
| 592 | 1 | return $this->client->listObjects([ |
|
| 593 | 1 | 'Bucket' => $this->getBucketWithAppId(), |
|
| 594 | 1 | 'Prefix' => ((string) $directory === '') ? '' : ($directory.'/'), |
|
| 595 | 1 | 'Delimiter' => $recursive ? '' : '/', |
|
| 596 | 1 | 'Marker' => $marker, |
|
| 597 | 1 | 'MaxKeys' => 1000, |
|
| 598 | 1 | ]); |
|
| 599 | } catch (ServiceResponseException $e) { |
||
| 600 | return [ |
||
| 601 | 'Contents' => [], |
||
| 602 | 'IsTruncated' => false, |
||
| 603 | 'NextMarker' => '', |
||
| 604 | ]; |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * @param Config $config |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | 4 | private function prepareUploadConfig(Config $config) |
|
| 614 | { |
||
| 615 | 4 | $options = []; |
|
| 616 | |||
| 617 | 4 | if (isset($this->config['encrypt']) && $this->config['encrypt']) { |
|
| 618 | $options['ServerSideEncryption'] = 'AES256'; |
||
| 619 | } |
||
| 620 | |||
| 621 | 4 | if ($config->has('params')) { |
|
| 622 | $options = array_merge($options, $config->get('params')); |
||
| 623 | } |
||
| 624 | |||
| 625 | 4 | if ($config->has('visibility')) { |
|
| 626 | $options['ACL'] = $this->normalizeVisibility($config->get('visibility')); |
||
| 627 | } |
||
| 628 | |||
| 629 | 4 | return $options; |
|
| 630 | } |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param $visibility |
||
| 634 | * |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | 1 | private function normalizeVisibility($visibility) |
|
| 638 | { |
||
| 639 | switch ($visibility) { |
||
| 640 | 1 | case AdapterInterface::VISIBILITY_PUBLIC: |
|
| 641 | $visibility = 'public-read'; |
||
| 642 | break; |
||
| 643 | } |
||
| 644 | |||
| 645 | 1 | return $visibility; |
|
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * @return Client |
||
| 650 | */ |
||
| 651 | public function getCOSClient() |
||
| 652 | { |
||
| 653 | return $this->client; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param $method |
||
| 658 | * @param $url |
||
| 659 | * |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | public function getAuthorization($method, $url) |
||
| 663 | { |
||
| 664 | $cosRequest = new \GuzzleHttp\Psr7\Request($method, $url); |
||
| 665 | |||
| 666 | $signature = new \Qcloud\Cos\Signature( |
||
| 667 | $this->config['credentials']['secretId'], |
||
| 668 | $this->config['credentials']['secretKey'] |
||
| 669 | ); |
||
| 670 | |||
| 671 | return $signature->createAuthorization($cosRequest); |
||
| 672 | } |
||
| 673 | } |
||
| 674 |