Home>

I'm running a site built with laravel in a production environment.
I want to upload images using S3.

Error message

I can upload images without problems in my local environment.
An error occurred when using the production environment (ec2).

The following is an error message.

GuzzleHttp \ Exception \ ClientException
Client error: `GET http: // 169.254.169.254/latest/meta-data/iam/security-credentials /` resulted in a `404 Not Found` response:<? Xml version =" 1.0 "encoding =" iso-8859 -1 "?><! DOCTYPE html PUBLIC"-// W3C // DTD XHTML 1.0 Transitional // EN "" http: // www. (Truncated ...)
return new $className ($message, $request, $response, $previous, $ctx);
    }

    / **
     * Get a short summary of the response
     *
     * Will return `null` if the response is not printable.
     *
     * @param ResponseInterface $response
     *
     * @return string | null

Controller contents

use Storage;
--Omitted
public function create (Request $request)
    {
        $this->validate ($request, News :: $rules);
        $news = new News;
        $form = $request->all ();
        if (isset ($form ['image'])) {
            $path = Storage :: disk ('s3')->putFile ('/', $form ['image'],

'public');
            $news->image_path = Storage :: disk ('s3')->url ($path);
        } else {
            $news->image_path = null;
        }
        unset ($form ['_ token']);
        unset ($form ['image']);$news->fill ($form);
        $news->save ();
        return redirect ('news/create');


view contents

@extends ('layouts.news')
@section ('title', 'Create new news')
@section ('content')
    
        
            
                 Create new news 
                <form action = "{{action ('NewsController @ create')}}" method = "post" enctype = "multipart/form-data">
                    @if (count ($errors)>0)
                        <ul>
                            @foreach ($errors->all () as $e)
                                <li>{{$e}}</li>
                            @endforeach
                        </ul>
                    @endif
                    
                        <label for = "title">Title</label>
                        
                            <input type = "text" name = "title" value = "{{old ('title')}}">
                        
                    
                    
                        <label for = "body">Body</label>
                        
                            <textarea name = "body" rows = "20">{{old ('body')}}</textarea>
                        
                    
                    
                        <label for = "title">Image</label>
                        
                            <input type = "file" name = "image">
                        
                    {{csrf_field ()}}
                    <input type = "submit" value = "update">
                     Back to TOP 
                </form>
            
        
    
@endsection


filesystems.php

's3' =>[
            'driver' =>'s3',
            'key' =>env ('AWS_ACCESS_KEY_ID'),
            'secret' =>env ('AWS_SECRET_ACCESS_KEY'),
            'region' =>env ('AWS_DEFAULT_REGION'),
            'bucket' =>env ('AWS_BUCKET'),
        ],


.env

AWS_ACCESS_KEY_ID = ******************
AWS_SECRET_ACCESS_KEY = ***************
AWS_DEFAULT_REGION = ap-northeast-1
AWS_BUCKET = portfolio-salon

Turn off S3 bucket permissions (account, bucket).

Thinking

Because the error was 404, I thought about a description mistake, but since I was going locally, I set it up for production
I think there is something wrong.

I'm sorry, please give me some advice.

Supplemental information (FW/tool version etc.)

Laravel 6.2.0
PHP 7.2.19
cloud 9 (local environment)
ec2 (production environment)

  • Answer # 1

    Create an IAM role on the IAM management screen.
    Type: EC2
    Permission: Set access permission to S3 bucket or choose "Amazon S3 Full Access" from template
    Next, when creating an instance on the EC2 management screen, select the IAM role created earlier.
    For more information, try reading the related knowledge link below.
    I have created an IAM role and want to assign it to an EC2 instance.

    If you create an IAM role, you will be able to access it because the credentials are returned in curl http://169.254.169.254/latest/meta-data/iam/security-credentials/role_name.

  • Answer # 2

    GET http://169.254.169.254/latest/meta-data/iam/security-credentials
    Because it is

    , it seems to be used for the behavior that it is not found when trying to acquire the credential information of EC2 instance.
    How did you give permission to access S3 in the Cloud9 environment?
    Do you give the necessary privileges to the production instance in some way?