我正在使用Tweepy,一個推特python庫,django-storages
和boto
。我有一個自定義的manage.py
命令,可以在本地正常工作,它從文件系統獲取圖像併發送圖像。但是,如果我將存儲更改爲Amazon S3,則無法訪問該文件。它給了我這個錯誤:Django Tweepy無法訪問Amazon S3文件
raise TweepError('Unable to access file: %s' % e.strerror)
我試圖讓鬥中的圖像「公開」。沒有工作。這是代碼(它的工作原理沒有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
給我我想要的鳴叫圖像的完整URL,這樣的事情:
我也嘗試過手動構建網址,因爲它是存儲在我的存儲桶中的公共圖像,如下所示:
filename = "https://criptolibertad.s3.amazonaws.com/OrillaLibertaria/195.jpg"
但它不起作用。
¿爲什麼我得到Unable to access file
錯誤?
從tweepy源代碼如下所示:
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
無法從亞馬遜S3存儲的圖像,但我怎樣才能使它發揮作用?任何建議都會有幫助。
url是一種方法,而不是屬性。 –
@BurhanKhalid我應該怎麼稱呼它?如果我做'model_object.image.file.url()'我得到的錯誤:''S3BotoStorageFile'對象沒有屬性'url'' – alejoss
'model_object.image.url()' –