将图片从facebook messenger url上传到aws s3

时间:2017-11-24 18:13:36

标签: python amazon-web-services amazon-s3 facebook-messenger-bot

以下情况:用户向facebook messenger发送图像,facebook提供了一个类似于此的网址

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

我将此url存储在名为url的变量中。现在我想将该图像上传到我的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) 

但是,像这样的Python会抛出一个错误,没有关于url的文件或目录。怎么走?

1 个答案:

答案 0 :(得分:0)

正如@jordanm建议将文件下载到django项目静态文件夹,将其从那里上传到S3,然后从静态工作中删除文件。