0

I have a flask application that allows users to upload and download files.

However, once a file has been uploaded, I would like for there to be a button that allows them to open that file, but in the browser, as you can with local files.

I created the button and linked it to the script below:

Here's what I've tried:

@app.route('/openFileInBrowser/<filename>')
def openFileInBrowser(filename):
    bucket = 'testBucket'

    s3_client = boto3.client('s3')

    presigned_url = s3_client.generate_presigned_url('get_object', Params = {'Bucket':
    bucket, 'Key': filename}, ExpiresIn = 3600)

    return presigned_url

However, all this did was redirect to a webpage with the presigned_url written on it.

Update:

I am now able to redirect to the presigned_url using return redirect(presigned_url, code=302). However, for files like PDF and TXT, instead of viewing them in the browser, it downloads the file. How am I able to make it so it opens the file in their browser instead?

smac2020
  • 9,637
  • 4
  • 24
  • 38
safnasfsa
  • 107
  • 9
  • refer here: https://stackoverflow.com/questions/24577349/flask-download-a-file – Sharuzzaman Ahmat Raslan Sep 03 '21 at 06:54
  • That discusses downloading files that are stored into memory, I mean with AWS s3 and flask... can you elaborate if you think this can be applied @SharuzzamanAhmatRaslan – safnasfsa Sep 03 '21 at 06:59
  • you need to tell the browser to start the download. right now you just return a "text" of the URL – Sharuzzaman Ahmat Raslan Sep 03 '21 at 07:08
  • But I'm not looking at downloading the file. I want to open that file in the user's browser. As you can with local storage files. Is there a way to view the file hosted in AWS s3, in the browser. @SharuzzamanAhmatRaslan – safnasfsa Sep 03 '21 at 07:10
  • for the browser to show the file, it must download. and depends on what kind of file, not all will be viewable by the browser. use `redirect` to the S3 signed URL. refer here: https://stackoverflow.com/questions/14343812/redirecting-to-url-in-flask – Sharuzzaman Ahmat Raslan Sep 03 '21 at 07:12
  • Nice! That works, however that URL displays my `aws_access_key_id` and `aws_secret_access_key`... is that a problem? @SharuzzamanAhmatRaslan – safnasfsa Sep 03 '21 at 07:43
  • the URL should not contain your key_id or secret. it should be similar like this: `https://presignedurldemo.s3.eu-west-2.amazonaws.com/image.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAJJWZ7B6WCRGMKFGQ%2F20180210%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20180210T171315Z&X-Amz-Expires=1800&X-Amz-Signature=12b74b0788aa036bc7c3d03b3f20c61f1f91cc9ad8873e3314255dc479a25351&X-Amz-SignedHeaders=host` – Sharuzzaman Ahmat Raslan Sep 03 '21 at 07:53
  • This is mine:`https://testlab.s3.amazonaws.com/Tax_Basics_Cheat_Sheet.pdf?AWSAccessKeyId='myAWSAccessKeyID'&Signature=fjTdNaOuTHXK%2Bh7H%2FMa4czwTaj0%3D&Expires=1630655723q` – safnasfsa Sep 03 '21 at 07:57
  • Also, It does not open the file in the browser. It works with Mp4 files and PNG files. However, for something like a PDF, it simply downloads it. Is there a way to fix this? @SharuzzamanAhmatRaslan – safnasfsa Sep 03 '21 at 07:58
  • just adding: you can use [boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) package to manage AWS files, including generating signed URLs. – janpeterka Sep 03 '21 at 08:00
  • @janpeterka is that not what I'm doing? Is there a difference between signed and presigned urls? – safnasfsa Sep 03 '21 at 08:26
  • Yes. When I access the link, it works. However, for files like PDFs, it downloads them instead of opening it in the browser... Is there a way to open the file in browser with the predesigned url @SharuzzamanAhmatRaslan – safnasfsa Sep 03 '21 at 08:33

0 Answers0