Getting Started with Cloudflare R2

Getting Started with Cloudflare R2

Langkah 3: Contoh Kode Implementasi

Silahkan install Laravel fresh atau gunakan projek kamu yang ingin mengemplementasikan R2 ini.

Selanjutnya install package S3

composer require league/flysystem-aws-s3-v3 "^3.0" --with-all-dependencies
./composer.json has been updated
Running composer update league/flysystem-aws-s3-v3 --with-all-dependencies
Loading composer repositories with package information
Updating dependencies
Lock file operations: 4 installs, 3 updates, 0 removals
  - Locking aws/aws-crt-php (v1.2.7)
  - Locking aws/aws-sdk-php (3.336.7)
  - Upgrading guzzlehttp/promises (2.0.3 => 2.0.4)
  - Upgrading league/flysystem (3.29.0 => 3.29.1)
  - Locking league/flysystem-aws-s3-v3 (3.29.0)
  - Locking mtdowling/jmespath.php (2.8.0)
  - Upgrading symfony/deprecation-contracts (v3.5.0 => v3.5.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 3 updates, 0 removals
  - Downloading aws/aws-crt-php (v1.2.7)
  - Downloading mtdowling/jmespath.php (2.8.0)
  - Downloading aws/aws-sdk-php (3.336.7)
  - Downloading league/flysystem-aws-s3-v3 (3.29.0)
  - Installing aws/aws-crt-php (v1.2.7): Extracting archive
  - Upgrading symfony/deprecation-contracts (v3.5.0 => v3.5.1): Extracting archive
  - Upgrading guzzlehttp/promises (2.0.3 => 2.0.4): Extracting archive
  - Upgrading league/flysystem (3.29.0 => 3.29.1): Extracting archive
  - Installing mtdowling/jmespath.php (2.8.0): Extracting archive
  - Installing aws/aws-sdk-php (3.336.7): Extracting archive
  - Installing league/flysystem-aws-s3-v3 (3.29.0): Extracting archive
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.  

  laravel/sail .............................................................................. DONE
  laravel/tinker ............................................................................ DONE
  nesbot/carbon ............................................................................. DONE
  nunomaduro/collision ...................................................................... DONE
  nunomaduro/termwind ....................................................................... DONE

78 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].  

Found 4 security vulnerability advisories affecting 4 packages.
Run "composer audit" for a full list of advisories.

Oke, selanjutnya adalah tambahkan baris berikut ke file .env kita.

R2_ACCESS_KEY_ID=YOUR_ACCESS_KEY
R2_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
R2_BUCKET=YOUR_BUCKET_NAME
R2_ENDPOINT=XXXXXXX.r2.cloudflarestorage.com

Lalu cari key FILESYSTEM_DISK dan ubah nilainya menjadi r2 seperti ini:

FILESYSTEM_DISK=r2

Kira-kira lengkapnya menjadi seperti ini:

FILESYSTEM_DISK=r2

// ...

R2_ACCESS_KEY_ID=17c995******b72dec******87976
R2_SECRET_ACCESS_KEY=fa55efa680d******5ecd898f06ae37761807f******b3bae36643f
R2_BUCKET=nurfachmi-academy
R2_ENDPOINT=https://9deccd0505******7560976cb3ff6.r2.cloudflarestorage.com

Beberapa value di atas sudah disensor dengan karakter *.

Selanjutnya tambahkan pengaturan disk di filesystem yang ada di config/filesystem.php.

<?php

return [

    // ...

    'disks' => [

        // ...

       'r2' => [
            'driver' => 's3',
            'key' => env('R2_ACCESS_KEY_ID'),
            'secret' => env('R2_SECRET_ACCESS_KEY'),
            'region' => 'auto',
            'bucket' => env('R2_BUCKET'),
            'url' => env('R2_URL'),
            'endpoint' => env('R2_ENDPOINT'),
            'use_path_style_endpoint' => env('R2_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],
    ],
];

Sip, sekarang kita beralih ke MVC-nya. Model, View, dan Controller. Let’s go!

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *