NukeFS: Simplest shortener-based cloud storage


SUBMITTED BY: Guest

DATE: Oct. 15, 2012, 6:12 p.m.

FORMAT: Python

SIZE: 1.7 kB

HITS: 1359

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

comments powered by Disqus