config = $config; $this->bucket = $config['aws']['s3']['bucket']; $credentials = new Credentials( $config['aws']['s3']['access_key'], $config['aws']['s3']['secret_key'] ); $this->s3Client = new S3Client([ 'version' => 'latest', 'region' => $config['aws']['s3']['region'], 'endpoint' => $config['aws']['s3']['host'], 'credentials' => $credentials, 'use_path_style_endpoint' => true, 'profile' => null, 'use_aws_shared_config_files' => false, ]); } /** * @throws \Exception */ public function uploadFile(string $localPath, string $remotePath, string $mimeType, string $acl = 'private') { try { $result = $this->s3Client->putObject([ 'Bucket' => $this->bucket, 'Key' => $remotePath, 'SourceFile' => $localPath, 'ACL' => $acl, 'ContentType' => $mimeType, ]); return $result; } catch (\Exception $e) { throw new \Exception("Error uploading file to CDN: " . $e->getMessage()); } } /** * @throws \Exception */ public function renameFile(string $oldRemotePath, string $newRemotePath) { // S3 does not support renaming directly. Copy then delete. try { $this->s3Client->copyObject([ 'Bucket' => $this->bucket, 'CopySource' => "{$this->bucket}/{$oldRemotePath}", 'Key' => $newRemotePath, ]); $this->s3Client->deleteObject([ 'Bucket' => $this->bucket, 'Key' => $oldRemotePath, ]); } catch (\Exception $e) { throw new \Exception("Error renaming file on CDN: " . $e->getMessage()); } } public function clearCache(string $remotePath) { // If using CloudFront or another CDN in front of S3, implement invalidation logic here. // This might involve calling the CloudFront API to invalidate the specific path. } }