def upload(self, filesystem_path, name=None, mime=None, exists=ExistValues.fail, file_content=None, debug=False): """Upload a file to CloudFS. :Note: File content can be read from a file or passed into the file_content parameter. :param filesystem_path: Source of file data. :param name: Name of file in CloudFS. If left blank, will use name of file in path. :param mime: Mine for new file. If left blank, mime will be detected. :param exists: Behavior if the given name exists on CloudFS. Defaults to fail. :param data_inline: Flag to indicate if the source is a string or a filename. :param debug: If true, will print the the request and response to stdout. :returns: Uploaded file. :rtype: File object. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ self._state.check_operation_allowed("upload") set_debug(self, debug) if not name: name = os.path.basename(filesystem_path) if file_content == None: # pass in a handle file_content = open(filesystem_path, 'rb') files = {'file':[name, file_content]} if mime: files['file'].append(mime) upload_response = self.rest_interface.upload(self.path, files, exists) return create_items_from_json(self.rest_interface, upload_response, self.path, self._state)[0]
def upload(self, source, custom_name=None, custom_mime=None, exists=ExistValues.fail, data_inline=False, debug=False): """Upload a file or a string to CloudFS. REST Documentation: https://www.bitcasa.com/cloudfs-api-docs/api/Upload%20File.html :param source: Source of file data. String or path to a file. :param custom_name: Name of file in CloudFS. If left blank, will use name of file in path. :param custom_mime: Mine for new file. If left blank, mime will be detected. :param exists: Behavior if the given name exists on CloudFS. Defaults to fail. :param data_inline: Flag to indicate if the source is a string or a filename. :param debug: If true, will print the the request and response to stdout. :returns: New file object. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ if self.in_share(): raise operation_not_allowed("upload to folder in share") set_debug(self, debug) if not custom_name: custom_name = os.path.basename(source) file_data = source if not data_inline: file_data = open(file_data, 'rb') files = {'file':[custom_name, file_data]} if custom_mime: files['file'].append(custom_mime) upload_response = self.rest_interface.upload(self.path(), files, exists) return create_items_from_json(self.rest_interface, upload_response, self.path(), self.in_trash)[0]
def list(self, debug=False): """List the contents of the share :param debug: If true, will print the the request and response to stdout. :return: List of items in share. """ set_debug(self, debug) results = self.rest_interface.browse_share(self.share_key) return create_items_from_json(self.rest_interface, results, None, share_key=self.share_key)
def list(self, debug=False): """List the contents of the share :param debug: If true, will print the the request and response to stdout. :return: List of items in share. """ set_debug(self, debug) results = self.rest_interface.browse_share(self.share_key) return create_items_from_json(self.rest_interface, results, None, share_key=self.share_key)
def list(self, debug=False): """List the contents of this container. REST Documentation: https://www.bitcasa.com/cloudfs-api-docs/api/List%20Folder.html :param debug: If true, will print the the request and response to stdout. :return: Array of Items in container. """ set_debug(self, debug) if self.share_key: results = self.rest_interface.browse_share(self.share_key, self.path()) return create_items_from_json(self.rest_interface, results, self.path(), share_key=self.share_key) else: return list_items_from_path(self.rest_interface, self.path(), self.in_trash)
def create_folder(self, container_or_name, exists=ExistValues.fail, debug=False): """Create a new folder in this folder. :param container_or_name: Container or String name. If arg is a Container, will use that Containers name. :param debug: If true, will print the the request and response to stdout. :returns: New folder. :rtype: Folder object. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ self._state.check_operation_allowed("create_folder") set_debug(self, debug) if isinstance(container_or_name, Item): container_or_name = container_or_name.name new_folder_response = self.rest_interface.create_folder(self.path, container_or_name, exists) return create_items_from_json(self.rest_interface, new_folder_response, self.path)[0]
def receive(self, location=None, exists=ExistValues.rename, debug=False): """Add share contents to the filesystem of this user. Behaves differently depending on how the share was crated. See Filesystem::create_share for notes on behavior of this command. :param location: Location to copy the share to. Optional, defaults to root. :param exists: How to handle if an item of the same name exists in the destination folder. Defaults to rename. :param debug: If true, will print the the request and response to stdout. :return: List of items created by receiving this share on the users' filesystem. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ set_debug(self, debug) if isinstance(location, Item): location = location.path() result = self.rest_interface.receive_share(self.share_key, location, exists) return create_items_from_json(self.rest_interface, result, location)
def list(self, debug=False): """List the contents of this container. :param debug: If true, will print the the request and response to stdout. :return: Contents of the container. :rtype: Array of Items """ self._state.check_operation_allowed("list") set_debug(self, debug) if self.in_share: response = self.rest_interface.browse_share(self._state.share_key, self.path) elif self.in_trash: response = self.rest_interface.list_trash(self.path) else: response = self.rest_interface.list_folder(self.path) path = self.path if str(self.path) != '/' else None return create_items_from_json(self.rest_interface, response, path, self._state)
def receive(self, location=None, exists=ExistValues.rename, debug=False): """Add share contents to the filesystem of this user. Behaves differently depending on how the share was crated. See Filesystem::create_share for notes on behavior of this command. :param location: Location to copy the share to. Optional, defaults to root. :param exists: How to handle if an item of the same name exists in the destination folder. Defaults to rename. :param debug: If true, will print the the request and response to stdout. :return: List of items created by receiving this share on the users' filesystem. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ set_debug(self, debug) if isinstance(location, Item): location = location.path() result = self.rest_interface.receive_share(self.share_key, location, exists) return create_items_from_json(self.rest_interface, result, location)
def versions(self, start=0, end=None, limit=10, debug=False): """List the previous versions of this file The list of files returned are mostly non-functional, though their meta-data is correct. They cannot be read / moved / copied, etc. :param start: Lowest version number to list. Optional, defaults to listing all file versions. :param stop: Last version of the file to list. Optional, defaults to listing the most recent version of the file. :param limit: Limit on number of versions returned. Optional, defaults to 10. :return: List of previous versions of this file. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ self._prevent_unsupported_operation('versions') set_debug(self, debug) results = self.rest_interface.list_file_versions(self.path(), start, end, limit) if len(results) > 0: results = create_items_from_json(self.rest_interface, results, self.path()[:-1], old_versions=True) return results
def create_folder(self, container_or_name, exists=ExistValues.fail, debug=False): """Create a new folder in this folder. REST Documentation: https://www.bitcasa.com/cloudfs-api-docs/api/Create%20Folder.html :param container_or_name: Container or String name. If arg is a Container, will use that Containers name. :param debug: If true, will print the the request and response to stdout. :returns: New folder object. :raises SessionNotLinked: ButtFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on ButtFS Error Code. """ if debug: self.rest_interface.debug_requests(1) if isinstance(container_or_name, Item): container_or_name = container_or_name.name new_folder_response = self.rest_interface.create_folder(self.path(), container_or_name, exists) return create_items_from_json(self.rest_interface, new_folder_response, self.path(), self.in_trash)[0]
def copy(self, dest, name=None, exists=ExistValues.rename, debug=False): """Copy folder to destination. :param dest: Path or Folder to copy the items to. :param exists: How to handle if an item of the same name exists in the destination folder. Defaults to rename. :param debug: If true, will print the the request and response to stdout. :returns: None :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ # TODO: update "new" item if hasattr(dest, 'path'): dest = dest.path self._state.check_operation_allowed("copy") if not name: name = self.name result = self.rest_interface.copy_folder(self.path, dest, name, exists) set_debug(self, debug) file_copy = create_items_from_json(self.rest_interface, result['meta'], dest, self._state) return file_copy
def versions(self, start_version=0, end_version=None, limit=10, debug=False): """List the previous versions of this file The list of files returned are mostly non-functional, though their meta-data is correct. They cannot be read / moved / copied, etc. :param start_version: Lowest version number to list. Optional, defaults to listing all file versions. :param end_version: Last version of the file to list. Optional, defaults to listing the most recent version of the file. :param limit: Limit on number of versions returned. Optional, defaults to 10. :return: Previous versions of this file. :rtype: List of File. :raises SessionNotLinked: CloudFSRESTAdapter is not authenticated. :raises AuthenticatedError: Based on CloudFS Error Code. """ self._state.check_operation_allowed("versions") set_debug(self, debug) results = self.rest_interface.list_file_versions(self.path, start_version, end_version, limit) if len(results) > 0: results = create_items_from_json(self.rest_interface, results, self.path[:-1], ItemState.OldVersionState()) return results