Files
egbim_homepage/egbim/sw_upload/upload_presign.php
T
2026-07-23 16:15:57 +09:00

75 lines
2.2 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json; charset=utf-8');
require __DIR__ . '/../vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Credentials\Credentials;
// === 버킷/키 설정 ===
$bucket_name = "baron-software-test";
$account_id = "81fa2d48964d31dd0da9558f9ce601d1";
$access_key = "9ade5915f7abd402db60a87f3b3b6d76";
$secret_key = "a1a1cfd59f739977f98fb73f214894e39dbac9a39b1f6208020aa2e1303ea54c";
// === 프론트에서 넘어온 값 ===
$product_code = $_POST['product_code'] ?? '';
$sw_version = $_POST['sw_version'] ?? '';
$filename = $_POST['filename'] ?? '';
$content_type = $_POST['content_type'] ?? 'application/octet-stream';
if (!$product_code || !$sw_version || !$filename) {
http_response_code(400);
echo json_encode(['error' => '필수값 누락 (product_code, sw_version, filename)']);
exit;
}
// === 제품 코드 → 실제 폴더명 매핑 ===
$map = [
"1" => "eg-bim",
"2" => "tova",
"3" => "gaia",
"999" => "test"
];
$folder = $map[$product_code] ?? $product_code; // 혹시 매핑 없으면 그대로 사용
// === 업로드 경로 ===
// ex) eg-bim/1.2.3/setup_eg-bim_1.2.3.exe
$key = "{$folder}/{$sw_version}/{$filename}";
try {
$credentials = new Credentials($access_key, $secret_key);
$s3 = new S3Client([
'region' => 'auto',
'version' => 'latest',
'endpoint' => "https://{$account_id}.r2.cloudflarestorage.com",
'credentials' => $credentials,
]);
// Presign URL 생성
$cmd = $s3->getCommand('PutObject', [
'Bucket' => $bucket_name,
'Key' => $key,
'ContentType' => $content_type
]);
$request = $s3->createPresignedRequest($cmd, '+1 hour');
echo json_encode([
'url' => (string)$request->getUri(), // 브라우저에서 PUT 할 주소
'key' => $key // DB에 저장할 전체 경로
]);
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'error' => 'Presign 실패',
'detail' => $e->getMessage()
]);
}
?>