4

I am trying to store image in S3 bucket and i am using laravel 5.5 i am new and i am stuck here: What i am trying is:

My Controller:

public function imageUploadPost(Request $request)
{
    $this->validate($request, [
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);


    $imageName = time().'.'.$request->image->getClientOriginalExtension();
    $image = $request->file('image');
    $t = Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');
    $imageName = Storage::disk('s3')->url($imageName);


    return back()
        ->with('success','Image Uploaded successfully.')
        ->with('path',$imageName);
}

My routes:

Route::post('s3-image-upload','S3ImageController@imageUploadPost');

My config/filesystems.php

's3' => [
    'driver' => 's3',
    'key'       => env('AccessKeyID'),
    'secret'    => env('SecretAccessKey'),
    'region'    => env('region'),
    'bucket'    => env('mybucket'),
],

And i am getting these values from my env file my .env file looks like:

AccessKeyID=xyz
SecretAccessKey=xyz
region=us-east-2
mybucket=spikessales

Now when i upload file and hit upload button it says :

Encountered a permanent redirect while requesting https://spikessales.s3.us-east-2.amazonaws.com/1519812331.jpg. Are you sure you are using the correct region for this bucket?

Here I am confused how to put my region also I have created bucket name (spikessales) and I dont know how to give region as I am giving region which is present as aws browser url: look like:

  https://s3.console.aws.amazon.com/s3/home?region=us-east-2

I am giving rgion which present at the end of this url (us-east-2) as u can see in my env file. And the region which I have created during creating bucket name is US East(N.Virginia). please tell me how to write region correctly.

Any help will be highly appreciated!

Caconde
  • 4,177
  • 7
  • 35
  • 32
shahzad1122
  • 285
  • 1
  • 6
  • 23
  • check this post :: https://stackoverflow.com/questions/56105104/how-to-fix-upload-image-to-s3-using-laravel/61826028#61826028 – pankaj May 15 '20 at 18:35

2 Answers2

1

In your AWS API call set the region from your AWS S3 settings (it is shown right in S3 bucket GUI), and do not pay attention to region shown in URL.

In my AWS S3 console for example it also shows region=us-east-2 in URL, although I set up EU (Frankfurt) region in AWS S3 settings.

SergeyLebedev
  • 3,673
  • 15
  • 29
0

To find your S3 bucket region follow these steps from this zappysys.com article

  1. Open your AWS Console by visiting https://console.aws.amazon.com/

  2. From dashboard click on S3 option (or visit https://console.aws.amazon.com/s3/home)

  3. You will see all buckets in the left side list

  4. Click on desired S3 bucket name

  5. Click on Properties Tab at the top

  6. Now you will see Region for the selected bucket along with many other properties.

enter image description here

You can now change the region in your env file based on what you see here.

HelloSpeakman
  • 810
  • 9
  • 22