Files
manjapro_v6/controller/uploadMinioFiles.php
T
2026-08-20 18:39:37 +07:00

68 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/../minio/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
function uploadToMinioStorageRaw($fileData, $objectPath)
{
// --- Inisialisasi koneksi MinIO ---
$minio = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'endpoint' => 'https://storage.manjapro.net', // URL MinIO kamu
'use_path_style_endpoint' => true,
'credentials' => [
'key' => 'admin', // Ganti sesuai kredensial kamu
'secret' => '@P4ssw0rd', // Ganti sesuai kredensial kamu
],
'suppress_php_deprecation_warning' => true
]);
$bucket = 'files';
// --- Tentukan Content-Type berdasarkan ekstensi file ---
$ext = strtolower(pathinfo($objectPath, PATHINFO_EXTENSION));
switch ($ext) {
case 'jpg':
case 'jpeg':
$contentType = 'image/jpeg';
break;
case 'png':
$contentType = 'image/png';
break;
case 'pdf':
$contentType = 'application/pdf';
break;
case 'txt':
$contentType = 'text/plain';
break;
case 'json':
$contentType = 'application/json';
break;
default:
$contentType = 'application/octet-stream';
}
try {
// --- Upload langsung dari memori ke MinIO ---
$result = $minio->putObject([
'Bucket' => $bucket,
'Key' => $objectPath,
'Body' => $fileData,
'ContentType' => $contentType,
'ACL' => 'public-read', // agar bisa diakses publik
]);
return $result['ObjectURL'];
} catch (AwsException $e) {
error_log('Upload MinIO gagal: ' . $e->getMessage());
return false;
} catch (Exception $e) {
error_log('Kesalahan umum: ' . $e->getMessage());
return false;
}
}
?>