34968-vm/includes/S3Service.php
Flatlogic Bot 2d8abe32bb V27
2025-10-17 06:23:25 +00:00

44 lines
1.2 KiB
PHP

<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/aws_config.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
class S3Service {
private static $s3Client;
private static function getS3Client() {
if (!self::$s3Client) {
self::$s3Client = new S3Client([
'version' => 'latest',
'region' => AWS_REGION,
'credentials' => [
'key' => AWS_ACCESS_KEY_ID,
'secret' => AWS_SECRET_ACCESS_KEY,
],
]);
}
return self::$s3Client;
}
public static function uploadFile($filePath, $key, $bucket = null) {
$bucket = $bucket ?: AWS_S3_BUCKET;
$s3 = self::getS3Client();
try {
$result = $s3->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $filePath,
'ACL' => 'public-read', // Make file publicly readable
]);
return $result['ObjectURL'];
} catch (S3Exception $e) {
// Handle exception
error_log($e->getMessage());
return null;
}
}
}
?>