Files

100 lines
2.8 KiB
PHP

<?php
require_once __DIR__ . '/../minio/vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
/**
* Upload file ke MinIO storage
*/
// function uploadToMinioStorageRaw($imageData, $objectPath) {
// $minio = new S3Client([
// 'version' => 'latest',
// 'region' => 'us-east-1',
// 'endpoint' => 'https://storage.manjapro.net',
// 'use_path_style_endpoint' => true,
// 'credentials' => [
// 'key' => 'admin',
// 'secret' => '@P4ssw0rd',
// ],
// ]);
// $bucket = 'files';
// try {
// $result = $minio->putObject([
// 'Bucket' => $bucket,
// 'Key' => $objectPath,
// 'Body' => $imageData, // <== langsung dari memori, bukan file
// 'ContentType' => 'image/png',
// 'ACL' => 'public-read',
// ]);
// return $result['ObjectURL'];
// } catch (Exception $e) {
// error_log('Upload MinIO gagal: ' . $e->getMessage());
// return false;
// }
// }
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;
}
}
?>