Class S3FileAccessV2
java.lang.Object
dev.jcputney.elearning.parser.impl.access.AbstractS3FileAccess
dev.jcputney.elearning.parser.impl.access.S3FileAccessV2
- All Implemented Interfaces:
FileAccess,AutoCloseable
Optimized implementation of FileAccess using AWS S3 SDK v2 with batch operations, streaming
support, and intelligent caching.
-
Field Summary
Fields inherited from class dev.jcputney.elearning.parser.impl.access.AbstractS3FileAccess
allFilesCache, bucketName, COMMON_MODULE_FILES, directoryListCache, executorService, fileExistsCache, fileSizeCache, MAX_CACHE_SIZE, rootPath, smallFileCache, STREAMING_THRESHOLD -
Constructor Summary
ConstructorsConstructorDescriptionS3FileAccessV2(software.amazon.awssdk.services.s3.S3Client s3Client, String bucketName, String rootPath) Constructs an optimized S3FileAccessV2 instance with the specified S3 client and bucket name.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. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckFileExistsOnS3(String path) Checks if a file exists in the S3 bucket at the specified path.protected StringdetectInternalRootDirectory(String rootPath) Detects the internal root directory from a given root path by querying the S3 bucket and inspecting the common prefixes.Gets the contents of a file as an InputStream with intelligent streaming/caching.getFileContentsInternal(String path, StreamingProgressListener progressListener) Gets the contents of a file as an InputStream with optional progress tracking.protected longgetFileSizeOnS3(String path) Retrieves the file size of a specified file stored on S3.protected InputStreamgetInputStreamWrapper(InputStream stream, long fileSize) Wraps the provided InputStream to add optional progress tracking functionality if a StreamingProgressListener is available in the thread-local context.protected byte[]getS3ObjectAsBytes(String fullPath) Retrieves an object stored in S3 as a byte array.protected InputStreamgetS3ObjectStream(String fullPath) Retrieves the InputStream of an object stored in S3 at the specified full path.listFilesOnS3(String directoryPath) Lists all files in a specified directory within an S3 bucket.voidprepareForModule(String moduleRoot) Prepare this file access instance for interacting with a specific module root.Methods inherited from class dev.jcputney.elearning.parser.impl.access.AbstractS3FileAccess
clearCaches, close, fileExistsBatch, fileExistsInternal, fullPath, getAllFiles, getCachedFileSize, getCacheStats, getFileContentsBase, getInternalRootDirectory, getRootPath, getTotalSize, listFilesInternal, prefetchCommonFiles, reconfigureRootPath, shutdownMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.jcputney.elearning.parser.api.FileAccess
fileExists, getFileContents, listFiles
-
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
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
Description copied from class:AbstractS3FileAccessGets the contents of a file as an InputStream with intelligent streaming/caching.- Specified by:
getFileContentsInternalin interfaceFileAccess- Overrides:
getFileContentsInternalin classAbstractS3FileAccess- 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
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:
getInputStreamWrapperin classAbstractS3FileAccess- 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
Checks if a file exists in the S3 bucket at the specified path.- Specified by:
checkFileExistsOnS3in classAbstractS3FileAccess- 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
Retrieves the file size of a specified file stored on S3.- Specified by:
getFileSizeOnS3in classAbstractS3FileAccess- 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
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:
listFilesOnS3in classAbstractS3FileAccess- 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
Retrieves an object stored in S3 as a byte array.- Specified by:
getS3ObjectAsBytesin classAbstractS3FileAccess- 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
Retrieves the InputStream of an object stored in S3 at the specified full path.- Specified by:
getS3ObjectStreamin classAbstractS3FileAccess- 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
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:
detectInternalRootDirectoryin classAbstractS3FileAccess- 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.
-