Saving files in a S3 subfolder
If I'm not mistaken, there are no real folders in S3. But there seems to be a possibility to save files with path's so it looks like a folder (when navigated through the AWS console, the folders are even visible).
Having a FileField in Django, how would I append a folder name to it?
E.g. the below works but is being put in the root bucket, which becomes cluttered very quickly. I'd love to put all those files in a 'subfolder'
class presentation(models.Model):
""" Holds the presentation themselves"""
author = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
)
protected_storage = storages.backends.s3boto3.S3Boto3Storage(
acl='private',
querystring_auth=True,
querystring_expire=120, # 2 minutes, try to ensure people won't/can't share
)
python django amazon-s3
add a comment |
If I'm not mistaken, there are no real folders in S3. But there seems to be a possibility to save files with path's so it looks like a folder (when navigated through the AWS console, the folders are even visible).
Having a FileField in Django, how would I append a folder name to it?
E.g. the below works but is being put in the root bucket, which becomes cluttered very quickly. I'd love to put all those files in a 'subfolder'
class presentation(models.Model):
""" Holds the presentation themselves"""
author = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
)
protected_storage = storages.backends.s3boto3.S3Boto3Storage(
acl='private',
querystring_auth=True,
querystring_expire=120, # 2 minutes, try to ensure people won't/can't share
)
python django amazon-s3
If bysubfolder
, you meanbucket
, then mention thebucket
argument toS3Boto3Storage
constructor.
– Sachin Kukreja
Nov 23 '18 at 8:16
add a comment |
If I'm not mistaken, there are no real folders in S3. But there seems to be a possibility to save files with path's so it looks like a folder (when navigated through the AWS console, the folders are even visible).
Having a FileField in Django, how would I append a folder name to it?
E.g. the below works but is being put in the root bucket, which becomes cluttered very quickly. I'd love to put all those files in a 'subfolder'
class presentation(models.Model):
""" Holds the presentation themselves"""
author = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
)
protected_storage = storages.backends.s3boto3.S3Boto3Storage(
acl='private',
querystring_auth=True,
querystring_expire=120, # 2 minutes, try to ensure people won't/can't share
)
python django amazon-s3
If I'm not mistaken, there are no real folders in S3. But there seems to be a possibility to save files with path's so it looks like a folder (when navigated through the AWS console, the folders are even visible).
Having a FileField in Django, how would I append a folder name to it?
E.g. the below works but is being put in the root bucket, which becomes cluttered very quickly. I'd love to put all those files in a 'subfolder'
class presentation(models.Model):
""" Holds the presentation themselves"""
author = models.ForeignKey(User, on_delete=models.CASCADE)
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
)
protected_storage = storages.backends.s3boto3.S3Boto3Storage(
acl='private',
querystring_auth=True,
querystring_expire=120, # 2 minutes, try to ensure people won't/can't share
)
python django amazon-s3
python django amazon-s3
asked Nov 23 '18 at 7:53
Bishonen_PLBishonen_PL
1429
1429
If bysubfolder
, you meanbucket
, then mention thebucket
argument toS3Boto3Storage
constructor.
– Sachin Kukreja
Nov 23 '18 at 8:16
add a comment |
If bysubfolder
, you meanbucket
, then mention thebucket
argument toS3Boto3Storage
constructor.
– Sachin Kukreja
Nov 23 '18 at 8:16
If by
subfolder
, you mean bucket
, then mention the bucket
argument to S3Boto3Storage
constructor.– Sachin Kukreja
Nov 23 '18 at 8:16
If by
subfolder
, you mean bucket
, then mention the bucket
argument to S3Boto3Storage
constructor.– Sachin Kukreja
Nov 23 '18 at 8:16
add a comment |
1 Answer
1
active
oldest
votes
Add the upload_to parameter and have it point to a function.
def my_upload_func(instance, filename):
return f"test/awesome/{filename}"
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
upload_to=my_upload_func
)
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442621%2fsaving-files-in-a-s3-subfolder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add the upload_to parameter and have it point to a function.
def my_upload_func(instance, filename):
return f"test/awesome/{filename}"
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
upload_to=my_upload_func
)
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
add a comment |
Add the upload_to parameter and have it point to a function.
def my_upload_func(instance, filename):
return f"test/awesome/{filename}"
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
upload_to=my_upload_func
)
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
add a comment |
Add the upload_to parameter and have it point to a function.
def my_upload_func(instance, filename):
return f"test/awesome/{filename}"
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
upload_to=my_upload_func
)
Add the upload_to parameter and have it point to a function.
def my_upload_func(instance, filename):
return f"test/awesome/{filename}"
file = models.FileField(
null=False,
blank=False,
help_text='PPT or other presentation accessible only to members',
storage=protected_storage,
upload_to=my_upload_func
)
answered Nov 23 '18 at 8:16
yrekkehsyrekkehs
2471412
2471412
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
add a comment |
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
worked beautifully, thanks a lot! What is the instance for, though, if it's not used in the upload func?
– Bishonen_PL
Nov 23 '18 at 8:46
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
instance is if you wanted to derive some type of attribute from the presentation. let's say you wanted to put it in the bucket of author's name with f"{instance.author.name}/{filename}"
– yrekkehs
Nov 23 '18 at 10:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442621%2fsaving-files-in-a-s3-subfolder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If by
subfolder
, you meanbucket
, then mention thebucket
argument toS3Boto3Storage
constructor.– Sachin Kukreja
Nov 23 '18 at 8:16