K.mawa
K.mawa

Reputation: 51

aws s3 by boto3 key of upload file added something strange strings

now i use python2.7 and s3 and AWS lambda. i try getting key of jpeg file which put on s3. i use boto3. then i have trouble. that is, when i got key by boto3, contents of key is not only path to file but something like suffix strings is add (like '/media/test.jpg.dAAAfd01'. i want to get just only "/media/test.jpg")

if you know hot to solve this error, i would like you to tell me.

error message is like this.

IOError: [Errno 2] No such file or directory: '/media/test.jpg.dAAAfd01'

code i type (py2.7)

s3 = boto3.client('s3')

for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key_with_extention = str(record['s3']['object']['key'])
        key = key_with_extention.split(".")[0] + "." + key_with_extention.split(".")[1]
        download_path = '/tmp/{}'.format(key)

        s3.download_file(Bucket=bucket, Key=key ,Filename=download_path) #i got error here

i tried to use split to solve it. like this,

key = '/media/hoge/hoge/test.jpg.dAAAfd01' key.split(".")[0]+".jpg"

but key.split(".")[0] is not effect.

so i am confused.

help me...

Upvotes: 2

Views: 1735

Answers (2)

K.mawa
K.mawa

Reputation: 51

after many times trial, i got the cause of this result. it is regulation of AWS lambda.

at AWS lambda, you can use only "tmp" directory. and maltiple directory is forbidden by AWS. so i tried to save path like "tmp/foo/bar/foo/bar" using os.makedirs or so on ,and i got error.

in my case,

s3.download_file(Bucket=bucket, Key=key ,Filename=download_path)

download_path has maltiple directory is cause of rasing this error

then i changed download_path into "tmp/test.jpg" i solved it. thank you so much.

Upvotes: 3

Bruno Rubin
Bruno Rubin

Reputation: 356

Try this to get the key:

import urllib  

...

key = urllib.unquote_plus(record['s3']['object']['key'].encode('utf8'))

Upvotes: 0

Related Questions