ProfessorGT's avatar

Need to move file from AWS S3 bucket to Local storage path

Has anyone ever successfully moved a file from AWS to a local file path? I'm trying to load a file using Laravel Excel on a worker but I'm using Heroku so I can't store the file when I upload it. So I first send the file to an S3 bucket. That's working great. But now when the job actually runs on the worker. I need to pull that file from the S3 bucket to a local path so laravel excel can read the file in from the local file system and import the excel file data.

Has anyone ever copied a file from an S3 bucket to a local path successfully? Any help would be greatly appreciated.

Thanks in advance!

I've already dug into the Laravel Excel docs and github to confirm 100% that laravel excel does not support external URLs for loading in an import file. Which really stinks. :(

0 likes
6 replies
ProfessorGT's avatar

This is what I'm doing right now but its not working......

    public function loadUserIntakeData()
    {

        $s3_file = Storage::disk('s3')->get('/templates/'. $this->upload->filename);
        $s3 = Storage::disk('public');
        $s3->put("/storage/templates/" . $this->upload->filename, $s3_file);
        

        // Check if the uploaded spreadsheet can be located
        if ($this->findIntakeSheet() === false) {

            // If the file is unable to be located log an error and fail the job
            Log::error('Upload failed - Unable to locate uploaded intake spreadsheet');
            return false;

        }
        ... other code here ....
    }

    public function findIntakeSheet()
    {

        // Make sure the intake spreadsheet still exists before trying to load it
        if (File::exists(public_path("/storage/templates/" . $this->upload->filename)))
        {
            return true;
        }else{
            return false;
        }
    }

It's properly finding the file on S3, but when I try to read the file locally after this its not finding the file.

Cronix's avatar
Cronix
Best Answer
Level 67

By default, the "local" disk puts files in /storage/app/public. Did you change that? So when you put("/storage/templates/" it's actually putting it in /storage/app/public/storage/templates (with storage repeated)?

1 like
ProfessorGT's avatar

No, I didn't change the path. Let me try that out real quick. Thanks for catching that Cronix!

ProfessorGT's avatar

That was it @Cronix !!!! Thank you so much dude! I had the wrong path. Was banging my head against the desk for a while on that one.

Cronix's avatar

You're welcome! I'm glad it was a simple thing. A second set of eyes always helps lol.

Please or to participate in this conversation.