Php laravel Upload file directly to AWS S3 bucket
Asked Answered
L

2

7

Can anyone help me how to upload a file into aws S3 bucket using PHP laravel. But the file should directly get uploaded into S3 using pre signed URL.

Lawyer answered 20/6, 2020 at 11:46 Comment(1)
Hope this can help you #62484966Invocation
W
4

I will try to answer this question. So, there are two ways to do this:

  1. You send the pre-signed URL to Frontend Client and let them upload the file to S3 directly, and once uploaded they notify your server of the same.

  2. You receive the file directly on the server and upload it to S3, in this case, you won't need any pre-signed URL, as you would have already configured the AWS access inside the project.


Since solution 1 is self-explanatory, I will try to explain the solution 2.

Laravel provides Storage Facade for handling filesystem operations. It follows the philosophy of multiple drivers - Public, Local Disk, Amazon S3, FTP plus option of extending the driver.

Step 1: Configure your .env file with AWS keys, you will need the following values to start using Amazon S3 as the driver:

  • AWS Key
  • AWS Secret
  • AWS Bucket Name
  • AWS Bucket Region

Step 2: Assuming that you already have the file uploaded to your server. We will now upload the file to S3 from our server.

If you have mentioned s3 as the default disk, following snippet will do the upload for you:

Storage::put('avatars/1', $fileContents);

If you are using multiple disks, you can upload the file by:

Storage::disk('s3')->put('avatars/1', $fileContents);

We are done! Your file is now uploaded to your S3 bucket. Double-check it inside you S3 bucket.


If you wish to learn more about Laravel Storage, click here.

Wilsonwilt answered 20/6, 2020 at 14:45 Comment(9)
Thanks for your swift help. Can you help me with the 1st way to do that. Since I am new to AWS. The requirement is the file should be directly stored into the s3, without coming on to the server where the laravel project is hosted.Lawyer
Go for solution 1 then, it saves the resource on your server too. Upload the file directly to S3 from Frontend. To get pre-signed URL, you can create an API which gives new upload URLs everytime.Wilsonwilt
But how do we do that? Is there any link or tutorial that I can refer to?Lawyer
There are plenty with explaining how file upload works in Laravel. But I am afraid that you will find any tutorial for your use case. But if you have any specific question, you can always reach out to the community.Wilsonwilt
Sure, I'll look into it. Really appreciate and thankful for your help. :)Lawyer
I don't understand why this was voted as an answer since it literally states that the answer is "self explanatory" then you answer something else...Herb
Hi @iosifv, Solution 1 is self-explanatory, in the latter part, I tried to explain the solution 2.Wilsonwilt
first of all OP asked for solution1 which was not answered. Second it's not self explanatory, you have the presigned url and you do what with it in order to send a file purely in the frontend?? I am actually trying to do this myself and can't find any documentation regarding this.Herb
and solution 2 is completely irrelevant in this case since OP specifically requested NOT TO SEND TO SERVER and make the upload exclusively in the frontendHerb
S
2
use Storage;
use Config;

$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');

$command = $client->getCommand('PutObject', [
    'Bucket' => $bucket,
    'Key' => '344772707_360.mp4'  // file name in s3 bucket which you want to access
]);

$request = $client->createPresignedRequest($command, '+20 minutes');

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

We can use 'PutObject' to generate a signed-url for uploading files onto S3.

Saloop answered 16/6, 2021 at 5:44 Comment(2)
And then what do you do with this signed url?Franconian
You may check out this answer for how the pre-signed URL was used in js to upload files to S3Saloop

© 2022 - 2024 — McMap. All rights reserved.