Django: How to download file from S3Having Django serve downloadable filesHow to combine 2 or more querysets in a Django view?Does Django scale?How to debug in Django, the good way?Programmatically saving image to Django ImageFieldDownloading a csv file in djangoNeed a minimal Django file upload exampleHow to check Django versiondifferentiate null=True, blank=True in djangoDownloading an entire S3 bucket?Downloading a file from an s3 Bucket to the USERS computer

Is Newton's third law really correct?

How can a clan of females defend themselves in the ancient world against wandering bands?

First occurrence in the Sixers sequence

Is there any way to revive my Sim?

Leaving job close to major deadlines

How can I prevent a user from copying files on another hard drive?

Synaptic Static - when to roll the d6?

What preparations would Hubble have needed to return in a Shuttle?

How to compute the inverse of an operation in Q#?

What is the maximum that Player 1 can win?

What is that ceiling compartment of a Boeing 737?

Definition of 'vrit'

Can a character learn spells from someone else's spellbook and then sell it?

"Prove that ∂A is closed given ∂A = Cl(A) − Int(A)"

I have found ports on my Samsung smart tv running a display service. What can I do with it?

Why are there no file insertion syscalls

Why is Havana covered in 5-digit numbers in Our Man in Havana?

Scaling an object to change its key

Why do you need to heat the pan before heating the olive oil?

Name for a function whose effect is canceled by another function?

Counterfeit checks were created for my account. How does this type of fraud work?

Can the pre-order traversal of two different trees be the same even though they are different?

In the US, can a former president run again?

Bent arrow under a node



Django: How to download file from S3


Having Django serve downloadable filesHow to combine 2 or more querysets in a Django view?Does Django scale?How to debug in Django, the good way?Programmatically saving image to Django ImageFieldDownloading a csv file in djangoNeed a minimal Django file upload exampleHow to check Django versiondifferentiate null=True, blank=True in djangoDownloading an entire S3 bucket?Downloading a file from an s3 Bucket to the USERS computer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















What I want to do is to download file from Amazon S3. I've been looking up the ways to do it but it didn't work well. I saw this question and used one of the answer's code but it didn't work so I am trying to do other way.



What I did is like this.



html



<a href="#" download=" file.file.url "> file.filename </a>


when I click the filename, it actually starts downloading but the file doesn't work. It says we don't support the file type.



I think it's because the path is wrong. When I inspect element on browser, the path looks correct but when I download the file, the file name is like https__... not https://



How can I fix this? What is the cause?
I'm currently checking if it works on local.



Update



my current code
I ended up creating a view to download a file but it still doesn't work even on production. The content of the file is always empty. For image file, it cannot be opened.



def download_file(request, file_pk):
file = get_object_or_404(File, pk=file_pk)
name = file.file.name
path_to_file = "https://%s/media/%s" % (settings.AWS_S3_CUSTOM_DOMAINm name)

resp = HttpResponse(content_type='application/force-download')
resp['Content-Disposition'] = "attachment; filename=%s" % name
resp['X-Sendfile'] = path_to_file
return resp









share|improve this question
























  • is it possible to download directly on browser?

    – JPG
    Mar 25 at 4:30











  • @JPG That's what I want to do. It starts downloading but the content is empty.

    – Ooto
    Mar 25 at 4:34











  • can you check your s3 policies are open ??

    – Nakul Narayanan
    Mar 25 at 6:03

















0















What I want to do is to download file from Amazon S3. I've been looking up the ways to do it but it didn't work well. I saw this question and used one of the answer's code but it didn't work so I am trying to do other way.



What I did is like this.



html



<a href="#" download=" file.file.url "> file.filename </a>


when I click the filename, it actually starts downloading but the file doesn't work. It says we don't support the file type.



I think it's because the path is wrong. When I inspect element on browser, the path looks correct but when I download the file, the file name is like https__... not https://



How can I fix this? What is the cause?
I'm currently checking if it works on local.



Update



