Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

laravel upload pdf to AWS S3 bucket for authenticated users

i have application with below steps,

  1. User signup and purchase the course
  2. User submits the answer writing pdf file in application to AWS s3 storage. and i have a piece of code like below,

Upload to S3

$path = $request->file('answer_file')->store('pdffiles','s3');
 return $path;

In AWS i have created a bucket with default configuration and i have created a IAM user. S3 bucket is private and i have not touched other confgurations.

And to view the particular file i have a code like below,

$file_path = "pdffiles/filename.pdf";

        $client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();

        $bucket = config('filesystems.disks.s3.bucket');
        $command = $client->getCommand('GetObject', [
                'Bucket' => $bucket,
                'Key' => $file_path  // file name in s3 bucket which you want to access
            ]);
        $request = $client->createPresignedRequest($command, '+5 minutes');

        $presignedUrl = (string) $request->getUri(); // Get the actual presigned-url

        $headers = [
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => 'attachment; filename="filename.pdf"',
            ];
        return Response::make($presignedUrl, 200, $headers);

So i am able to download the pdf file and also expiry thing also working fine. i am new to AWS s3 storage so i am wondering if this is good enough for a files where only authenticated users can access the files. It would be helpfull if anyone guide me if any extra things i should consider.

0 likes
9 replies
OshaneThomas's avatar

In my company we are using laravel for our main project. This project has a lot of installations (100+) on live server, and every time a new client buy an instance, an sh script copy the https://1921681254.mx/ "core" app, create database and edit .env (and other stuff) so the application can run.

My question is: can I create a core directory and create symlinks to it i https://100001.onl/ nside each client directory? In this way I can save space and manage one installation.

Of course symlinks for everything, except configs, storage and public folder (we use react as front-end).

Thanks

Snapey's avatar

what is the file actually called? Unless it's a single user system it's unlikely to be filename.pdf

Snapey's avatar

why not use the existing Storage drivers?

Deekshith's avatar

@snapey thanks for the reply. right now i am using Storage driver for local storage as it is exceeding the storage so i am using AWS s3 now. And yes filename.pdf is just example name what i am doing is , Once user uploads the file i am storing it in database with file name and a uuid attached to it.

So for filepath i will do like this,

$file_path = "pdffiles/'.$useranswer->filename;

this way i am retrieving the file from aws.

Also i checked temporaryURl() in laravel. this is same as what i have added in the code to get signed URL for that private PDF?

Snapey's avatar

You don't need to use the temporary signed url if you are fetching the file with your server then giving it to the user.

You would use the temporary URL when you want to give them a time limited link to the file

Deekshith's avatar

@Snapey Sorry but i did not get how to do this it would be helpful if you add some piece of code .

I am getting the temporary URL from storage of s3 bucket client like below,

$request = $client->createPresignedRequest($command, '+5 minutes');
$presignedUrl = (string) $request->getUri(); // Get the actual presigned-url
Snapey's avatar
Snapey
Best Answer
Level 122

@Deekshith You might put this in an <a href="{{ $presignedUrl }}"> tag in a blade view so that the user can download the file.

If you want to still have a download route, then you don't need to bother with the presigned step since you are already authenticated with the IAM account

Please or to participate in this conversation.