Support the ongoing development of Laravel.io →
Requests
Last updated 1 year ago.
0

Have you tried downloading the image off of S3 and opening it using a text editor to see what the contents is? It may contain the URL like you said but good to check.

Your syntax appears to be correct for the Filesystem, have you tried using dd($file) rather than echo?

0

You're right, a text editor shows these files as paths, e.g. "/tmp/phpGZ7FD1"

dd($file) outputs:

UploadedFile {#27 ▼
  -test: false
  -originalName: "Screenshot from 2015-01-20 11:12:20.png"
  -mimeType: "image/png"
  -size: 30755
  -error: 0
}

The problem remains the same: how to upload the file to S3.

Ideally I want to do this without storing the file in a temporary folder on my server. I'm not even sure where the tmp folder is.

Again, any help would be much appreciated.

0

I have come up with a crude solution

  1. Perform basic validation + save the uploaded file to the root folder used by the local filesystem (i.e. /storage/app)
  2. Get the newly saved file from the 'storage/app' folder and then upload it to S3
  3. Delete the file from the 'storage/app' folder.

View (as before)

Controller:

    public function postUploadMedia(Request $request)
    {
        //Get Uploaded File   (using Facade, which is more in-line with official documentation)
        $file = \Illuminate\Support\Facades\Request::file('file');
        
        //Perform some Validation on file type, size etc. here...


        //Set Filename (example)
        $filename = "uploadedfile";

        //Set Directory
        $directory = "media";


        /************************
        * Move File to Local Folder, copy to S3, then delete. 
        * Note storage_path().'/app' is defined in filesystems.php in config
        ************************/

        // Move File
        $file->move(storage_path().'/app', $filename);
        
        //Get File and Put on S3
        $uploadedfile = Storage::get($filename);
        Storage::disk('s3')->put($directory.'/'.$filename, $uploadedfile);
        
        //Delete File from local folder
        Storage::delete($filename);


       //Return view here

        }

Note that I am also saving the filename and location to a mysql table (not shown in code above).

This works, but feels a bit rough, and not very laravel-like.

One of my concerns is what happens to the '/tmp/astag131' files demonstrated in my opening post. I have been unable to find these files or the /tmp folder on my system or in the Laravel directory (is it the root /tmp file??).

It might be that these files/folder are being deleted immediately, but I am concerned that its hidden somewhere (potentially insecure) and filling up with junk, never to be rebooted as it's a production server.

I'm still open to better suggestions

Edit: Thanks to the posts below I can see that this method may result in a snappier experience for users, as they will not have to wait for the upload to S3. I have marked it as the solution to my problem.

Last updated 8 years ago.
0

Nothing wrong with your approach. In fact, it's got a nice advantage. Uploading to S3 takes time and you don't really want the user to have to wait around for that, so you save it locally (not in the temp folder; move it to an uploads folder somewhere) and then show it to the user straight away, who can then go off and be happy someplace else on your site. Then in a background queue you upload the file to S3 and once that's done you can update the database with the S3 stuff and delete the local file.

0

I just took a look at how I'm uploading it in my own code. I'm running it through Intervention Image and uploading it without storing locally looks like:

$file = $request->file('file');

$image = Image::make($image); // facade from the Intervention Image package

$payload = (string)$image->encode();  // encode it as string 

Flysystem::disk('s3')->put($path, $payload); 

I also perform some resizing and so on, but it allows me to go from upload form to directly uploading to Amazon. The only issue is that it does wait for the upload to complete before providing a response to the browser, so a solution where you store it and then queue a job then respond to the user, allowing the queue to perform the actual upload may be better.

0

Thank you everyone for your thoughtful input, you've been very helpful.

I'm going to make a small edit to my post and mark it as the solution. Intervention Image looks great, I'll consider adding it to my project in the future.

0

Larave file system or disk ? which I will choose ? Could you suggest me ? I am newbie in laravel . I know, how they are working. But I do not know which is better and what are the situation of using one of them.

Last updated 5 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.