1

I am trying to upload a file to AWS s3 bucket

public function fileTest(Request $request)
  {

      //this will create folder test inside my bucket and put the contents
      $request->file->store('test','s3');
  }

But the above code will create a folder named test inside my bucket and put the file into it.

But i want to directly upload to the bucket

$request->file->store('s3');

I've tried above code but dosen't worked.

Jabaa
  • 1,743
  • 7
  • 31
  • 60
  • you can check this solution. i have detailed explained. https://stackoverflow.com/questions/56105104/how-to-fix-upload-image-to-s3-using-laravel/61826028#61826028 – pankaj May 15 '20 at 18:30
  • you can check my solution that is easy and clear. https://stackoverflow.com/questions/56105104/how-to-fix-upload-image-to-s3-using-laravel/61826028#61826028 – pankaj Jul 31 '20 at 08:08

4 Answers4

2

Solved my issue with following

  $s3 = Storage::disk('s3');
  $s3->put('',$request->file);

Shorthand

Storage::disk('s3')->put('',$request->file)

Jabaa
  • 1,743
  • 7
  • 31
  • 60
2
public function uploadFile (Request $request) {
    $file = $request->file('file');

    $imageName = 'uploads/' . $file->getClientOriginalName();

    Storage::disk('s3')->put($imageName, file_get_contents($file));
    Storage::disk('s3')->setVisibility($imageName, 'public');

    $url = Storage::disk('s3')->url($imageName);

    return Response::json(['success' => true, 'response' => $url]);
}
Hemant Sah
  • 31
  • 3
2

Put your file on s3 server

    $agreement_contract_copy = $request->file('agreement_contract');
    $filename = $id. '/agreement' . '_' . time() . '.pdf';
    $student->agreement_copy = $filename;
    $student->save();
    Storage::disk('s3')->put($filename, file_get_contents($agreement_copy));

If you can not define as public so default its store as private and when you use private method so in file geting time used temp method as below. Get your file

Storage::disk('s3')->temporaryUrl
    ($student->agreement_copy, \Carbon\Carbon::now()->addMinutes(5))

$student->agreement_copy

Tested

Kaushik shrimali
  • 1,178
  • 8
  • 15
0

In a very short solution.

Storage::disk('s3')->put("destination_path", file_get_contents("BinaryResourse"));
pankaj
  • 1
  • 17
  • 36
  • more detail; please check https://stackoverflow.com/questions/56105104/how-to-fix-upload-image-to-s3-using-laravel/61826028#61826028 – pankaj May 15 '20 at 18:33