def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) print "Fuse Init" # Set-Up Logger self.bcfslog = logging.getLogger(__name__) self.bcfslog.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler( address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_LOCAL6) formatter = logging.Formatter( 'Thead:%(thread)d(%(threadName)s) Function:%(module)s.%(funcName)s: %(message)s' ) handler.setFormatter(formatter) self.bcfslog.addHandler(handler) # Python FUSE Options self.bitcasa = Bitcasa('config.json', self.bcfslog) if (self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") print "Authenticated fine" # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {}
def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) # Python FUSE Options self.bitcasa = Bitcasa('config.json') if(self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {}
def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) print "Fuse Init" # Set-Up Logger self.bcfslog = logging.getLogger(__name__) self.bcfslog.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler(address = '/dev/log', facility=logging.handlers.SysLogHandler.LOG_LOCAL6) formatter = logging.Formatter('Thead:%(thread)d(%(threadName)s) Function:%(module)s.%(funcName)s: %(message)s') handler.setFormatter(formatter) self.bcfslog.addHandler(handler) # Python FUSE Options self.bitcasa = Bitcasa('config.json', self.bcfslog) if(self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") print "Authenticated fine" # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {}
# Test Client for Bitcasa Python SDK from bitcasa import Bitcasa # Start Client client = Bitcasa('config.json') print("Bitcasa has been authorized with your account.") print("### Folder List (/) ###") folder_info = client.list_folder('/') folder_path = folder_info[0]['path'] print(folder_info) print(folder_path) print("### Adding Folder (to root)###") print(client.add_folder(folder_path, "Test"))
# Bitcasa Authentication Example # # 2013 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True) # Get our URL print("Bitcasa OAuth URL: " + client.oauth_url()) # Get Auth Code print("Now, go to the URL above in a Web Browser and Enter the Authorization Code Below") auth_code = input("Authorization Code: ") # Authenticate print("Your access token: " + client.authenticate(auth_code))
class BitcasaFS(fuse.Fuse): def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) print "Fuse Init" # Set-Up Logger self.bcfslog = logging.getLogger(__name__) self.bcfslog.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler(address = '/dev/log', facility=logging.handlers.SysLogHandler.LOG_LOCAL6) formatter = logging.Formatter('Thead:%(thread)d(%(threadName)s) Function:%(module)s.%(funcName)s: %(message)s') handler.setFormatter(formatter) self.bcfslog.addHandler(handler) # Python FUSE Options self.bitcasa = Bitcasa('config.json', self.bcfslog) if(self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") print "Authenticated fine" # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {} def getattr(self, path): print 'called getattr:', path if (path == '/'): t = MyStat(); t.st_atime = int(time.time()) t.st_mtime = t.st_atime t.st_ctime = t.st_atime return t # Else pass File/Folder Object # mkdir Check elif(path.split('/')[-1] in self.dir): return BitcasaStat(self.dir[path.split('/')[-1]]) else: # DNE return -errno.ENOENT # Directory Methods def readdir(self, path, offset): # Turn English into Bitcasa Base64 # Root Path - This clears our breadcrumbs @ preps if(path == "/"): # Get Files/Folders for Root bdir = self.bitcasa.list_folder(path) # Clear Breadcrumbs self.breadcrumbs = { } # Add Root Breadcrumb self.breadcrumbs['/'] = { 'Name':'', 'Path':'/', 'Type':'folders', 'mtime':'0'} # Reset Path self.bpath = "" else: # Load next round of Files/Folders from Bitcasa bdir = self.bitcasa.list_folder(self.dir[path.split('/')[-1]]['Path']) # Add our new Breadcrumb # TODO - add logic to check and see if we have this breadcrumb already. self.breadcrumbs[path.split('/')[-1]] = self.dir[path.split('/')[-1]] # Current Bitcasa Path self.bpath = self.dir[path.split('/')[-1]]['Path'] print(self.bpath) for b in bdir: # Get Extra File Stuff if(b['category'] == 'folders'): item = { 'Name':b['name'].encode('utf-8'), 'Path':b['path'].encode('utf-8'), 'Type':b['category'], 'mtime':b['mtime'] } else: item = { 'Name':b['name'].encode('utf-8'), 'Path':b['path'].encode('utf-8'), 'Type':b['category'], 'mtime':b['mtime'], 'ID':b['id'].encode('utf-8'), 'Size':b['size'] } self.dir[b['name'].encode('utf-8')] = item # Now yield to FUSE yield fuse.Direntry(b['name'].encode('utf-8')) #def mkdir(self, path, mode): # result = self.bitcasa.add_folder(self.bpath, path.split('/')[-1]) # if(result['error'] == None): # new = result['result']['items'][0] # item = { 'Name':new['name'].encode('utf-8'), 'Path':new['path'].encode('utf-8'), 'Type':new['category'], 'mtime':new['mtime'] } # self.dir[new['name'].encode('utf-8')] = item # return 0 # else: # return -errno.ENOSYS # WIP - Doesn't report "file not found" #def rmdir(self, path): # print("Removing " + path + ";CurrentPath: " + self.bpath) # result = self.bitcasa.delete_folder(self.dir[path.split('/')[-1]]['Path']) # if(result['error'] == None): # return 0 # else: # return -errno.ENOSYS # File Methods def open(self, path, flags): #pprint.pprint(self.dir) #print "Trying to open: ", path + "/" + self.dir[path.split('/')[-1]]['ID'] print "OPEN started: filename is ", self.dir[path.split('/')[-1]]['Name']+" Client Pid:"+str(self.GetContext()['uid']) download_url = self.bitcasa.download_file_url(self.dir[path.split('/')[-1]]['ID'], self.dir[path.split('/')[-1]]['Path'], self.dir[path.split('/')[-1]]['Name'], self.dir[path.split('/')[-1]]['Size']) self.dir[path.split('/')[-1]]['DownloadURL'] = download_url #temp_file = self.bitcasa.download_file(self.dir[path.split('/')[-1]]['ID'], self.dir[path.split('/')[-1]]['Path'], self.dir[path.split('/')[-1]]['Name'], self.dir[path.split('/')[-1]]['Size']) #temp_file = self.bitcasa.cache_dir + "/" + self.dir[path.split('/')[-1]]['Name'] if download_url != None: return None else: return -errno.EACCESS # Read using streaming def read(self, path, size, offset, fh=None): self.bcfslog.debug("READ started: "+path+" offset:"+str(offset)+" size:"+str(size)) return self.bitcasa.download_file_part(self.dir[path.split('/')[-1]]['DownloadURL'], offset, size, self.dir[path.split('/')[-1]]['Size'], str(self.GetContext()['uid'])) # Release the file after reading is done def release(self, path, flags, fh=None): #ToDo flush aheadBuffer looking for keys with the client pid self.bcfslog.debug("release file: "+str(path)+", buffer still allocated:"+str(len(self.bitcasa.aheadBuffer))+" Client Pid:"+str(self.GetContext()['uid'])) def flush(self, path, fh=None): self.bcfslog.debug("flush: "+str(path)+", buffer still allocated:"+str(len(self.bitcasa.aheadBuffer))+" Client Pid:"+str(self.GetContext()['uid'])) return 0 def fsdestroy(self): self.bcfslog.debug("destroy") self.bitcasa.pool.shutdown() self.bitcasa.httpd_thread.stop()
# Bitcasa User Profile Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get & Print User Profile print(client.user_profile());
# Test Client for Bitcasa Python SDK from bitcasa import Bitcasa # Start Client client = Bitcasa('config.json') print("Bitcasa has been authorized with your account.") print("### Folder List (/) ###") print(client.list_folder('/')) print("### Adding Folder ###") print(client.add_folder("Test", "/VeKqFJiJSbqdoV5XFwbxxg"))
# Bitcasa List Directory Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results for files in root_dir: print(files['name'] + " - " + files['path'])
# Bitcasa Delete Directory Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results i = 0 for files in root_dir: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get Some User Input directory = input("Enter # of Folder to Enter: ") dir_contents = client.dir(root_dir[int(directory)]['path']) # Enter Directory & Print Results i = 0 for files in dir_contents: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get More User Input delete_dir = input("Enter # of Folder to Delete: ")
# Bitcasa Download File Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results i = 0 for files in root_dir: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get Some User Input directory = input("Enter # of Folder to Enter: ") dir_contents = client.dir(root_dir[int(directory)]['path']) # Enter Directory & Print Results i = 0 for files in dir_contents: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get More User Input file_num = input("Enter # of File to Download: ") new_name = input("Input Filename to Save File As: ")
class BitcasaFS(fuse.Fuse): def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) # Python FUSE Options self.bitcasa = Bitcasa('config.json') if(self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {} def getattr(self, path): print 'called getattr:', path if (path == '/'): t = [0,]*10 t[0] = 0755 t[3] = 2; t[6] = 2048 return t # Else pass File/Folder Object # mkdir Check elif(path.split('/')[-1] in self.dir): return BitcasaStat(self.dir[path.split('/')[-1]]) else: # DNE return -errno.ENOENT # Directory Methods def readdir(self, path, offset): # Turn English into Bitcasa Base64 # Root Path - This clears our breadcrumbs @ preps if(path == "/"): # Get Files/Folders for Root bdir = self.bitcasa.list_folder(path) # Clear Breadcrumbs self.breadcrumbs = { } # Add Root Breadcrumb self.breadcrumbs['/'] = { 'Name':'', 'Path':'/', 'Type':'folders', 'mtime':'0'} # Reset Path self.bpath = "" else: # Load next round of Files/Folders from Bitcasa bdir = self.bitcasa.list_folder(self.dir[path.split('/')[-1]]['Path']) # Add our new Breadcrumb # TODO - add logic to check and see if we have this breadcrumb already. self.breadcrumbs[path.split('/')[-1]] = self.dir[path.split('/')[-1]] # Current Bitcasa Path self.bpath = self.dir[path.split('/')[-1]]['Path'] print(self.bpath) for b in bdir: # Get Extra File Stuff if(b['category'] == 'folders'): item = { 'Name':b['name'].encode('utf-8'), 'Path':b['path'].encode('utf-8'), 'Type':b['category'], 'mtime':b['mtime'] } else: item = { 'Name':b['name'].encode('utf-8'), 'Path':b['path'].encode('utf-8'), 'Type':b['category'], 'mtime':b['mtime'], 'ID':b['id'].encode('utf-8'), 'Size':b['size'] } self.dir[b['name'].encode('utf-8')] = item # Now yield to FUSE yield fuse.Direntry(b['name'].encode('utf-8')) def mkdir(self, path, mode): result = self.bitcasa.add_folder(self.bpath, path.split('/')[-1]) if(result['error'] == None): new = result['result']['items'][0] item = { 'Name':new['name'].encode('utf-8'), 'Path':new['path'].encode('utf-8'), 'Type':new['category'], 'mtime':new['mtime'] } self.dir[new['name'].encode('utf-8')] = item return 0 else: return -errno.ENOSYS # WIP - Doesn't report "file not found" def rmdir(self, path): print("Removing " + path + ";CurrentPath: " + self.bpath) result = self.bitcasa.delete_folder(self.dir[path.split('/')[-1]]['Path']) if(result['error'] == None): return 0 else: return -errno.ENOSYS # File Methods def open(self, path, flags): print "Trying to open: ", path + "/" + self.dir[path.split('/')[-1]]['ID'] print "Filename is: ", self.dir[path.split('/')[-1]]['Name'] temp_file = self.bitcasa.download_file(self.dir[path.split('/')[-1]]['ID'], self.dir[path.split('/')[-1]]['Path'], self.dir[path.split('/')[-1]]['Name'], self.dir[path.split('/')[-1]]['Size']) if temp_file != None: return open(temp_file, "rb") else: return -errno.EACCESS # Read from Cached File # @todo - Consider adding "streaming" mode, we could do it. def read(self, path, size, offset, fh): fh.seek(offset) return fh.read(size)
# Bitcasa Rename Directory Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results i = 0 for files in root_dir: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get Some User Input directory = input("Enter # of Folder to Enter: ") dir_contents = client.dir(root_dir[int(directory)]['path']) # Enter Directory & Print Results i = 0 for files in dir_contents: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get More User Input rename_dir = input("Enter # of Folder to Rename: ") new_name = input("Input New Folder Name: ")
# Bitcasa Add Directory Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results i = 0 for files in root_dir: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get Some User Input directory = input("Enter Folder number to add New Folder to: ") folder_name = input("Enter Name for new Folder: ") # Add Directory print(client.mkdir(root_dir[int(directory)]['path'], folder_name))
# Bitcasa Upload File Example # # 2014 Michael Thomas (Biscuit Labs) # # Get our Bitcasa Module from bitcasa import Bitcasa # Create an instance of the Bitcasa Class client = Bitcasa('APP_CLIENT_ID','APP_CLIENT_SECRET',True, None, 'ACCESS_TOKEN') # Get Root Directory Contents root_dir = client.dir(); # Print Results i = 0 for files in root_dir: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get Some User Input directory = input("Enter # of Folder to Enter: ") dir_contents = client.dir(root_dir[int(directory)]['path']) # Enter Directory & Print Results i = 0 for files in dir_contents: print("["+str(i)+"] "+files['name'] + " - " + files['path']) i = i + 1 # Get More User Input file_num = input("Enter # of Folder to Upload File To: ") new_name = input("Enter Path of File to Upload: ")
class BitcasaFS(fuse.Fuse): def __init__(self, *args, **kw): fuse.Fuse.__init__(self, *args, **kw) print "Fuse Init" # Set-Up Logger self.bcfslog = logging.getLogger(__name__) self.bcfslog.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler( address='/dev/log', facility=logging.handlers.SysLogHandler.LOG_LOCAL6) formatter = logging.Formatter( 'Thead:%(thread)d(%(threadName)s) Function:%(module)s.%(funcName)s: %(message)s' ) handler.setFormatter(formatter) self.bcfslog.addHandler(handler) # Python FUSE Options self.bitcasa = Bitcasa('config.json', self.bcfslog) if (self.bitcasa == None): sys.exit("Failed to authenticate Bitcasa Client.") print "Authenticated fine" # Bitcasa Encoded Path (for things like rename/create/delete) self.bpath = "" # Breadcrumbs to how we got where we are. self.breadcrumbs = {} # Files/Folders in Current Path self.dir = {} def getattr(self, path): print 'called getattr:', path if (path == '/'): t = MyStat() t.st_atime = int(time.time()) t.st_mtime = t.st_atime t.st_ctime = t.st_atime return t # Else pass File/Folder Object # mkdir Check elif (path.split('/')[-1] in self.dir): return BitcasaStat(self.dir[path.split('/')[-1]]) else: # DNE return -errno.ENOENT # Directory Methods def readdir(self, path, offset): # Turn English into Bitcasa Base64 # Root Path - This clears our breadcrumbs @ preps if (path == "/"): # Get Files/Folders for Root bdir = self.bitcasa.list_folder(path) # Clear Breadcrumbs self.breadcrumbs = {} # Add Root Breadcrumb self.breadcrumbs['/'] = { 'Name': '', 'Path': '/', 'Type': 'folders', 'mtime': '0' } # Reset Path self.bpath = "" else: # Load next round of Files/Folders from Bitcasa bdir = self.bitcasa.list_folder( self.dir[path.split('/')[-1]]['Path']) # Add our new Breadcrumb # TODO - add logic to check and see if we have this breadcrumb already. self.breadcrumbs[path.split('/')[-1]] = self.dir[path.split('/') [-1]] # Current Bitcasa Path self.bpath = self.dir[path.split('/')[-1]]['Path'] print(self.bpath) for b in bdir: # Get Extra File Stuff if (b['category'] == 'folders'): item = { 'Name': b['name'].encode('utf-8'), 'Path': b['path'].encode('utf-8'), 'Type': b['category'], 'mtime': b['mtime'] } else: item = { 'Name': b['name'].encode('utf-8'), 'Path': b['path'].encode('utf-8'), 'Type': b['category'], 'mtime': b['mtime'], 'ID': b['id'].encode('utf-8'), 'Size': b['size'] } self.dir[b['name'].encode('utf-8')] = item # Now yield to FUSE yield fuse.Direntry(b['name'].encode('utf-8')) #def mkdir(self, path, mode): # result = self.bitcasa.add_folder(self.bpath, path.split('/')[-1]) # if(result['error'] == None): # new = result['result']['items'][0] # item = { 'Name':new['name'].encode('utf-8'), 'Path':new['path'].encode('utf-8'), 'Type':new['category'], 'mtime':new['mtime'] } # self.dir[new['name'].encode('utf-8')] = item # return 0 # else: # return -errno.ENOSYS # WIP - Doesn't report "file not found" #def rmdir(self, path): # print("Removing " + path + ";CurrentPath: " + self.bpath) # result = self.bitcasa.delete_folder(self.dir[path.split('/')[-1]]['Path']) # if(result['error'] == None): # return 0 # else: # return -errno.ENOSYS # File Methods def open(self, path, flags): #pprint.pprint(self.dir) #print "Trying to open: ", path + "/" + self.dir[path.split('/')[-1]]['ID'] print "OPEN started: filename is ", self.dir[path.split( '/')[-1]]['Name'] + " Client Pid:" + str(self.GetContext()['uid']) download_url = self.bitcasa.download_file_url( self.dir[path.split('/')[-1]]['ID'], self.dir[path.split('/')[-1]]['Path'], self.dir[path.split('/')[-1]]['Name'], self.dir[path.split('/')[-1]]['Size']) self.dir[path.split('/')[-1]]['DownloadURL'] = download_url #temp_file = self.bitcasa.download_file(self.dir[path.split('/')[-1]]['ID'], self.dir[path.split('/')[-1]]['Path'], self.dir[path.split('/')[-1]]['Name'], self.dir[path.split('/')[-1]]['Size']) #temp_file = self.bitcasa.cache_dir + "/" + self.dir[path.split('/')[-1]]['Name'] if download_url != None: return None else: return -errno.EACCESS # Read using streaming def read(self, path, size, offset, fh=None): self.bcfslog.debug("READ started: " + path + " offset:" + str(offset) + " size:" + str(size)) return self.bitcasa.download_file_part( self.dir[path.split('/')[-1]]['DownloadURL'], offset, size, self.dir[path.split('/')[-1]]['Size'], str(self.GetContext()['uid'])) # Release the file after reading is done def release(self, path, flags, fh=None): #ToDo flush aheadBuffer looking for keys with the client pid self.bcfslog.debug("release file: " + str(path) + ", buffer still allocated:" + str(len(self.bitcasa.aheadBuffer)) + " Client Pid:" + str(self.GetContext()['uid'])) def flush(self, path, fh=None): self.bcfslog.debug("flush: " + str(path) + ", buffer still allocated:" + str(len(self.bitcasa.aheadBuffer)) + " Client Pid:" + str(self.GetContext()['uid'])) return 0 def fsdestroy(self): self.bcfslog.debug("destroy") self.bitcasa.pool.shutdown() self.bitcasa.httpd_thread.stop()