Skip to content
Advertisement

Individual s3 bucket for each user in laravel [closed]

I have a Laravel application, I want to have a secure storage system for my registered users.

  1. How should I plan for it, should I create a new bucket for each user, or create folders for each user inside one bucket
  2. How can I restrict users not to access each others folders?

Thank you

Advertisement

Answer

Definitely go with one bucket for all and then separate it with folders. Buckets is more to separate apps for interaction with each other in my opinion.

I would create a prefix and always use that, you could secure they can’t interact with each other files, if the code uses the same functions.

public function saveFile($user, $fileName, $file) {
    Storage::put($user->getS3Prefix() . $fileName, $file )
}

public function getFile() {
    return Storage::get($user->getS3Prefix() . $fileName)
}

Now on your user model, have a prefix generation, in theory that could be the user id.

class User {
    public function getS3Prefix() {
        return "/{$this->id}/";
    }
}

This is a simple straight forward solution and i do not feel like you need to overthink this.

There is a clever solution, S3 config has a root option, that indicates which folder there should be the root.

So in a service provider, you could actually do something like this.

if ($user = Auth::user()) {
    config(['storage.disks.s3.root' => "/{$user->id}/"]);
}

I think this is a little to flaky solution and hard to control whenever there is not a user in context for example in commands or jobs. But think it is appropriate as another alternative solution.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement