Completed part 6

This commit is contained in:
Thuan Bui
2025-05-09 14:30:07 +09:00
parent bfdebd20ec
commit 7c1d409f69
4 changed files with 19 additions and 6 deletions
+2 -1
View File
@@ -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.
+3 -3
View File
@@ -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);
+4 -2
View File
@@ -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
}
+10
View File
@@ -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,
],
],
/*