简体   繁体   English

如何在 laravel 5.5 的 s3 存储桶中上传图像

[英]How to upload image in s3 bucket in laravel 5.5

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:我正在尝试将图像存储在 S3 存储桶中,我正在使用 laravel 5.5 我是新手,我被困在这里:我正在尝试的是:

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我的配置/文件系统.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:我从我的 env 文件中获取这些值 my.env 文件如下所示:

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 .请求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:在这里我很困惑如何放置我的区域我也创建了存储桶名称(spikessales)并且我不知道如何提供区域因为我正在提供作为 aws 浏览器 url 存在的区域:看起来像:

  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.正如您在我的 env 文件中看到的那样,我给出了出现在该 url (us-east-2) 末尾的 rgion。 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!任何帮助将不胜感激!

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.在您的AWS API调用中,从您的AWS S3设置中设置区域(它显示在S3存储桶 GUI 中),并且不要注意 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.例如,在我的AWS S3控制台中,它还在 URL 中显示region=us-east-2 ,尽管我在AWS S3设置中设置了EU (Frankfurt)区域。

To find your S3 bucket region follow these steps from this zappysys.com article要查找您的 S3 存储桶区域,请按照这篇 zappysys.com 文章中的这些步骤操作

  1. Open your AWS Console by visiting https://console.aws.amazon.com/通过访问https://console.aws.amazon.com/打开您的 AWS 控制台

  2. From dashboard click on S3 option (or visit https://console.aws.amazon.com/s3/home )从仪表板单击 S3 选项(或访问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单击所需的 S3 存储桶名称

  5. Click on Properties Tab at the top单击顶部的属性选项卡

  6. Now you will see Region for the selected bucket along with many other properties.现在您将看到所选存储桶的区域以及许多其他属性。

在此处输入图像描述

You can now change the region in your env file based on what you see here.您现在可以根据您在此处看到的内容更改 env 文件中的区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM