Saving files in a S3 subfolder












0















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
)









share|improve this question























  • If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

    – Sachin
    Nov 23 '18 at 8:16
















0















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
)









share|improve this question























  • If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

    – Sachin
    Nov 23 '18 at 8:16














0












0








0








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
)









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:53









Bishonen_PLBishonen_PL

13910




13910













  • If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

    – Sachin
    Nov 23 '18 at 8:16



















  • If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

    – Sachin
    Nov 23 '18 at 8:16

















If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

– Sachin
Nov 23 '18 at 8:16





If by subfolder, you mean bucket, then mention the bucket argument to S3Boto3Storage constructor.

– Sachin
Nov 23 '18 at 8:16












1 Answer
1






active

oldest

votes


















1














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
)





share|improve this answer
























  • 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














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
});


}
});














draft saved

draft discarded


















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









1














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
)





share|improve this answer
























  • 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


















1














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
)





share|improve this answer
























  • 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
















1












1








1







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
)





share|improve this answer













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
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 8:16









yrekkehsyrekkehs

2701413




2701413













  • 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











  • 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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()