JOSS

JOSS with Laravel

Estimated reading: 3 minutes 101 views

Tutorial: Meng-upload File ke JOSS Menggunakan Laravel

Pendahuluan

Anda dapat menyimpan file langsung ke bucket JOSS dari aplikasi Laravel Anda. Berikut cara meng-upload file ke JOSS menggunakan Laravel.

Tested on: Laravel 9.19

  1. Konfigurasi S3 di Laravel
    a. Install Dependensi
    Anda perlu menginstal package yang diperlukan untuk menggunakan S3 dengan Laravel. Jalankan perintah berikut di terminal Anda:

    composer require aws/aws-sdk-php-laravel league/flysystem-aws-s3-v3

    b. Update Konfigurasi filesystems.php
        Buka file config/filesystems.php dan tambahkan konfigurasi berikut untuk disk S3:

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => '', // Kosongkan ini
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_URL'),
        'use_path_style_endpoint' => true,
        'throw' => false,
    ],

    c. Update File .env
    Pastikan Anda mengatur variabel lingkungan di file .env Anda dengan benar:

    AWS_ACCESS_KEY_ID=your-joss-access-key
    AWS_SECRET_ACCESS_KEY=your-joss-secret-key
    AWS_BUCKET=your-bucket-name
    AWS_URL=https://s3.jetdino.id

  2. Buat Route dan Controller

    a. Buat controller untuk upload sederhana ke S3:

    php artisan make:controller S3Controller
    

    b. Isi S3Controller.php

    <?php
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Storage;
    
    class S3Controller extends Controller
    {
        public function uploadFile(Request $request)
        {
            if ($request->hasFile('file')) {
                $path = $request->file('file')->store('uploads', 's3');
                return response()->json(['url' => Storage::disk('s3')->url($path)]);
            }
            return response()->json(['error' => 'No file uploaded'], 400);
        }
    }

    c. Buat Route untuk Upload
    Tambahkan route berikut ke file routes/web.php:

    <?php
    use Illuminate\Support\Facades\Route;
    use App\Http\Controllers\S3Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Storage;
    
    Route::get('/upload', function () {
        return view('upload');
    });
    
    Route::post('/upload', function (Request $request) {
        $request->validate([
            'file' => 'required|file',
        ]);
    
        Log::info("File yang diterima", ['filename' => $request->file('file')->getClientOriginalName()]);
    
        // Simpan file ke S3
        try {
            $path = Storage::disk('s3')->putFile('uploads', $request->file('file'));
    
            Log::info("Path yang dikembalikan: ", ['path' => $path]);
    
            if (!$path) {
                throw new \Exception("Path kosong, upload gagal.");
            }
    
            return "File berhasil di-upload! Path: $path";
        } catch (\Exception $e) {
            Log::error("Error saat meng-upload file: " . $e->getMessage());
            return "Gagal meng-upload file: " . $e->getMessage();
        }
    });

    d. Buat View untuk Upload
    Buat file upload.blade.php di folder resources/views dan tambahkan form berikut:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Upload File</title>
    </head>
    <body>
        <h1>Upload File ke JOSS</h1>
        <form action="/upload" method="POST" enctype="multipart/form-data">
            @csrf
            <input type="file" name="file" required>
            <button type="submit">Upload</button>
        </form>
    </body>
    </html>

    e.  Uji Upload

    1) Akses aplikasi Anda melalui browser di http://url-laravel/upload.
    2) Pilih file untuk di-upload dan klik tombol “Upload”. Anda akan melihat pesan yang menunjukkan apakah upload berhasil atau gagal.

Leave a Comment

Share this Doc

JOSS with Laravel

Or copy link

CONTENTS