patpaskoch's avatar

Uploading images to s3

I try to upload files/images to s3 files just work fine but Intervention\Images I have trouble

$path = $request->file('file')->store('folder', 's3');

this just works fine it returns the path of the file.

I adjust to policy so the folder is public

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

but if try to store a Intervention\Image I get an error response from s3, funny thing is that the image gets uploaded to s3, I just get an error as a response.


$file = request()->file('file');

$fileName = time() . '.' . $file->getClientOriginalExtension();

$image = Image::make($file);

$image->resize(null, 500, function ($constraint) {
    $constraint->aspectRatio();
});

$image = $image->stream();

$path = Storage::disk('s3')->put('folder/' . $fileName, $image->__toString(), 'public');

error message looks like

<Error>
  <Code>NoSuchKey</Code>
  <Message>The resource you requested does not exist</Message>
  <RequestId>sdsf2342sdf234</Resource> 
  <HostId>4442587FB7D0A2F9</RequestId>
</Error>

anybody any idea what i am missing?

0 likes
2 replies
mikefolsom's avatar

I have run into similar issues writing Intervention images to the local filesystem. I'm guessing maybe folder/ doesn't exist, or doesn't have appropriate permissions. My approach has been to add this function to a global helpers file. See if it helps at all...

/**
 * Create a directory with appropriate file permissions.
 */
function make_directory($path, $mode = 0755, $recursive = true, $force = true)
{
    return File::exists($path) or File::makeDirectory($path, $mode, $recursive, $force);
}
mikefolsom's avatar

Just realized this method probably won't work on S3. The Flysystem adapter has a createDir method that might be more appropriate.

Please or to participate in this conversation.