2013-06-05 74 views
0

A做了一個功能,可以從他的社交網絡下載用戶的頭像並將其用作我們的網站頭像。我使用Django 1.5和boto與S3存儲上傳的媒體..以編程方式下載圖片並通過Boto使用ImageField上傳圖片

我做的功能完美的時候運行本地,但由於某種原因,當運行在博託它拋出一個異常。下面是代碼我使用

utils.py

# -*- coding: utf-8 -*- 
import json 
import urllib2 
import requests 
from django.core.files import File 
from django.core.files.temp import NamedTemporaryFile 


def download_photo(url): 
    """ 
    """ 
    r = requests.get(url) 

    img_temp = NamedTemporaryFile(delete=True) 
    img_temp.write(r.content) 
    img_temp.flush() 
    return File(img_temp) 


def graph_fb_profile_image(fb_uid, size=48): 
    url = 'http://graph.facebook.com/%s?fields=picture.height(%s).width(%s)' % (fb_uid, size, size) 
    request = urllib2.Request(url) 
    protocol = urllib2.build_opener() 
    response = protocol.open(request) 
    resp_json = json.load(response) 
    avatar = resp_json.get('picture').get('data').get('url') 
    return avatar 

views.py

 # get user avatar from facebook 
     avatar_url = graph_fb_profile_image(kwargs['response'].get('id'), 320) 
     user.avatar_src.save('avatar_%s.jpg' % (user.id,), download_photo(avatar_url), save=True) 

堆棧錯誤我得到的是以下

Traceback (most recent call last): 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response 
    response = callback(request, *callback_args, **callback_kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/newrelic-1.11.0.55/newrelic/api/object_wrapper.py", line 216, in __call__ 
    self._nr_instance, args, kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/newrelic-1.11.0.55/newrelic/hooks/framework_django.py", line 475, in wrapper 
    return wrapped(*args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view 
    return view_func(*args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/decorators.py", line 29, in wrapper 
    return func(request, request.social_auth_backend, *args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/views.py", line 42, in complete 
    return complete_process(request, backend, *args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/views.py", line 111, in complete_process 
    user = auth_complete(request, backend, *args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/views.py", line 196, in auth_complete 
    *xargs, **xkwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/backends/__init__.py", line 373, in continue_pipeline 
    return authenticate(*args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 59, in authenticate 
    user = backend.authenticate(**credentials) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/backends/__init__.py", line 107, in authenticate 
    out = self.pipeline(pipeline, *args, **kwargs) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/social_auth/backends/__init__.py", line 136, in pipeline 
    result = func(*args, **out) or {} 

File "/var/www/snowflake-env/snowflake/snowflake/apps/accounts/pipeline.py", line 45, in set_user_details 
    user.avatar_src.save('avatar_%s.jpg' % (user.id,), download_photo(avatar_url), save=True) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/django/db/models/fields/files.py", line 86, in save 
    self.name = self.storage.save(name, content) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/django/core/files/storage.py", line 48, in save 
    name = self._save(name, content) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/storages/backends/s3boto.py", line 282, in _save 
    reduced_redundancy=self.reduced_redundancy) 

File "/var/www/snowflake-env/local/lib/python2.7/site-packages/boto/s3/key.py", line 1112, in set_contents_from_file 
    raise AttributeError('fp is at EOF. Use rewind option ' 

AttributeError: fp is at EOF. Use rewind option or seek() to data start. 

任何意見,將不勝感激。

+0

我不明白你意思是「跑在博託」。你能解釋一下嗎? – garnaat

+0

boto作爲後端存儲,所以任何上傳的文件直接進入亞馬遜S3存儲並不存儲在服務器上 –

+0

有關設置的更多詳細信息,請點擊http://django-storages.readthedocs.org/en/latest/backends/amazon- S3.html –

回答

2

OK,我找到了答案,我需要添加尋求(0)我的功能,並使其如下

def download_photo(url): 
    """ 
    """ 
    r = requests.get(url) 

    img_temp = NamedTemporaryFile(delete=True) 
    img_temp.write(r.content) 
    img_temp.flush() 
    img_temp.seek(0) 
    return File(img_temp) 

這解決了這個問題對我來說