NukeFS2: NukeFS concept extended to unlimited file storage (in theory)


SUBMITTED BY: Guest

DATE: Oct. 16, 2012, 6:30 p.m.

FORMAT: Python

SIZE: 1.7 kB

HITS: 1408

  1. #!/usr/bin/env python
  2. # NukeFS2: simplest shortener-based file cloud storage for Python 2.6+
  3. # Chain storage version
  4. # by Multiversum
  5. # Note: it gets damn slow on large files so plz don't upload >1M files
  6. from requests import get
  7. from json import dumps, loads
  8. from urllib import quote_plus, unquote_plus
  9. CHUNKLEN=22000 #current TinyURL effective chunk length limitation
  10. def upload(localpath):
  11. 'Upload a file from localpath to cloud and get its access key'
  12. chunks = []
  13. with open(localpath, 'rb') as fileobj:
  14. while 1:
  15. data = fileobj.read(CHUNKLEN)
  16. if not data: break
  17. chunks.append(''.join(data.encode('base64').splitlines()))
  18. fileobj.close()
  19. baseurl = 'http://tinyurl.com/api-create.php?url='
  20. key = None
  21. for i in xrange(len(chunks),0,-1):
  22. chunkobj = {'data' : chunks[i-1], 'next' : key}
  23. encoded = quote_plus(dumps(chunkobj))
  24. key = get(baseurl+encoded).text[19:]
  25. return key
  26. def download(key, localpath):
  27. 'Download a file to localpath by its access key'
  28. with open(localpath, 'wb') as fileobj:
  29. while 1:
  30. data = get('http://tinyurl.com/'+key, allow_redirects=False).headers['location']
  31. if not data: break
  32. chunkobj = loads(unquote_plus(data))
  33. if not chunkobj['data']: break
  34. fileobj.write(chunkobj['data'].decode('base64'))
  35. if not chunkobj['next']: break
  36. key = chunkobj['next']
  37. fileobj.close()
  38. if __name__ == '__main__': #usage example
  39. megakey = upload('uploaded.zip')
  40. print 'File access key: ', megakey
  41. download(megakey, 'downloaded.zip')
  42. print 'Downloaded.'

comments powered by Disqus