Laravel league/flysystem getting file URL with AWS S3

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Managing File URLs in Laravel with league/flysystem and AWS S3 Adapter Introduction: In this article, we will discuss how to utilize the power of Laravel, league/flysystem, and the AWS S3 adapter to create a file management system. We'll cover the basics, including setting up an S3 connection, handling file operations, and generating download URLs for files stored in your S3 bucket. 1. Setting Up League/Flysystem with S3 Adapter: To get started, install the league/flysystem library and its S3 adapter using Composer. Then, configure the Laravel environment to use this new package. Follow these simple steps: - Install packages via Composer: `composer require league/flysystem` and `composer require league/container`. - Add the S3 adapter provider class to Laravel's providers array in config/app.php: `League\Flysystem\AwsS3\AwsS3AdapterServiceProvider::class,` 2. Creating a File Manager Class: To encapsulate your file management logic, create a new class called FileManager which has access to the Flysystem instance and the S3 adapter configuration. This will allow you to easily access different file operations and generate download URLs for files stored in your S3 bucket. 3. Reading and Writing Files Using the AWS S3 Adapter: We use the read() and write() methods provided by Flysystem's S3 adapter to interact with files on the server. Here are some examples of how you can utilize these functions: - To read a file, use `$contents = $filesystem->read('filename.txt');` which will return the contents of the file as a string. - To write a file, use `$filesystem->write('filename.txt', 'Contents');`, where 'Contents' is the content you want to save in the file. 4. Listing Files and Paths: You can also list all files, paths, or just directories using the S3 adapter's listContents() and listPaths() methods. For example: - To get the contents of a directory and its subdirectories, use `$paths = $filemanager->listPaths('path/to/dir');`. - To get all file contents in a specific location, use `$contents = $filemanager->listContents();`. 5. Generating Download URLs for S3 Buckets: The main objective is to generate downloadable links for your files stored on the AWS S3 bucket. Unfortunately, league/flysystem doesn't directly provide this functionality. However, you can use the AWS SDK for PHP to achieve this goal. To accomplish this, you need to set up an AWS account and get your S3 credentials. - First, install the AwsSdk library: `composer require aws/aws-sdk`. - Include the necessary class files in your project: `use Aws\S3\S3Client;` and `use GuzzleHttp\ClientInterface;`. - Create an S3 client with your credentials: `$s3 = new S3Client([ 'region' => 'us-east-2', // Replace this with your region 'version' => 'latest', 'credentials' => [ 'key' => $accessKey, 'secret' => $secretKey, ], ]);` - Now you can use the S3Client's getObjectUrl() method to generate download URLs for your files: `$downloadUrl = $s3->getObjectUrl('[bucket]/[dir]/[file]', '[fileName].ext');`. Replace '[bucket]', '[dir]', and '[fileName].ext' with actual values from your S3 bucket. Conclusion: In this blog post, we've explored how to use Laravel's league/flysystem package and AWS S3 adapter for file management. We provided code examples and a thorough explanation of the steps involved in generating download URLs for files on an S3 bucket with the help of the AWS SDK for PHP library. By following these guidelines, you can build a robust file management system using Laravel and leverage the power of AWS services.