This is my python script which downloads the most recent image from my S3 bucket. When I run this script using sudo python script.py
it does as expected,
but not when I run it as python script.py
. In this case, the script finishes cleanly without exceptions or process lockup, but there is no image file.
Why does this happen? Is there something I could do at boto's library end or any other thing?
import boto
import logging
def s3_download():
bucket_name = 'cloudadic'
conn = boto.connect_s3('XXXXXX', 'YYYYYYY')
bucket = conn.get_bucket(bucket_name)
for key in bucket.list('ocr/uploads'):
try:
l = [(k.last_modified, k) for k in bucket]
key = sorted(l, cmp=lambda x, y: cmp(x[0], y[0]))[-1][1]
res = key.get_contents_to_filename(key.name)
except:
logging.info(key.name + ":" + "FAILED")
if __name__ == "__main__":
s3_download()
As @Nearoo in comments had suggested to use except Exception as inst
to catch exceptions.
catching the exception like this
except Exception as inst:
print(type(inst))
print(inst.args)
print(inst)
Should fetch you this error if the script is compiled using python 3x If you would compile your script using python 2.7, this error wouldn't come.
Probably you might be having multiple versions of python on your system,
which should be the reason for your difference in behavior of python script.py
and sudo python script.py
and for the same @mootmoot answer suggests to use virtualenv
'cmp' is an invalid keyword argument for this function
.
Now if you would have googled this error, you should find that cmp
has been deprecated in python 3x and suggestions would be to use key
instead.
add these imports
import functools
from functools import cmp_to_key
and replace with these
key2 = sorted(l, key = cmp_to_key(lambda x,y: (x[0] > y[0]) - (x[0] < y[0])))[-1][1]
res = key2.get_contents_to_filename(key2.name)
(x[0] > y[0]) - (x[0] < y[0])
is an alternate to cmp(x[0], y[0])
cmp
is replaced by key
and cmp_to_key
is used from functools lib