laravel amazon s3 file upload

Laravel Amazon S3 File Upload

If you want to upload files to Amazon S3 storage service using Laravel, there are several ways to do it. In this article, we will discuss some of the most common ways to upload files to Amazon S3 using Laravel.

Method 1: Using AWS SDK for PHP

The easiest and most commonly used way to upload files to Amazon S3 using Laravel is by using the AWS SDK for PHP. This SDK provides a simple and easy-to-use interface to interact with Amazon S3.

First, you need to install the AWS SDK for PHP using Composer. Run the following command to install the SDK:

composer require aws/aws-sdk-php

Once you have installed the SDK, you need to configure it in Laravel. You can do this by creating a new service provider and adding the following code:

use Aws\S3\S3Client;

// ...

public function register()
{
    $this->app->singleton(S3Client::class, function ($app) {
        return new S3Client([
            'version' => 'latest',
            'region'  => env('AWS_DEFAULT_REGION'),
            'credentials' => [
                'key'    => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY'),
            ],
        ]);
    });
}

Once you have configured the SDK, you can use the following code to upload a file to Amazon S3:

$s3 = app(S3Client::class);
$s3->putObject([
    'Bucket' => env('AWS_BUCKET'),
    'Key'    => 'path/to/file',
    'Body'   => fopen('/path/to/local/file', 'r'),
    'ACL'    => 'public-read',
]);

This code will upload the file located at `/path/to/local/file` to the specified bucket and key on Amazon S3. The `ACL` parameter specifies the access level for the uploaded file. In this case, it is set to `public-read` so that the uploaded file can be accessed by anyone with the URL.

Method 2: Using Flysystem

Another popular way to upload files to Amazon S3 using Laravel is by using Flysystem. Flysystem is a filesystem abstraction layer for PHP that allows you to easily work with local and remote filesystems.

First, you need to install Flysystem and the AWS S3 adapter using Composer. Run the following command to install them:

composer require league/flysystem-aws-s3-v3

Once you have installed Flysystem and the AWS S3 adapter, you need to configure them in Laravel. You can do this by adding the following code to your `config/filesystems.php` file:

'disks' => [

    // ...

    's3' => [
        'driver' => 's3',
        'key'    => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
    ],

    // ...

],

Once you have configured Flysystem, you can use the following code to upload a file to Amazon S3:

use Illuminate\Support\Facades\Storage;

// ...

Storage::disk('s3')->put('path/to/file', fopen('/path/to/local/file', 'r'), 'public');

This code will upload the file located at `/path/to/local/file` to the specified path on Amazon S3. The `public` parameter specifies the access level for the uploaded file. In this case, it is set to `public` so that the uploaded file can be accessed by anyone with the URL.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe