2010-11-04 11 views
3

Je veux juste télécharger une image. Ensuite, téléchargez-le sur Amazon S3. Mais ça ne marche pas.Erreur Python: la fonction intégrée ou l'objet méthode n'a pas d'attribut 'StringIO'

'builtin_function_or_method' object has no attribute 'StringIO' 
Traceback (most recent call last): 
    File "flickrDump.py", line 16, in <module> 
    imgpath = s3.upload_thumbnail(thumbnail_name=tools.randomString(10), thumbnail_data=tdata,bucket="fabletest") 
    File "../lib/s3.py", line 52, in upload_thumbnail 
    k.set_contents_from_string(thumbnail_data) 
    File "/usr/lib/pymodules/python2.6/boto/s3/key.py", line 539, in set_contents_from_string 
    self.set_contents_from_file(fp, headers, replace, cb, num_cb, policy) 
    File "/usr/lib/pymodules/python2.6/boto/s3/key.py", line 455, in set_contents_from_file 
    self.send_file(fp, headers, cb, num_cb) 
    File "/usr/lib/pymodules/python2.6/boto/s3/key.py", line 366, in send_file 
    return self.bucket.connection.make_request('PUT', self.bucket.name, 
AttributeError: 'str' object has no attribute 'connection' 

Mon code pour le télécharger et le télécharger est la suivante:

tdata = tools.download("http://farm5.static.flickr.com/4148/5124630813_c11b05e6da_z.jpg") 
imgpath = s3.upload_thumbnail(thumbnail_name=tools.randomString(10), thumbnail_data=tdata,bucket="fabletest") 
print imgpath 

La bibliothèque que je utilise est la bibliothèque s3. J'ai téléchargé quelque part, donc ça devrait être standard.

from boto.s3.connection import S3Connection 
from boto.s3.key import Key 
from boto.s3.bucket import Bucket 
import datetime 

ACCESSKEY = 'MYKEY' 
SECRETKEY = 'MYSECRET' 


def get_bucket_path(bucket,filename,https=False): 
    path = None 
    if isinstance(bucket, Bucket): 
     path = bucket.name 
    else: 
     path = bucket 

    if https: 
     return "https://s3.amazonaws.com/%s/%s" % (path, filename) 
    else: 
     return "http://s3.amazonaws.com/%s/%s" % (path, filename) 

def _aws_keys(): 
    return ACCESSKEY, SECRETKEY 

def _conn(): 
    key,secret = _aws_keys() 
    return S3Connection(key,secret) 

def cache_bucket(conn = _conn()): 
    bucket = conn.create_bucket('mimvicache') bucket.make_public() 
    return bucket 

class AwsException(Exception): 
    def __init__(self,value): 
     self.errorval = value 
    def __str__(self): 
     return repr(self.errorval) 

def upload_thumbnail(thumbnail_name,thumbnail_data=None,thumbnail_path=None,bucket=cache_bucket 
(),conn=_conn(),notes=None,image_id=None): 
    k = Key(bucket) 
    k.key = thumbnail_name 

    if notes is not None: 
     k.set_metadata("notes",notes) 

    if image_id is not None: 
     k.set_metadata("image_id",image_id) 


    if thumbnail_data is not None: 
     k.set_contents_from_string(thumbnail_data) 
    elif thumbnail_path is not None: 
     k.set_contents_from_filename(thumbnail_path) 
    else: 
     raise AwsException("No file name") 

    k.set_acl('public-read') 

    return get_bucket_path(bucket.name,k.key) 

Quelqu'un peut-il m'aider à télécharger cette image sur le site S3?

+0

Je suppose que mon analyse est correcte en ce qui concerne l'erreur. Si vous fournissez votre code complet, y compris l'importation, nous pouvons le résoudre. – pyfunc

+0

Je suppose que j'ai enfin compris. – pyfunc

Répondre

3

Dans votre code:

return self.bucket.connection.make_request('PUT', self.bucket.name,...... 
AttributeError: 'str' object has no attribute 'connection' 

Cela signifie que certains comment self.bucket est évaluée à une chaîne et vous ne pouvez pas appeler évidemment la méthode « connexion » sur elle. Donc, pour une analyse plus poussée, regardez la fonction upload_thumbnail, elle attend que bucket = cache_bucket() comme argument. C'est-à-dire qu'il attend un objet seau.

def upload_thumbnail(thumbnail_name,thumbnail_data=None,thumbnail_path=None,bucket=cache_bucket 
(),conn=_conn(),notes=None,image_id=None) 

Ce que vous passez dans votre code est string !! -> (bucket = "fabletest")

imgpath = s3.upload_thumbnail(thumbnail_name=tools.randomString(10), thumbnail_data=tdata,bucket="fabletest") 

Votre code devrait ressembler à ceci. vous devrez peut-être l'assainir. Mais la clé est de passer le bucket et l'objet connection à la fonction upload_thumbnail.

import S3 
connection = S3.AWSAuthConnection('your access key', 'your secret key') 
buck = connection.create_bucket('mybucketname') 
tdata = tools.download("http://farm5.static.flickr.com/4148/5124630813_c11b05e6da_z.jpg") 
imgpath = s3.upload_thumbnail(thumbnail_name=tools.randomString(10), thumbnail_data=tdata,bucket=buck, conn=connection) 
print imgpath