markusgray's avatar

S3 Flysystem given wrong adapter

I keep getting this error when I try to upload a file.

Argument 1 passed to League\Flysystem\AwsS3v3\AwsS3Adapter::__construct() must be an instance of Aws\S3Client, instance of Aws\S3\S3Client given,

Here's my upload code.

if($request->hasFile('cover')){
     $file = $request->file('cover');
     $ext = $request->file('cover')->guessExtension();
     $filename = md5(time()).$ext;

     Storage::disk('s3')->put($filename, file_get_contents($file));

     $show->cover = $filename;
}

I have league/flysystem-aws-s3-v3 installed.

0 likes
4 replies
markusgray's avatar

Turns out I was using the wrong flysystem/flysystem-aws-s3-v3 version.

guezandy's avatar

@markusgray were you able to get this to work? I have the same situation and i'm getting:

S3Exception in WrappedHttpHandler.php line 152:
Error executing "HeadObject" on "https://s3.amazonaws.com/testrbucket/file.txt"; AWS HTTP error: Client error: 403 (client): 403 Forbidden (Request-ID: BF4C61A3B6218425) 

which i'm guessing is an AWS error, any advice?/Did you do anything special on the AWS side.

Note: I have: "league/flysystem-aws-s3-v3": "~1.0" in composer

filesystem.php configured as:

        's3' => [
            'driver' => 's3',
            'key'    => env('AWS_KEY'),
            'secret' => env('AWS_SECRET'),
            'region' => env('AWS_LOCATION'),
            'bucket' => 'testrbucket',
        ],

and i'm doing:

  public function store() {
  //  \Storage::disk('local')->put('file.txt', 'Contents');
    \Storage::disk('s3')->put('file.txt', 'Contents');
    return "Done";
  }

I was able to get the file to load locally.

markusgray's avatar

@guezandy In order to upload you need to manage permission in your bucket's properties pane. You have to add a bucket policy and cors configs below I will list what I have.

Bucket Policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::[YOUR BUCKET NAME HERE]/*"
        }
    ]
}

CORS

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:8000</AllowedOrigin>
        <AllowedOrigin>[YOUR ADDITIONAL ADDRESS HERE]</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
        <ExposeHeader>x-amz-request-id</ExposeHeader>
        <ExposeHeader>x-amz-id-2</ExposeHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>
guezandy's avatar

@markusgray hey bro, thanks for the help! I was actually able to fix it this morning by allowing the user FullS3Access on the aws dashboard. So nothing on the project just a little configuration problem.

THANKS!

1 like

Please or to participate in this conversation.