mirror of
https://github.com/10h30/laravel-file-upload-series.git
synced 2026-07-11 18:55:57 +09:00
Completed part 6
This commit is contained in:
@@ -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) |
|
| 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) |
|
| 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) |
|
| 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.
|
> 📖 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.
|
||||||
|
|||||||
@@ -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
|
$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
|
$extension = $file->getClientOriginalExtension(); // Lấy phần mở rộng
|
||||||
$directory = 'uploads'; // Thư mục lưu file trên disk
|
$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
|
// Xác định tên file duy nhất
|
||||||
$finalFilename = $originalFilename; // Bắt đầu với tên gốc
|
$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
|
// 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'
|
$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
|
$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
|
// 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
|
// 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'.
|
// The disk used for storing was 's3'.
|
||||||
$disk = 's3';
|
$disk = 'minio';
|
||||||
|
|
||||||
if (Storage::disk($disk)->exists($upload->filename)) {
|
if (Storage::disk($disk)->exists($upload->filename)) {
|
||||||
Storage::disk($disk)->delete($upload->filename);
|
Storage::disk($disk)->delete($upload->filename);
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ class Upload extends Model
|
|||||||
public function getUrlAttribute(): string
|
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.
|
// Đả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) {
|
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
|
return ''; // Hoặc xử lý một cách thích hợp nếu tên tệp là null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,6 +60,16 @@ return [
|
|||||||
'report' => false,
|
'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,
|
||||||
|
],
|
||||||
|
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user