code: def setup_storage(storage=None, debug=False):
      """Create the storage instance from a configuration.
  
      Parameters
      ----------
      config: dict, optional
          Configuration for the storage backend. If not defined, global configuration
          is used.
      debug: bool, optional
          If using in debug mode, the storage config is overridden with legacy:EphemeralDB.
          Defaults to False.
  
      """
      if storage is None:
          storage = orion.core.config.storage.to_dict()
  
      storage = copy.deepcopy(storage)
  
      if storage.get("type") == "legacy" and "database" not in storage:
          storage[
              "database"
          ] = orion.core.config.storage.database.to_dict()
      elif storage.get("type") is None and "database" in storage:
          storage["type"] = "legacy"
  
      # If using same storage type
      if storage["type"] == orion.core.config.storage.type:
          storage = resolve_config.merge_configs(
              orion.core.config.storage.to_dict(), storage
          )
  
      if debug:
          storage = {
              "type": "legacy",
              "database": {"type": "EphemeralDB"},
          }
  
      storage_type = storage.pop("type")
  
      log.debug(
          "Creating %s storage client with args: %s",
          storage_type,
          storage,
      )
      try:
          return storage_factory.create(of_type=storage_type, **storage)
      except ValueError:
          if (
              storage_factory.create().__class__.__name__.lower()
              != storage_type.lower()
          ):
              raise
  
imports: from __future__ import annotations
  import contextlib
  import copy
  import logging
  import typing
  from datetime import datetime
  from typing import Literal
  import orion.core
  from orion.core.io import resolve_config
  from orion.core.utils import GenericFactory
label_desc: Use image node_id 3 for calling a global function with example usage: setup_storage(storage, debug) and returns: storage_factory