my current code
I ended up creating a view to download a file but it still doesn't work even on production. The content of the file is always empty. For image file, it cannot be opened.



def download_file(request, file_pk):
file = get_object_or_404(File, pk=file_pk)
name = file.file.name
path_to_file = "https://%s/media/%s" % (settings.AWS_S3_CUSTOM_DOMAINm name)

resp = HttpResponse(content_type='application/force-download')
resp['Content-Disposition'] = "attachment; filename=%s" % name
resp['X-Sendfile'] = path_to_file
return resp









share|improve this question
























  • is it possible to download directly on browser?

    – JPG
    Mar 25 at 4:30











  • @JPG That's what I want to do. It starts downloading but the content is empty.

    – Ooto
    Mar 25 at 4:34











  • can you check your s3 policies are open ??

    – Nakul Narayanan
    Mar 25 at 6:03













0












0








0


1






What I want to do is to download file from Amazon S3. I've been looking up the ways to do it but it didn't work well. I saw this question and used one of the answer's code but it didn't work so I am trying to do other way.



What I did is like this.



html



<a href="#" download=" file.file.url "> file.filename </a>


when I click the filename, it actually starts downloading but the file doesn't work. It says we don't support the file type.



I think it's because the path is wrong. When I inspect element on browser, the path looks correct but when I download the file, the file name is like https__... not https://



How can I fix this? What is the cause?
I'm currently checking if it works on local.



Update



my current code
I ended up creating a view to download a file but it still doesn't work even on production. The content of the file is always empty. For image file, it cannot be opened.



def download_file(request, file_pk):
file = get_object_or_404(File, pk=file_pk)
name = file.file.name
path_to_file = "https://%s/media/%s" % (settings.AWS_S3_CUSTOM_DOMAINm name)

resp = HttpResponse(content_type='application/force-download')
resp['Content-Disposition'] = "attachment; filename=%s" % name
resp['X-Sendfile'] = path_to_file
return resp









share|improve this question
















What I want to do is to download file from Amazon S3. I've been looking up the ways to do it but it didn't work well. I saw this question and used one of the answer's code but it didn't work so I am trying to do other way.



What I did is like this.



html



<a href="#" download=" file.file.url "> file.filename </a>


when I click the filename, it actually starts downloading but the file doesn't work. It says we don't support the file type.



I think it's because the path is wrong. When I inspect element on browser, the path looks correct but when I download the file, the file name is like https__... not https://



How can I fix this? What is the cause?
I'm currently checking if it works on local.



Update



my current code
I ended up creating a view to download a file but it still doesn't work even on production. The content of the file is always empty. For image file, it cannot be opened.



def download_file(request, file_pk):
file = get_object_or_404(File, pk=file_pk)
name = file.file.name
path_to_file = "https://%s/media/%s" % (settings.AWS_S3_CUSTOM_DOMAINm name)

resp = HttpResponse(content_type='application/force-download')
resp['Content-Disposition'] = "attachment; filename=%s" % name
resp['X-Sendfile'] = path_to_file
return resp






django amazon-s3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 5:59







Ooto

















asked Mar 25 at 4:13









OotoOoto

13718




13718












  • is it possible to download directly on browser?

    – JPG
    Mar 25 at 4:30











  • @JPG That's what I want to do. It starts downloading but the content is empty.

    – Ooto
    Mar 25 at 4:34











  • can you check your s3 policies are open ??

    – Nakul Narayanan
    Mar 25 at 6:03

















  • is it possible to download directly on browser?

    – JPG
    Mar 25 at 4:30











  • @JPG That's what I want to do. It starts downloading but the content is empty.

    – Ooto
    Mar 25 at 4:34











  • can you check your s3 policies are open ??

    – Nakul Narayanan
    Mar 25 at 6:03
















is it possible to download directly on browser?

– JPG
Mar 25 at 4:30





is it possible to download directly on browser?

– JPG
Mar 25 at 4:30













@JPG That's what I want to do. It starts downloading but the content is empty.

