上传的图像可在公共网址使用博托至S3 [英] Upload image available at public URL to S3 using boto

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

问题描述

我工作在Python的网络环境,我可以简单地上传从文件系统到S3的文件中的博托的key.set_contents_from_filename(路径/到/文件)。不过,我想上传一个形象,已经在网络上(比如<一个href="https://pbs.twimg.com/media/A9h_htACIAAaCf6.jpg:large">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).

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

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

如果有一种方式来获得博托的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,如上所述,我得到一个字符串对象有没有属性'告诉'的错误。使用set_contents_from_filename的URL,我得到一个没有这样的文件或目录的错误。该博托存储文档在上传本地文件叶关闭,并没有提及上传文件远程存储

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.

推荐答案

不幸的是,实在是没有任何办法做到这一点。在目前至少没有。我们可以添加一个方法博托,说 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存储到一个水桶为我们的一些能力。这听起来像一个pretty的有用的功能。您可能要张贴到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.

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

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