12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- declare(strict_types=1);
- namespace App\Service;
- use Aws\S3\S3Client;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\HttpServer\Annotation\Controller;
- use Hyperf\HttpServer\Annotation\RequestMapping;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- use function Hyperf\Support\env;
- use App\Tools\Result;
- class MinioService
- {
- /**
- * @Inject
- * @var ResponseInterface
- */
- protected $response;
- /**
- * @Inject
- * @var \Hyperf\Config\ConfigInterface
- */
- protected $config;
- protected $s3Client;
- public function __construct()
- {
- $this->s3Client = new S3Client([
- 'version' => 'latest',
- 'region' => 'chinese',
- 'endpoint' => env("OSS_ENDPOINT"),
- 'use_path_style_endpoint' => true,
- 'credentials' => [
- 'key' => env("OSS_KEY"),
- 'secret' => env("OSS_SECRET"),
- ],
- ]);
- }
- /**
- * 没几把鸟用用来测试的
- * @return \Psr\Http\Message\ResponseInterface
- */
- public function listBuckets()
- {
- try {
- // 获取所有存储桶列表
- $result = $this->s3Client->listBuckets();
- // 提取存储桶名称
- $buckets = array_column($result['Buckets'], 'Name');
- return Result::success($buckets);
- } catch (\Aws\Exception\AwsException $e) {
- var_dump($e->getMessage());
- return Result::error("报错了");
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- return Result::error("报错了");
- }
- }
- }
|