def test_is_folder(): setup_test_files() assert is_folder(f'{test_folder_path}/working') is True assert is_folder(f'{test_folder_path}/DoesNotExist') is False teardown_test_files()
def create(self, resource): """Create a container within resource's storage account.""" self._load_resource(resource) blob_folder = self._blob_folder() if is_folder(blob_folder): logger.warning(self._context("Container already exists")) else: # create new container logger.info(self._context("Creating container")) create_folder(blob_folder) self.disconnect() return is_folder(blob_folder)
def connect(self, resource): """Connect to container within resource's storage account.""" self._load_resource(resource) blob_folder = self._blob_folder() # make sure folder exists if is_folder(blob_folder): logger.debug(self._context("Connecting to container")) else: logger.warning(self._context("Container does not exist")) self.disconnect() return self.resource is not None
def remove(self, resource): """Remove a container from resource's storage account.""" self._load_resource(resource) blob_folder = self._blob_folder() if not is_folder(blob_folder): logger.warning(self._context(f"Container does not exist")) is_success = False else: logger.info(self._context(f"Removing container")) delete_folder(blob_folder) is_success = True self.disconnect() return is_success
def list(self, glob_pattern=""): """ Return a sorted list of blob names (and blob folder names) based on optional glob_pattern. Blob folder refers to the concept of logical folders which are implemented via slash delimited prefixes. Behavior (blob names refers to both blobs and blob folder names) - glob_pattern == '': return blob names in container's root - glob_pattern == <folder> or <folder>/: return blob names in specified folder - glob_pattern == <specific-blob-name>: return specific blob name - glob_pattern == <glob_pattern>: returns blob names matching glob_pattern """ # strip relative path so we don't step outside our emulated storage area glob_pattern = force_local_path(glob_pattern) # analyze glob_pattern to determine how to return blob names # if glob_pattern is a folder if not glob_pattern: # default to all blobs at the root level glob_pattern = "*" elif is_folder(f"{self._blob_folder()}/{glob_pattern}"): # if glob_pattern is a folder, return all blobs within folder glob_pattern = f"{force_trailing_slash(glob_pattern)}*" else: # use glob_pattern as-is pass # retrieve sorted blob names target_path = f"{self._blob_folder()}/{glob_pattern}" # build list of blob names with local parent path stripped from names blob_names = list() for blob_name in sorted(glob.glob(target_path)): # format name using Linux path delimiters blob_name = blob_name.replace(chr(92), "/") blob_name = blob_name.replace(f"{self._blob_folder()}/", "") blob_names.append(blob_name) blob_count = len(blob_names) logger.debug( self._context(f"list({glob_pattern}) returned {blob_count} blob names") ) logger.debug(self._context(f"list({glob_pattern}) = {blob_names}")) return blob_names