def insert_bucket(project, conn):
""" Creates a new bucket.
Args:
project: A string specifying a project ID.
conn: An S3Connection instance.
Returns:
A JSON string representing a bucket.
"""
bucket_info = request.get_json()
# TODO: Do the following lookup and create under a lock.
if conn.lookup(bucket_info['name']) is not None:
return error('Sorry, that name is not available. '
'Please try a different one.', HTTP_CONFLICT)
index_bucket(bucket_info['name'], project)
conn.create_bucket(bucket_info['name'])
# The HEAD bucket request does not return creation_date. This is an
# inefficient way of retrieving it.
try:
bucket = next(bucket for bucket in conn.get_all_buckets()
if bucket.name == bucket_info['name'])
except StopIteration:
return error('Unable to find bucket after creating it.')
bucket_url = url_for('get_bucket', bucket_name=bucket.name)
response = {
'kind': 'storage#bucket',
'id': bucket.name,
'selfLink': request.url_root[:-1] + bucket_url,
'name': bucket.name,
'timeCreated': bucket.creation_date,
'updated': bucket.creation_date
}
return Response(json.dumps(response), mimetype='application/json')
评论列表
文章目录