Ganesh Pitchai
Ganesh Pitchai

Reputation: 55

open and Save excel file in S3 using Python

I have some problem with excel(xlsx) file.I want to just open and save operation using python code.I have tried with python but couldn't found

cursor = context.cursor()
s3 = boto3.resource('s3')

bucket = s3.Bucket('bucket')

objects = bucket.objects.all()
for obj in objects:
  if obj.key.startswith('path/filename'):
    filename=obj.key
    openok=open(obj)
    readok = openok.readlines()
    readok.close()
    print ('file open and close sucessfully')```

Upvotes: 0

Views: 1347

Answers (1)

OsmosisJonesLoL
OsmosisJonesLoL

Reputation: 244

You can't read/interact with files directly on s3 as far as I know. I'd recommend downloading it locally, and then opening it. You can use the builtin tempfile module if you want to save it to a temporary path.

with tempfile.TemporaryDirectory() as tmpdir:
    local_file_path = os.path.join(tmpdir, "tmpfile")
    bucket.download_file(obj.key, local_file_path)
    openok=open(local_file_path)
    readok = openok.readlines()
    readok.close()

Upvotes: 2

Related Questions