Upload Profile Image to S3 Bucket

Published 3 months ago by joshuaf

So, I have my own code to upload a profile image to an s3 bucket, but I also want it to work for the settings/profile page. I would think that this would be easy as this is something that most people have to do, but I don't see an easy solution or anything in the docs about it.

Altering the photo controller is not really realistic as I deploy with composer. It is possible to alter the updaet function in update-profile-photo.js asset to point to a different URL, and then rebuild the assets, but this seems wrong.

I tried simply adding a route to direct the /settings/photo post call to my own function, but that never worked for some reason.

Best Answer (As Selected By joshuaf)
Cronix

Laravel stores profile images in the "public" disk as default. You'd just have to set the "public" disk to use S3 in /config/filesystems.php.

'public' => [
    'driver' => 's3',
    'key' => env('AWS_KEY'),
    'secret' => env('AWS_SECRET'),
    'region' => env('AWS_REGION'),
    'bucket' => env('AWS_BUCKET'),
]

And set the AWS_KEY, etc., in your .env with your credentials/settings and include/install league/flysystem-aws-s3-v3 ~1.0 in composer

Cronix
Cronix
3 months ago (574,260 XP)

Laravel stores profile images in the "public" disk as default. You'd just have to set the "public" disk to use S3 in /config/filesystems.php.

'public' => [
    'driver' => 's3',
    'key' => env('AWS_KEY'),
    'secret' => env('AWS_SECRET'),
    'region' => env('AWS_REGION'),
    'bucket' => env('AWS_BUCKET'),
]

And set the AWS_KEY, etc., in your .env with your credentials/settings and include/install league/flysystem-aws-s3-v3 ~1.0 in composer

Cronix
Cronix
3 months ago (574,260 XP)

It would have been nice if they allowed you to set which disk Spark uses for profile images in .env. Maybe I'll do a PR for that, but I'm not using Spark v6 so they probably wouldn't add it in. It will take days to upgrade so I've been avoiding it, and there's nothing really functionally different between v5 and v6 except for using bootstrap4 in v6 vs bs3 in v5 (and below) so it hasn't been a high priority.

joshuaf

Thanks. Worked like a charm. Any recommendation as to how I can force the naming convention? I want to assign specific names instead of the random string.

Also, I want to direct team images into a separate folder. I don't see how I can do that without altering the spark code though.

joshuaf

V6 of spark has per seat team billing. It's a big deal for my site and I kinda thought version 1 would do it, but it's nice to have it now.

Cronix
Cronix
3 months ago (574,260 XP)

Any recommendation as to how I can force the naming convention? I want to assign specific names instead of the random string.

You'd have to edit the storage method like I was showing from yesterday. I haven't tried to mess with it but this is where you'd do it. The pertinent lines are

    // this is setting the filename
    $path = $file->hashName('profiles');

    $disk = Storage::disk('public');

    // you'd want to change this to ->putFileAs() and specify the name
    $disk->put(
        $path, $this->formatImage($file)
    );

https://laravel.com/docs/5.6/filesystem#storing-files

Cronix
Cronix
3 months ago (574,260 XP)

V6 of spark has per seat team billing.

True, I had already implemented that from a resource I found here (great snippits!). The "Charge Per Team User" one. It was just a few lines to add.

https://github.com/jpmurray/awesome-spark

Cronix
Cronix
3 months ago (574,260 XP)

It's using the hash to help guarantee you aren't overwriting an existing image. Otherwise you'd have to check if the image already exists before saving it and if it does, rename it. Using hashes avoids all of that.

joshuaf

I thought that was the case, but since I have a database entry for each team and user and user and those entries have a unique ID associated with them ... my thinking was that I would have a folder for "profile" images and a folder for "team" images and the name of each would simply be the ID number. That way, if there was every any database issue, I would know which image went with which user/team via the name.

Cronix
Cronix
3 months ago (574,260 XP)

Yes it could definitely work that way. You could also save it to a different named bucket for team/user images.

If your question on how to upload it to a S3 bucket has been answered, please mark it as solved to help others.

Please sign in or create an account to participate in this conversation.