6

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.

  • Hope this can help you https://stackoverflow.com/questions/62484466/laravel-how-to-use-another-server-as-file-storage – STA Jun 20 '20 at 12:41

2 Answers2

3

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.

Vinayak Sarawagi
  • 914
  • 7
  • 11
  • 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. – Mohammad Abbas Sayed Jun 20 '20 at 15:15
  • 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. – Vinayak Sarawagi Jun 20 '20 at 15:19
  • But how do we do that? Is there any link or tutorial that I can refer to? – Mohammad Abbas Sayed Jun 20 '20 at 15:26
  • 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. – Vinayak Sarawagi Jun 20 '20 at 16:00
  • Sure, I'll look into it. Really appreciate and thankful for your help. :) – Mohammad Abbas Sayed Jun 20 '20 at 16:03
  • 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... – iosifv Jul 23 '20 at 12:11
  • Hi @iosifv, Solution 1 is self-explanatory, in the latter part, I tried to explain the solution 2. – Vinayak Sarawagi Jul 23 '20 at 19:52
  • 4
    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. – iosifv Jul 24 '20 at 08:34
  • 4
    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 frontend – iosifv Jul 24 '20 at 08:36
1
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.