Home > Software engineering >  Reading zip files from Amazon S3 using pre-signed url without knowing object key and bucket name
Reading zip files from Amazon S3 using pre-signed url without knowing object key and bucket name

Time:09-17

I have a password protected zip file stored in Amazon S3 which I need to read from a python program, extract the csv file from it and read to a dataframe. Initially, I was doing it using the object key and bucket name.

import zipfile
import boto3
import io
import pandas as pd

s3 = boto3.client('s3', aws_access_key_id="<acces_key>",
                        aws_secret_access_key="<secret_key>", region_name="<region>")
s3_resource = boto3.resource('s3', aws_access_key_id="<acces_key>",
                                    aws_secret_access_key="<secret_key>", region_name="<region>")


obj = s3.get_object(Bucket="<bucket_name>", Key="<obj_key>")
with io.BytesIO(obj["Body"].read()) as tf:

    # rewind the file
    tf.seek(0)

    with zipfile.ZipFile(tf, mode='r') as zipf:
        df = pd.read_csv(zipf.open('<file_name.csv>', pwd=b'<password>'), sep='|')
        print(df)

But due to some security concerns, I won't be able to do this anymore. That is, I won't be having object key and bucket name. And since I wont be having key, I will not have the file_name.csv either. All I will have is a pre-signed URL. Is it possible to read the zip files using pre-signed URLs? How do I do that?

CodePudding user response:

pre-signed URL contains all the information you require to download a file. But for that you don't need to use boto3. Instead you should use regular python tools to download files (or here )from the internet where url will be your pre-signed url.

  • Related