From 7c1d409f69208ea61c778a58fdacbc1fad274ce8 Mon Sep 17 00:00:00 2001 From: Thuan Bui <9248622+10h30@users.noreply.github.com> Date: Fri, 9 May 2025 14:30:07 +0900 Subject: [PATCH] Completed part 6 --- README.md | 3 ++- app/Http/Controllers/UploadController.php | 6 +++--- app/Models/Upload.php | 6 ++++-- config/filesystems.php | 10 ++++++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7510b26..45087b9 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ | 2 | Validation & Bảo mật khi upload | [`part-2-validation-security`](https://github.com/10h30/laravel-file-upload-series/tree/part-2-validation-security) | | 3 | Upload cùng lúc nhiều file | [`part-3-multiple-file-upload`](https://github.com/10h30/laravel-file-upload-series/tree/part-3-multiple-file-upload) | | 4 | Hiển thị và xoá các file đã upload | [`part-4-manage-uploads`](https://github.com/10h30/laravel-file-upload-series/tree/part-4-manage-uploads) | -| 5 | HUpload file lên Amazon S3 | [`part-5-upload-to-s3`](https://github.com/10h30/laravel-file-upload-series/tree/part-5-upload-to-s3) | +| 5 | Upload file lên Amazon S3 | [`part-5-upload-to-s3`](https://github.com/10h30/laravel-file-upload-series/tree/part-5-upload-to-s3) | +| 6 | Temporary URL & Upload lên MinIO | [`part-6-s3-temporary-url-minio`](https://github.com/10h30/laravel-file-upload-series/tree/part-6-s3-temporary-url-minio) | | | > 📖 Mỗi branch tương ứng với một phần trong series blog. Bạn có thể clone và chạy từng phần riêng biệt để dễ theo dõi. diff --git a/app/Http/Controllers/UploadController.php b/app/Http/Controllers/UploadController.php index 6698b0b..089a32c 100644 --- a/app/Http/Controllers/UploadController.php +++ b/app/Http/Controllers/UploadController.php @@ -51,7 +51,7 @@ class UploadController extends Controller $filenameWithoutExtension = pathinfo($originalFilename, PATHINFO_FILENAME); // Lấy tên file không có phần mở rộng $extension = $file->getClientOriginalExtension(); // Lấy phần mở rộng $directory = 'uploads'; // Thư mục lưu file trên disk - $disk = 's3'; // Disk S3 sẽ sử dụng (được định nghĩa trong config/filesystems.php) + $disk = 'minio'; // Disk S3 sẽ sử dụng (được định nghĩa trong config/filesystems.php) // Xác định tên file duy nhất $finalFilename = $originalFilename; // Bắt đầu với tên gốc @@ -66,7 +66,7 @@ class UploadController extends Controller // Lưu file bằng storeAs với tên file mới $storedFilePath = $file->storeAs($directory, $finalFilename, $disk); // Trả về đường dẫn tương đối: 'uploads/ten_file_cuoi_cung.jpg' - $urlFilePath = Storage::disk($disk)->url($storedFilePath); // Trả về URL public của file + $urlFilePath = Storage::disk($disk)->temporaryUrl($storedFilePath, now()->addMinutes(5)); // Trả về URL public của file $storedFilePaths[] = $urlFilePath; // Lưu URL của từng file vào array $storedFilePath; // Thêm đường dẫn file đã lưu vào array $storedFilePaths // Tạo bản ghi mới trong table uploads của database @@ -89,7 +89,7 @@ class UploadController extends Controller { // Xoá file vật lý khỏi disk 'public' dựa vào đường dẫn lưu trong $upload->filename // The disk used for storing was 's3'. - $disk = 's3'; + $disk = 'minio'; if (Storage::disk($disk)->exists($upload->filename)) { Storage::disk($disk)->delete($upload->filename); diff --git a/app/Models/Upload.php b/app/Models/Upload.php index e13f3b8..1a2371d 100644 --- a/app/Models/Upload.php +++ b/app/Models/Upload.php @@ -15,9 +15,11 @@ class Upload extends Model public function getUrlAttribute(): string { // Đảm bảo 's3' là tên disk chính xác được sử dụng để lưu trữ các file này. - $disk = 's3'; + $disk = 'minio'; if ($this->filename) { - return Storage::disk($disk)->url($this->filename); + // Thao tác này tạo ra một URL tạm thời mới mỗi khi thuộc tính 'url' được truy cập. + // URL sẽ có hiệu lực trong 5 phút kể từ thời điểm nó được tạo. + return Storage::disk($disk)->temporaryUrl($this->filename, now()->addMinutes(5)); } return ''; // Hoặc xử lý một cách thích hợp nếu tên tệp là null } diff --git a/config/filesystems.php b/config/filesystems.php index 3d671bd..fdb104e 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -60,6 +60,16 @@ return [ 'report' => false, ], + 'minio' => [ + 'driver' => 's3', + 'key' => env('MINIO_KEY'), + 'secret' => env('MINIO_SECRET'), + 'region' => 'us-east-1', + 'bucket' => env('MINIO_BUCKET'), + 'endpoint' => env('MINIO_ENDPOINT'), + 'use_path_style_endpoint' => true, + ], + ], /*