【发布时间】:2014-04-15 18:56:58
【问题描述】:
我正在尝试创建一个将图像上传到 Amazon S3 存储桶的小应用程序。我终于能够成功上传 something 但是当我在 S3 控制台中检查它时,所有上传的都是 HTML:
<input id="image" name="image" type="file">
烧瓶:
def s3upload(image, acl='public-read'):
key = app.config['S3_KEY']
secret = app.config['S3_SECRET']
bucket = app.config['S3_BUCKET']
conn = S3Connection(key, secret)
mybucket = conn.get_bucket(bucket)
r = redis.StrictRedis(connection_pool = pool)
iid = r.incr('image')
now = time.time()
r.zadd('image:created_on', now, iid)
k = Key(mybucket)
k.key = iid
k.set_contents_from_string(image)
return iid
@app.route('/', methods = ['GET', 'POST'])
def index():
form = ImageForm(request.form)
print 'CHECKING REQUEST'
if form.validate_on_submit():
print 'VALID REQUEST'
image = form.image.data
s3upload(image)
else:
image = None
r = redis.StrictRedis(connection_pool = pool)
last_ten = r.zrange('image:created_on', 0, 9)
print last_ten
images = []
key = app.config['S3_KEY']
secret = app.config['S3_SECRET']
bucket = app.config['S3_BUCKET']
conn = S3Connection(key, secret)
mybucket = conn.get_bucket(bucket)
for image in last_ten:
images.append(mybucket.get_key(image, validate = False))
return render_template('index.html', form=form, images=images)
我之前被告知使用set_contents_from_file 是不正确的,而是使用set_contents_from_string
Flask AttributeError: 'unicode' object has no attribute 'tell'
但是我觉得这可能是问题所在。感谢您的帮助。
【问题讨论】:
-
你能发布完整的回溯吗?
-
没有返回错误,它似乎可以工作,但它只是将该 HTML 上传到文本文件,没有图像。
标签: python amazon-web-services amazon-s3 flask