gpt4 book ai didi

Python 错误 : builtin function or method object has no attribute 'StringIO'

转载 作者:可可西里 更新时间:2023-11-01 16:12:08 25 4
gpt4 key购买 nike

我只想下载一张图片。然后将其上传到 Amazon S3。但它不起作用。

'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'

我下载和上传它的代码是这样的:

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

我使用的库是 s3 库。我在某个地方下载了这个,所以它应该是标准的。

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)

谁能帮我把这张图片上传到 S3?

最佳答案

在您的代码中:

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

这意味着 self.bucket 是如何被评估为一个字符串的,你显然不能在它上面调用方法“connection”。

因此,为了进一步分析,请查看函数 upload_thumbnail,它需要 bucket=cache_bucket() 作为参数。那就是它需要一个桶对象。

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

您在代码中传递的是字符串!! -> (bucket="fabletest")

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

您的代码应该是这样的。你可能需要对此进行 sanitizer 。但关键是将存储桶和连接对象传递给函数 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

关于Python 错误 : builtin function or method object has no attribute 'StringIO' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com