Skip to content
the poor coder

laravel amazon s3 file upload

Poor Man
Published
Reading time
2 min read
On this page

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:

Text
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:

Text
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:

Text
$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:

Text
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:

Text
'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:

Text
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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC