Django Tweepy 无法访问 Amazon S3 文件 [英] Django Tweepy can't access Amazon S3 file
问题描述
我正在使用 Tweepy,一个推文 Python 库,django-storages
和 boto
.我有一个自定义的 manage.py
命令,它可以在本地正常工作,它从文件系统中获取一个图像并将该图像发送到推文中.但是,如果我将存储更改为 Amazon S3,则无法访问该文件.它给了我这个错误:
I'm using Tweepy, a tweeting python library, django-storages
and boto
. I have a custom manage.py
command that works correctly locally, it gets an image from the filesystem and tweets that image. If I change the storage to Amazon S3, however, I can't access the file. It gives me this error:
raise TweepError('Unable to access file: %s' % e.strerror)
我尝试将存储桶中的图像设为公开".没用.这是代码(它在没有 S3 的情况下工作):
I tried making the images in the bucket "public". Didn't work. This is the code (it works without S3):
filename = model_object.image.file.url
media_ids = api.media_upload(filename=filename) # ERROR
params = {'status': tweet_text, 'media_ids': [media_ids.media_id_string]}
api.update_status(**params)
这一行:
model_object.image.file.url
给我我想要推特的图片的完整网址,如下所示:
Gives me the complete url of the image I want to tweet, something like this:
我也尝试手动构建 url,因为它是存储在我的存储桶中的公共图像,如下所示:
I also tried constructing the url manually, since it is a public image stored in my bucket, like this:
filename = "https://criptolibertad.s3.amazonaws.com/OrillaLibertaria/195.jpg"
但它不起作用.
¿为什么我会收到无法访问文件
错误?
¿Why do I get the Unable to access file
error?
来自 tweepy 的源代码如下所示:
The source code from tweepy looks like this:
def media_upload(self, filename, *args, **kwargs):
""" :reference: https://dev.twitter.com/rest/reference/post/media/upload
:allowed_param:
"""
f = kwargs.pop('file', None)
headers, post_data = API._pack_image(filename, 3072, form_field='media', f=f) # ERROR
kwargs.update({'headers': headers, 'post_data': post_data})
def _pack_image(filename, max_size, form_field="image", f=None):
"""Pack image from file into multipart-formdata post body"""
# image must be less than 700kb in size
if f is None:
try:
if os.path.getsize(filename) > (max_size * 1024):
raise TweepError('File is too big, must be less than %skb.' % max_size)
except os.error as e:
raise TweepError('Unable to access file: %s' % e.strerror)
看起来 Tweepy
无法从 Amazon S3 存储桶中获取图像,但我该如何使其工作?任何建议都会有所帮助.
Looks like Tweepy
can't get the image from the Amazon S3 bucket, but how can I make it work? Any advice will help.
推荐答案
当 tweepy 尝试在 _pack_image
中获取文件大小时出现问题:
The issue occurs when tweepy attempts to get file size in _pack_image
:
if os.path.getsize(filename) > (max_size * 1024):
函数os.path.getsize
假定它在磁盘上有一个文件路径;但是,在您的情况下,它会给出一个 URL.自然地,在磁盘上找不到该文件并引发 os.error
.例如:
The function os.path.getsize
assumes it is given a file path on disk; however, in your case it is given a URL. Naturally, the file is not found on disk and os.error
is raised. For example:
# The following raises OSError on my machine
os.path.getsize('https://criptolibertad.s3.amazonaws.com/OrillaLibertaria/195.jpg')
你可以做的是获取文件内容,暂时保存到本地,然后发推:
What you could do is to fetch the file content, temporarily save it locally and then tweet it:
import tempfile
with tempfile.NamedTemporaryFile(delete=True) as f:
name = model_object.image.file.name
f.write(model_object.image.read())
media_ids = api.media_upload(filename=name, f=f)
params = dict(status='test media', media_ids=[media_ids.media_id_string])
api.update_status(**params)
为了您的方便,我在这里发布了一个完整的示例:https://github.com/izzysoftware/so38134984
For your convenience, I published a fully working example here: https://github.com/izzysoftware/so38134984
这篇关于Django Tweepy 无法访问 Amazon S3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!