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

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

问题描述

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



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



如果有一种方法可以获取boto的key.set_contents_from_file或其他一些可以接受URL的命令,并且将图像很好地流传到S3而不必明确下载文件到我的服务器。

  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 =键(桶)
k.key =test
k.set_contents_from_file(url)
k.make_public()
返回成功?
除了异常,e:
返回e

使用set_contents_from_file,我得到一个字符串对象没有属性告诉错误。使用set_contents_from_filename与url,我得到一个没有这样的文件或目录错误。上传本地文件时,博托存储文档将不再提到上传远程存储的文件。

解决方案

不幸的是,真的没有办法做到这一点。至少不在这个时刻。我们可以向boto添加一个方法,例如 set_contents_from_url ,但该方法仍然需要将文件下载到本地机器,然后将其上传。它可能仍然是一个方便的方法,但它不会救你任何东西。



为了做你真正想要做的,我们需要有一些功能S3服务本身将允许我们传递URL,并将URL存储到我们的一个桶中。这听起来像是一个非常有用的功能。你可能想把它发贴到S3论坛。


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).

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

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

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.

解决方案

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.

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天全站免登陆