Created
February 26, 2014 19:40
-
-
Save meeuw/9236861 to your computer and use it in GitHub Desktop.
fake/dummy swift/cloudfiles server for debugging / development purposes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bottle import route, put, run, request, response | |
from time import time | |
from hashlib import md5 | |
import json | |
from os import makedirs, walk, stat | |
from os.path import dirname | |
from mimetypes import guess_type | |
from datetime import datetime | |
@route('/') | |
def index(): | |
response.set_header( | |
'X-Storage-Url', 'http://localhost:8080/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef') | |
response.set_header( | |
'Content-Type', 'text/xml') | |
response.set_header( | |
'X-Auth-Token', '01234567-89ab-cdef-0123-456789abcdef') | |
response.set_header( | |
'X-Server-Management-Url', 'http://localhost/10000000') | |
response.set_header( | |
'X-Storage-Token', '01234567-89ab-cdef-0123-456789abcdef') | |
response.set_header( | |
'X-CDN-Management-Url', 'http://localhost:8080/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef') | |
response.status = '204 No Content' | |
return '' | |
@route('/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef/') | |
def mossocloudfs(): | |
for dirpath, dirnames, filenames in walk('.'): | |
break | |
return "\n".join(dirnames) | |
@route('/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef/<container>') | |
def mossocloudfs_container_get(container): | |
response.set_header( | |
'X-Container-Object-Count', '4') | |
response.set_header( | |
'Accept-Ranges', 'bytes') | |
response.set_header( | |
'X-Timestamp', time()) | |
response.set_header( | |
'X-Container-Bytes-Used', 101782) | |
response.set_header( | |
'Content-Type', 'text/plain; charset=utf-8') | |
response.set_header( | |
'X-Trans-Id', 'txfca434cfe7f84b5981ae630f7d208408') | |
files = [] | |
for dirpath, dirnames, filenames in walk(container): | |
for filename in filenames: | |
files.append("/".join(dirpath.split('/')[1:]+[filename])) | |
if request.query.format == u'json': | |
ret = [] | |
for file in files: | |
filename = container+'/'+file | |
m = md5() | |
with open(filename) as f: m.update(f.read()) | |
s = stat(filename) | |
mime = guess_type(filename)[0] | |
if not mime: mime = "application/octet-stream" | |
ret.append({"hash": m.hexdigest(), "last_modified": | |
datetime.fromtimestamp(s.st_mtime).strftime('%Y-%m-%dT%H:%M:%S'), | |
"bytes": s.st_size, "name": file, "content_type": mime}) | |
response.status = '200 OK' | |
response.content_type = 'application/json' | |
return json.dumps(ret) | |
else: | |
response.status = '204 No Content' | |
return '' | |
@put('/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef/<container>') | |
def mossocloudfs_container_put(container): | |
response.status = '202 Accepted' | |
return '' | |
@route('/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef/<container>/<path:re:.*>') | |
def mossocloudfs_file_get(container, path): | |
filename = '%s/%s' % (container, path) | |
try: | |
with open(filename) as f: ret = f.read() | |
m = md5() | |
m.update(ret) | |
s = stat(filename) | |
mime = guess_type(filename)[0] | |
if not mime: mime = "application/octet-stream" | |
response.set_header('Accept-Ranges', 'bytes') | |
response.set_header('Last-Modified', | |
datetime.fromtimestamp(s.st_mtime).strftime('%Y-%m-%dT%H:%M:%S')) | |
response.set_header('Etag', m.hexdigest()) | |
response.set_header('X-Timestamp', s.st_mtime) | |
response.set_header('Content-Type', mime) | |
response.set_header('X-Trans-Id', 'tx0123456789abcdef0123456789abcdef') | |
except IOError: | |
response.status = '404 Not found' | |
ret = '' | |
return ret | |
@put('/MossoCloudFS_01234567-89ab-cdef-0123-456789abcdef/<container>/<path:re:.*>') | |
def mossocloudfs_file_put(container, path): | |
m = md5() | |
filename = '%s/%s' % (container, path) | |
try: makedirs(dirname(filename)) | |
except OSError: pass | |
with open(filename, 'w') as f: | |
buf = request.body.read() | |
m.update(buf) | |
f.write(buf) | |
response.set_header('Etag', m.hexdigest()) | |
response.status = '201 Created' | |
run(host='localhost', port=8080) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment