Search code examples
pythonamazon-web-servicesamazon-s3facebook-messenger-bot

Upload image from facebook messenger url to aws s3


Following situation: A user sends an image to facebook messenger and facebook provides an url that looks like this

https://scontent.xx.fbcdn.net/v/t35.0-12/23998500_10155996498283474_1759188226_o.jpg?_nc_ad=z-m&_nc_cid=0&oh=44c94cc85e75048cc4355e7422281cf5&oe=5A1A5AE7

I am storing this url in a variable called url. Now I would like to upload that image to my AWS S3:

import boto
import os

AWS_ACCESS_KEY_ID = ‘somekey’
AWS_SECRET_ACCESS_KEY = 'somesecret'
END_POINT = 'us-east-1'                          
S3_HOST = 's3.us-east-1.amazonaws.com'              
BUCKET_NAME = 'somestorage'

def upload_s3(url):
    fname = url
    uploaded_fname = 'somename'
    s3 = boto.s3.connect_to_region(END_POINT,
                               aws_access_key_id=AWS_ACCESS_KEY_ID,
                               aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
                               host=S3_HOST)

    bucket = s3.get_bucket(BUCKET_NAME)
    k = Key(bucket)
    k.key = uploaded_fname
    k.set_contents_from_filename(fname) 

However, like this Python throws me an error no such file or directory with regard to the url. What's the way to go?


Solution

  • As suggested by @jordanm downloading the file to the django project static folder, uploading it from there to S3 and then deleting the file then from static worked.