以编程方式下载图像并使用ImageField通过Boto上传它们

时间:2013-06-05 12:38:35

标签: django boto

A做了一个功能,可以从他的社交网络下载用户的头像并将其用作我们的网站头像。我使用django 1.5和boto与S3存储上传媒体..

我在本地运行时所做的功能完美无缺,但出于某种原因,在boto上运行它会抛出异常。以下是使用

的代码

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.

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:2)

好的,我找到了答案,我需要在我的函数中添加seek(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)

这解决了我的问题