code: def tar_and_upload_dir(
      session,
      bucket,
      s3_key_prefix,
      script,
      directory=None,
      dependencies=None,
      kms_key=None,
      s3_resource=None,
      settings: Optional[SessionSettings] = None,
  ) -> UploadedCode:
      """Package source files and upload a compress tar file to S3.
  
      The S3 location will be ``s3://<bucket>/s3_key_prefix/sourcedir.tar.gz``.
      If directory is an S3 URI, an UploadedCode object will be returned, but
      nothing will be uploaded to S3 (this allow reuse of code already in S3).
      If directory is None, the script will be added to the archive at
      ``./<basename of script>``. If directory is not None, the (recursive) contents
      of the directory will be added to the archive. directory is treated as the base
      path of the archive, and the script name is assumed to be a filename or relative path
      inside the directory.
  
      Args:
          session (boto3.Session): Boto session used to access S3.
          bucket (str): S3 bucket to which the compressed file is uploaded.
          s3_key_prefix (str): Prefix for the S3 key.
          script (str): Script filename or path.
          directory (str): Optional. Directory containing the source file. If it
              starts with "s3://", no action is taken.
          dependencies (List[str]): Optional. A list of paths to directories
              (absolute or relative) containing additional libraries that will be
              copied into /opt/ml/lib
          kms_key (str): Optional. KMS key ID used to upload objects to the bucket
              (default: None).
          s3_resource (boto3.resource("s3")): Optional. Pre-instantiated Boto3 Resource
              for S3 connections, can be used to customize the configuration,
              e.g. set the endpoint URL (default: None).
          settings (sagemaker.session_settings.SessionSettings): Optional. The settings
              of the SageMaker ``Session``, can be used to override the default encryption
              behavior (default: None).
      Returns:
          sagemaker.fw_utils.UploadedCode: An object with the S3 bucket and key (S3 prefix) and
              script name.
      """
      if directory and (
          is_pipeline_variable(directory)
          or directory.lower().startswith("s3://")
      ):
          return UploadedCode(s3_prefix=directory, script_name=script)
  
      script_name = script if directory else os.path.basename(script)
      dependencies = dependencies or []
      key = "%s/sourcedir.tar.gz" % s3_key_prefix
      if (
          settings is not None
          and settings.local_download_dir is not None
          and not (
              os.path.exists(settings.local_download_dir)
              and os.path.isdir(settings.local_download_dir)
          )
      ):
          raise ValueError(
              "Inputted directory for storing newly generated temporary directory does "
              f"not exist: '{settings.local_download_dir}'"
          )
      local_download_dir = (
          None if settings is None else settings.local_download_dir
      )
      tmp = tempfile.mkdtemp(dir=local_download_dir)
      encrypt_artifact = (
          True
          if settings is None
          else settings.encrypt_repacked_artifacts
      )
  
      try:
          source_files = (
              _list_files_to_compress(script, directory) + dependencies
          )
          tar_file = sagemaker.utils.create_tar_file(
              source_files, os.path.join(tmp, _TAR_SOURCE_FILENAME)
          )
  
          if kms_key:
              extra_args = {
                  "ServerSideEncryption": "aws:kms",
                  "SSEKMSKeyId": kms_key,
              }
          elif encrypt_artifact:
              # encrypt the tarball at rest in S3 with the default AWS managed KMS key for S3
              # see https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html#API_PutObject_RequestSyntax
              extra_args = {"ServerSideEncryption": "aws:kms"}
          else:
              extra_args = None
  
          if s3_resource is None:
              s3_resource = session.resource(
                  "s3", region_name=session.region_name
              )
          else:
              logger.debug("Using provided s3_resource")
  
          s3_resource.Object(bucket, key).upload_file(
              tar_file, ExtraArgs=extra_args
          )
      finally:
          shutil.rmtree(tmp)
  
      return UploadedCode(
          s3_prefix="s3://%s/%s" % (bucket, key),
          script_name=script_name,
      )
  
imports: from __future__ import absolute_import
  import json
  import logging
  import os
  import re
  import time
  import shutil
  import tempfile
  from collections import namedtuple
  from typing import List, Optional, Union, Dict
  from packaging import version
  import sagemaker.image_uris
  from sagemaker.instance_group import InstanceGroup
  from sagemaker.s3_utils import s3_path_join
  from sagemaker.session_settings import SessionSettings
  import sagemaker.utils
  from sagemaker.workflow import is_pipeline_variable
  from sagemaker.deprecations import renamed_warning, renamed_kwargs
  from sagemaker.workflow.entities import PipelineVariable
  from sagemaker.deprecations import deprecation_warn_base
label_desc: Use image node_id 6 for calling a global function with example usage: tar_and_upload_dir(session, bucket, s3_key_prefix, script, directory, dependencies, kms_key, s3_resource, settings) and returns: UploadedCode, UploadedCode