Class S3FileAccessV2

java.lang.Object
dev.jcputney.elearning.parser.impl.access.AbstractS3FileAccess
dev.jcputney.elearning.parser.impl.access.S3FileAccessV2
All Implemented Interfaces:
FileAccess, AutoCloseable

public final class S3FileAccessV2 extends AbstractS3FileAccess
Optimized implementation of FileAccess using AWS S3 SDK v2 with batch operations, streaming support, and intelligent caching.
  • Constructor Details

    • S3FileAccessV2

      public S3FileAccessV2(software.amazon.awssdk.services.s3.S3Client s3Client, String bucketName, String rootPath)
      Constructs an optimized S3FileAccessV2 instance with the specified S3 client and bucket name.
      Parameters:
      s3Client - The S3 client to use for accessing files.
      bucketName - The name of the S3 bucket to access.
      rootPath - The root path of the S3 bucket to access.
    • S3FileAccessV2

      public S3FileAccessV2(software.amazon.awssdk.services.s3.S3Client s3Client, String bucketName, String rootPath, boolean eagerCache)
      Constructs an optimized S3FileAccessV2 instance with the specified S3 client and bucket name.
      Parameters:
      s3Client - The S3 client to use for accessing files.
      bucketName - The name of the S3 bucket to access.
      rootPath - The root path of the S3 bucket to access.
      eagerCache - Whether to eagerly cache all files on initialization.
  • Method Details

    • prepareForModule

      public void prepareForModule(String moduleRoot)
      Prepare this file access instance for interacting with a specific module root. Clears any cached state and optionally re-populates caches when eager caching is enabled.
      Parameters:
      moduleRoot - The module prefix/key to scope subsequent operations to.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path) throws IOException
      Description copied from class: AbstractS3FileAccess
      Gets the contents of a file as an InputStream with intelligent streaming/caching.
      Specified by:
      getFileContentsInternal in interface FileAccess
      Overrides:
      getFileContentsInternal in class AbstractS3FileAccess
      Parameters:
      path - The path of the file to get contents from (guaranteed to be non-null).
      Returns:
      An InputStream containing the file contents.
      Throws:
      IOException - if the file can't be read.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path, StreamingProgressListener progressListener) throws IOException
      Gets the contents of a file as an InputStream with optional progress tracking.
      Parameters:
      path - The path of the file to get contents from (guaranteed to be non-null).
      progressListener - Optional progress listener for tracking large file operations.
      Returns:
      An InputStream containing the file contents.
      Throws:
      IOException - If an error occurs while getting file contents.
    • getInputStreamWrapper

      protected InputStream getInputStreamWrapper(InputStream stream, long fileSize)
      Wraps the provided InputStream to add optional progress tracking functionality if a StreamingProgressListener is available in the thread-local context. If no listener is set, the original InputStream is returned unmodified.
      Specified by:
      getInputStreamWrapper in class AbstractS3FileAccess
      Parameters:
      stream - The InputStream to be wrapped or returned as-is if no progress listener is found.
      fileSize - The size of the file being read, in bytes, or -1 if unknown.
      Returns:
      An InputStream that may provide progress tracking via a wrapped implementation, or the original InputStream if no listener is available.
    • checkFileExistsOnS3

      protected boolean checkFileExistsOnS3(String path)
      Checks if a file exists in the S3 bucket at the specified path.
      Specified by:
      checkFileExistsOnS3 in class AbstractS3FileAccess
      Parameters:
      path - The relative path of the file to check within the S3 bucket.
      Returns:
      true if the file exists on S3, otherwise false.
    • getFileSizeOnS3

      protected long getFileSizeOnS3(String path)
      Retrieves the file size of a specified file stored on S3.
      Specified by:
      getFileSizeOnS3 in class AbstractS3FileAccess
      Parameters:
      path - The relative path of the file within the S3 bucket.
      Returns:
      The size of the file in bytes if found, or 0 if the file does not exist or an error occurs.
    • listFilesOnS3

      protected List<String> listFilesOnS3(String directoryPath)
      Lists all files in a specified directory within an S3 bucket. The method recursively retrieves all file keys under the specified directory path, filtering out directory markers or irrelevant paths.
      Specified by:
      listFilesOnS3 in class AbstractS3FileAccess
      Parameters:
      directoryPath - The relative path of the directory within the S3 bucket to list files from.
      Returns:
      A list of file keys representing the files in the specified directory. If an error occurs during the operation, an empty list is returned.
    • getS3ObjectAsBytes

      protected byte[] getS3ObjectAsBytes(String fullPath) throws IOException
      Retrieves an object stored in S3 as a byte array.
      Specified by:
      getS3ObjectAsBytes in class AbstractS3FileAccess
      Parameters:
      fullPath - The full path of the object in the S3 bucket.
      Returns:
      A byte array containing the data of the specified object from S3.
      Throws:
      IOException - If an error occurs while fetching the object from S3.
    • getS3ObjectStream

      protected InputStream getS3ObjectStream(String fullPath) throws IOException
      Retrieves the InputStream of an object stored in S3 at the specified full path.
      Specified by:
      getS3ObjectStream in class AbstractS3FileAccess
      Parameters:
      fullPath - The full path of the object in the S3 bucket, including directories and file name.
      Returns:
      An InputStream allowing access to the content of the object stored in S3.
      Throws:
      IOException - If an error occurs while accessing the object in S3.
    • detectInternalRootDirectory

      protected String detectInternalRootDirectory(String rootPath)
      Detects the internal root directory from a given root path by querying the S3 bucket and inspecting the common prefixes. If exactly one common prefix is found, it is returned as the internal root directory; otherwise, the provided rootPath is returned. This method is used to resolve a potential internal hierarchy structure within the provided root path.
      Specified by:
      detectInternalRootDirectory in class AbstractS3FileAccess
      Parameters:
      rootPath - The initial root path within the S3 bucket to detect the internal root directory from.
      Returns:
      The detected internal root directory based on the common prefixes in the S3 bucket. If detection fails or no single common prefix is found, the original rootPath is returned.