使用 boto 将公共 URL 上可用的图像上传到 S3 [英] Upload image available at public URL to S3 using boto

查看:18
本文介绍了使用 boto 将公共 URL 上可用的图像上传到 S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 网络环境中工作,我可以使用 boto 的 key.set_contents_from_filename(path/to/file) 将文件从文件系统上传到 S3.但是,我想上传已经在网络上的图片(比如 https://pbs.twimg.com/media/A9h_htACIAAaCf6.jpg:large).

I'm working in a Python web environment and I can simply upload a file from the filesystem to S3 using boto's key.set_contents_from_filename(path/to/file). However, I'd like to upload an image that is already on the web (say https://pbs.twimg.com/media/A9h_htACIAAaCf6.jpg:large).

我是否应该以某种方式将图像下载到文件系统,然后像往常一样使用 boto 将其上传到 S3,然后删除图像?

Should I somehow download the image to the filesystem, and then upload it to S3 using boto as usual, then delete the image?

如果有一种方法可以获取 boto 的 key.set_contents_from_file 或其他一些可以接受 URL 并将图像很好地流式传输到 S3 的命令,而无需明确地将文件副本下载到我的服务器,那将是最理想的.

What would be ideal is if there is a way to get boto's key.set_contents_from_file or some other command that would accept a URL and nicely stream the image to S3 without having to explicitly download a file copy to my server.

def upload(url):
    try:
        conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
        bucket_name = settings.AWS_STORAGE_BUCKET_NAME
        bucket = conn.get_bucket(bucket_name)
        k = Key(bucket)
        k.key = "test"
        k.set_contents_from_file(url)
        k.make_public()
                return "Success?"
    except Exception, e:
            return e

使用 set_contents_from_file,如上所述,我收到字符串对象没有属性 'tell'"错误.将 set_contents_from_filename 与 url 一起使用,我收到 No such file or directory 错误.boto 存储文档 没有上传本地文件,并且没有提及上传远程存储的文件.

Using set_contents_from_file, as above, I get a "string object has no attribute 'tell'" error. Using set_contents_from_filename with the url, I get a No such file or directory error . The boto storage documentation leaves off at uploading local files and does not mention uploading files stored remotely.

推荐答案

不幸的是,真的没有任何方法可以做到这一点.至少目前不是.我们可以向 boto 添加一个方法,比如 set_contents_from_url,但该方法仍然需要将文件下载到本地机器然后上传.它可能仍然是一种方便的方法,但它不会为您节省任何东西.

Unfortunately, there really isn't any way to do this. At least not at the moment. We could add a method to boto, say set_contents_from_url, but that method would still have to download the file to the local machine and then upload it. It might still be a convenient method but it wouldn't save you anything.

为了做您真正想做的事情,我们需要在 S3 服务本身上有一些功能,允许我们将 URL 传递给它并让它为我们将 URL 存储到存储桶中.这听起来像是一个非常有用的功能.您可能想将其发布到 S3 论坛.

In order to do what you really want to do, we would need to have some capability on the S3 service itself that would allow us to pass it the URL and have it store the URL to a bucket for us. That sounds like a pretty useful feature. You might want to post that to the S3 forums.

这篇关于使用 boto 将公共 URL 上可用的图像上传到 S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