def upload(local, remote): import json import ubuntuone.couch.auth as auth import mimetypes import urllib # Create remote path (which contains volume path) base = "https://one.ubuntu.com/api/file_storage/v1/~/" answer = auth.request(base + urllib.quote(remote), http_method="PUT", request_body='{"kind":"file"}') node = json.loads(answer[1]) # Read info about local file try: data = bytearray(open(local, 'rb').read()) size = len(data) content_type = mimetypes.guess_type(local)[0] content_type = content_type or 'application/octet-stream' headers = {"Content-Length": str(size), "Content-Type": content_type} # Upload content of local file to content_path from original response try: base = "https://files.one.ubuntu.com" url = base + urllib.quote(node.get('content_path'), safe="/~") auth.request(url, http_method="PUT", headers=headers, request_body=data) except TypeError: dirs = remote.split('/') print dirs[0] + ' is not a volume' except IOError as e: print local + ":\n\tI/O error({0}): {1}".format(e.errno, e.strerror)
def put(local, remote): import json import ubuntuone.couch.auth as auth import mimetypes import urllib # Create remote path (which contains volume path) base = "https://one.ubuntu.com/api/file_storage/v1/~/" answer = auth.request(base + urllib.quote(remote), http_method="PUT", request_body='{"kind":"file"}') node = json.loads(answer[1]) # Read info about local file data = bytearray(open(local, 'rb').read()) size = len(data) content_type = mimetypes.guess_type(local)[0] content_type = content_type or 'application/octet-stream' headers = {"Content-Length": str(size), "Content-Type": content_type} # Upload content of local file to content_path from original response base = "https://files.one.ubuntu.com" url = base + urllib.quote(node.get('content_path'), safe="/~") auth.request(url, http_method="PUT", headers=headers, request_body=data)
def createVolume(self, name): import ubuntuone.couch.auth as auth import urllib try: base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/" auth.request(base + urllib.quote(name), http_method="PUT") return True except: return False
def get(self, filename, local_path, raise_errors=False): """Get file and put in local_path (Path object)""" import json import ubuntuone.couch.auth as auth remote_full = self.meta_base + self.quote(filename) answer = auth.request(remote_full) self.handle_error(raise_errors, 'get', answer, remote_full, filename) node = json.loads(answer[1]) remote_full = self.content_base + self.quote(node.get('content_path')) answer = auth.request(remote_full) self.handle_error(raise_errors, 'get', answer, remote_full, filename) f = open(local_path.name, 'wb') f.write(answer[1]) local_path.setdata()
def get(remote, local): import json import ubuntuone.couch.auth as auth import curllib # Request metadata base = "https://one.ubuntu.com/api/file_storage/v1/~/" answer = auth.request(base + urllib.quote(remote)) node = json.loads(answer[1]) # Request content base = "https://files.one.ubuntu.com" url = base + urllib.quote(node.get('content_path'), safe="/~") answer = auth.request(url) f = open(local, 'wb') f.write(answer[1])
def get_metadata(path,base): import json import ubuntuone.couch.auth as auth import urllib # Request user metadata url = base + urllib.quote(path) + "?include_children=true" answer = auth.request(url) return answer
def delete(self, filename_list, raise_errors=False): """Delete all files in filename list""" import types import ubuntuone.couch.auth as auth assert type(filename_list) is not types.StringType for filename in filename_list: remote_full = self.meta_base + self.quote(filename) answer = auth.request(remote_full, http_method="DELETE") self.handle_error(raise_errors, 'delete', answer, remote_full, ignore=[404])
def download(remote, local): import json import ubuntuone.couch.auth as auth import urllib # Request metadata base = 'https://one.ubuntu.com/api/file_storage/v1/~/' answer = auth.request(base + urllib.quote(remote)) node = json.loads(answer[1]) if node.get('content_path'): try: # Request content base = "https://files.one.ubuntu.com" url = base + urllib.quote(node.get('content_path'), safe = "/~") answer = auth.request(url) f = open(local, 'wb') f.write(answer[1]) except: print traceback.print_exc() else: print '%s does not exist' % remote
def query(path): import json import ubuntuone.couch.auth as auth import urllib # Request metadata base = "https://one.ubuntu.com/api/file_storage/v1/~/" url = base + urllib.quote(path) answer = auth.request(url) node = json.loads(answer[1]) # Print interesting info print 'Size:', node.get('size')
def list(self, raise_errors=False): """List files in that directory""" import json import ubuntuone.couch.auth as auth import urllib remote_full = self.meta_base + "?include_children=true" answer = auth.request(remote_full) self.handle_error(raise_errors, 'list', answer, remote_full) filelist = [] node = json.loads(answer[1]) if node.get('has_children') == True: for child in node.get('children'): path = urllib.unquote(child.get('path')).lstrip('/') filelist += [path] return filelist
def put(self, source_path, remote_filename = None, raise_errors=False): """Copy file to remote""" import json import ubuntuone.couch.auth as auth import mimetypes if not remote_filename: remote_filename = source_path.get_filename() remote_full = self.meta_base + self.quote(remote_filename) answer = auth.request(remote_full, http_method="PUT", request_body='{"kind":"file"}') self.handle_error(raise_errors, 'put', answer, source_path.name, remote_full) node = json.loads(answer[1]) remote_full = self.content_base + self.quote(node.get('content_path')) data = bytearray(open(source_path.name, 'rb').read()) size = len(data) content_type = mimetypes.guess_type(source_path.name)[0] content_type = content_type or 'application/octet-stream' headers = {"Content-Length": str(size), "Content-Type": content_type} answer = auth.request(remote_full, http_method="PUT", headers=headers, request_body=data) self.handle_error(raise_errors, 'put', answer, source_path.name, remote_full)
def get_children(path): import json import ubuntuone.couch.auth as auth import urllib # Request children metadata base = "https://one.ubuntu.com/api/file_storage/v1/~/" url = base + urllib.quote(path) + "?include_children=true" answer = auth.request(url) # Create file list out of json data filelist = [] node = json.loads(answer[1]) if node.get('has_children') == True: for child in node.get('children'): child_path = urllib.unquote(child.get('path')).lstrip('/') filelist += [child_path] print filelist
def _query_file_info(self, filename, raise_errors=False): """Query attributes on filename""" import json import ubuntuone.couch.auth as auth from duplicity import log remote_full = self.meta_base + self.quote(filename) answer = auth.request(remote_full) code = self.parse_error(answer) if code is not None: if code == log.ErrorCode.backend_not_found: return {'size': -1} elif raise_errors: self.handle_error(raise_errors, 'query', answer, remote_full, filename) else: return {'size': None} node = json.loads(answer[1]) size = node.get('size') return {'size': size}
def delete(path): import ubuntuone.couch.auth as auth import urllib base = "https://one.ubuntu.com/api/file_storage/v1/~/" auth.request(base + urllib.quote(path), http_method="DELETE")
def create_volume(path): import ubuntuone.couch.auth as auth import urllib base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/" auth.request(base + urllib.quote(path), http_method="PUT")
def create_volume(self, raise_errors=False): import ubuntuone.couch.auth as auth answer = auth.request(self.volume_uri, http_method="PUT") self.handle_error(raise_errors, 'put', answer, self.volume_uri)