– Ooto
Mar 25 at 4:34





@JPG That's what I want to do. It starts downloading but the content is empty.

– Ooto
Mar 25 at 4:34













can you check your s3 policies are open ??

– Nakul Narayanan
Mar 25 at 6:03





can you check your s3 policies are open ??

– Nakul Narayanan
Mar 25 at 6:03












1 Answer
1






active

oldest

votes


















0














That's because of the metadata setting of your file stored in S3.



Can you try to change the metadata of the file you want to download?



The step is below



In your S3 storage, choose the file and select [properties] -> [metadata]
Then, add new metadata as the "Content-Deposition" key and "attachment" value.
The link below is very useful to understand.



https://docs.easydigitaldownloads.com/article/1172-how-do-i-force-files-to-download-in-amazon-s3



Then, you need to change the HTML like this.



 <a href=" file.file.url " download="filename.extension"> file.filename </a>


I'm not sure it works for any type of files. But it worked when I developed a system that downloads a jpeg file.






share|improve this answer

























    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%2f55331188%2fdjango-how-to-download-file-from-s3%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









    0














    That's because of the metadata setting of your file stored in S3.



    Can you try to change the metadata of the file you want to download?



    The step is below



    In your S3 storage, choose the file and select [properties] -> [metadata]
    Then, add new metadata as the "Content-Deposition" key and "attachment" value.
    The link below is very useful to understand.



    https://docs.easydigitaldownloads.com/article/1172-how-do-i-force-files-to-download-in-amazon-s3



    Then, you need to change the HTML like this.



     <a href=" file.file.url " download="filename.extension"> file.filename </a>


    I'm not sure it works for any type of files. But it worked when I developed a system that downloads a jpeg file.






    share|improve this answer





























      0














      That's because of the metadata setting of your file stored in S3.



      Can you try to change the metadata of the file you want to download?



      The step is below



      In your S3 storage, choose the file and select [properties] -> [metadata]
      Then, add new metadata as the "Content-Deposition" key and "attachment" value.
      The link below is very useful to understand.



      https://docs.easydigitaldownloads.com/article/1172-how-do-i-force-files-to-download-in-amazon-s3



      Then, you need to change the HTML like this.



       <a href=" file.file.url " download="filename.extension"> file.filename </a>


      I'm not sure it works for any type of files. But it worked when I developed a system that downloads a jpeg file.






      share|improve this answer



























        0












        0








        0







        That's because of the metadata setting of your file stored in S3.



        Can you try to change the metadata of the file you want to download?



        The step is below



        In your S3 storage, choose the file and select [properties] -> [metadata]
        Then, add new metadata as the "Content-Deposition" key and "attachment" value.
        The link below is very useful to understand.



        https://docs.easydigitaldownloads.com/article/1172-how-do-i-force-files-to-download-in-amazon-s3



        Then, you need to change the HTML like this.



         <a href=" file.file.url " download="filename.extension"> file.filename </a>


        I'm not sure it works for any type of files. But it worked when I developed a system that downloads a jpeg file.






        share|improve this answer















        That's because of the metadata setting of your file stored in S3.



        Can you try to change the metadata of the file you want to download?



        The step is below



        In your S3 storage, choose the file and select [properties] -> [metadata]
        Then, add new metadata as the "Content-Deposition" key and "attachment" value.
        The link below is very useful to understand.



        https://docs.easydigitaldownloads.com/article/1172-how-do-i-force-files-to-download-in-amazon-s3



        Then, you need to change the HTML like this.



         <a href=" file.file.url " download="filename.extension"> file.filename </a>


        I'm not sure it works for any type of files. But it worked when I developed a system that downloads a jpeg file.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 25 at 6:18

























        answered Mar 25 at 6:10









        Ryohei ImaizumiRyohei Imaizumi

        864




        864





























            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%2f55331188%2fdjango-how-to-download-file-from-s3%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







            Popular posts from this blog

            How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

            Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?