code: def download_folder(bucket_name, prefix, target, sagemaker_session):
      
      boto_session = sagemaker_session.boto_session
  
      s3 = boto_session.resource('s3')
      bucket = s3.Bucket(bucket_name)
  
      prefix = prefix.lstrip('/')
  
      # there is a chance that the prefix points to a file and not a 'directory' if that is the case
      # we should just download it.
      objects = list(bucket.objects.filter(Prefix=prefix))
  
      if len(objects) > 0 and objects[0].key == prefix and prefix[-1] != '/':
          s3.Object(bucket_name, prefix).download_file(os.path.join(target, os.path.basename(prefix)))
          return
  
      # the prefix points to an s3 'directory' download the whole thing
      for obj_sum in bucket.objects.filter(Prefix=prefix):
          # if obj_sum is a folder object skip it.
          if obj_sum.key != '' and obj_sum.key[-1] == '/':
              continue
          obj = s3.Object(obj_sum.bucket_name, obj_sum.key)
          s3_relative_path = obj_sum.key[len(prefix):].lstrip('/')
          file_path = os.path.join(target, s3_relative_path)
  
          try:
              os.makedirs(os.path.dirname(file_path))
          except OSError as exc:
              # EEXIST means the folder already exists, this is safe to skip
              # anything else will be raised.
              if exc.errno != errno.EEXIST:
                  raise
          obj.download_file(file_path)
docs: Download a folder from S3 to a local path
  
      Args:
          bucket_name (str): S3 bucket name
          prefix (str): S3 prefix within the bucket that will be downloaded. Can be a single file.
          target (str): destination path where the downloaded items will be placed
          sagemaker_session (:class:`sagemaker.session.Session`): a sagemaker session to interact with S3.