codingjoe/django-s3file

Uploading with user id as the destination folder

Closed this issue · 1 comments

Hey there,

This plugin is awesome—it's sped up my uploads by roughly 30-40% in comparison to uploading through my application server.

However I'm new to AWS development and Django as well and could use your help. My team has an application that I just took over where users login through a user pool that's managed by Cognito.

When a user logs in, we get their username, store it in the session (e.g. request.session['usr'] = usr), and use that to upload their files into their private folder in our S3 bucket. (e.g. s3bucket/user_id/fileupload_folder).

So I guess I have two questions—is there anyway to append this user ID to the destination path when uploading via your plugin?

I assume that's not an option without forking the project and making my own custom implementation. As an alternative, is there a way to get the file's destination, so that I can move it into the directory of my choice in my view after it's been uploaded? I noticed there's a random directory that's made when uploading (e.g. s3/temp/_XRhseyETnW4NPAGuLDE5g/) so I'm not sure how to get the new file's destination. I'm not sure how efficient that solution is, and if I'd just be better off uploading through the app server to begin with.

Hi @jwill490
yes and no. For what you are trying to do, you will need to either fork this package, or subclass and overwrite some if it's methods.

Anyways. I wouldn't recommend that. You see, this package does not intend that users upload the image to the final destination. This should be handled by your application server. You can easily include the user id in your upload_to callable, or manually move the fine in your view (should you not use model forms).

Why do I think the latter approach is better? You don't want users uploading incomplete files. This is why I recommend setting up expiration for the upload folder.

Let me know if you have any further questions.

Best
-Joe