123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace App\Service;
- use Aws\S3\S3Client;
- use Hyperf\Contract\ConfigInterface;
- use Hyperf\Di\Annotation\Inject;
- class OSSManager
- {
- #[Inject]
- protected S3Client $s3Client;
- #[Inject]
- protected ConfigInterface $config;
- public function uploadFile($bucket, $key, $filePath)
- {
- $result = $this->s3Client->putObject([
- 'Bucket' => $bucket,
- 'Key' => $key,
- 'SourceFile' => $filePath,
- ]);
- return $result['ObjectURL']?? null;
- }
- public function downloadFile($bucket, $key, $downloadPath)
- {
- $result = $this->s3Client->getObject([
- 'Bucket' => $bucket,
- 'Key' => $key,
- ]);
- if ($result['ContentLength'] > 0) {
- file_put_contents($downloadPath, $result['Body']);
- }
- return true;
- }
- public function deleteFile($bucket, $key)
- {
- $this->s3Client->deleteObject([
- 'Bucket' => $bucket,
- 'Key' => $key,
- ]);
- return true;
- }
- }
|