Class AmazonS3FileSystem
java.lang.Object
com.northconcepts.datapipeline.core.DataObject
com.northconcepts.datapipeline.core.Endpoint
com.northconcepts.datapipeline.internal.lang.FileSystem
com.northconcepts.datapipeline.amazons3.AmazonS3FileSystem
Class for accessing an Amazon S3 file system.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAdapts the backend-neutralCredentialsResolverseam to the AWS SDK'sso resolved credentials flow through the native client natively.invalid reference
AwsCredentialsProvider -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longHow long before a credentials snapshot's reported expiry theAmazonS3FileSystem.CredentialsResolverBridgere-resolves from theCredentialsResolver.static final intstatic final StringPass tosetDelimiter(String)to list one folder level at a time.static final intFields inherited from class com.northconcepts.datapipeline.core.Endpoint
BUFFER_SIZE, captureElapsedTime, DEFAULT_READ_BUFFER_SIZEFields inherited from class com.northconcepts.datapipeline.core.DataObject
id, log, name, TIMESTAMP_FORMAT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the Amazon S3 connection — or, while a source or sink stream or a size lookup is still using it, as soon as the last of them finishes.voiddeleteFile(String bucket, String key) Deletes an S3 object from the specified bucket.booleanChecks if an S3 object exists in the specified bucket.protected voidfinalize()Disconnects even with leases outstanding: this file system being unreachable, the streams holding them were dropped without being closed.S3ClientReturns the AWS SDK client, ornullif none was supplied and this file system is not connected.AwsCredentialsProviderReturns the AWS SDK credentials provider used when noCredentialsResolveris set (defaults to the standard AWS credentials provider chain).The delimiter sent with every listing.Returns the S3 service endpoint override ornullif none is set.longgetFileSize(String bucket, String key) Returns the size of an S3 object in bytes.Returns the AWS region set bysetRegion(String), ornullif it is resolved when connecting.booleanisDebug()Indicates if multipart uploads log each part and their completion or abort at debug level (default is false).Returns the buckets owned by the authenticated user.listFolder(String bucket, String folder) Returns the objects under the specified folder, including nested keys.listRootFolder(String bucket) Returns the objects in the specified bucket, including nested keys.nextBatch(S3ObjectListing previousListing) Returns the next set of paged objects.voidopen()Opens a connection to the Amazon S3 file system.Reads the file in the S3 bucket.setBasicAWSCredentials(String accessKey, String secretKey) Authenticates with the given access key and secret key, replacing the configured credentials provider, unless aCredentialsResolveris set, which takes precedence.setClient(S3Client client) Uses the given AWS SDK client instead of building one from this file system's credentials, region and endpoint settings; the caller remains responsible for closing it.setCredentialsProvider(AwsCredentialsProvider credentialsProvider) Sets the AWS SDK credentials provider used when noCredentialsResolveris set (defaults to the standard AWS credentials provider chain).setCredentialsResolver(CredentialsResolver credentialsResolver) Sets the backend-neutral resolver supplying this file system's credentials (seeCredentialsResolver).setDebug(boolean debug) Indicates if multipart uploads log each part and their completion or abort at debug level (default is false).setDelimiter(String delimiter) Sets the delimiter sent with every listing (unset by default, so listings include nested keys);FOLDER_DELIMITERlists one folder level at a time.setEndpointOverride(String endpointOverride) Overrides the S3 service endpoint (e.g.Sets the AWS region (e.g.useProfileCredentialsProvider(String profileName) Authenticates using the named profile from the AWS profile files (instead of the default profile used by the default credentials provider chain), unless aCredentialsResolveris set, which takes precedence.writeMultipartFile(String bucket, String filePath) Returns a stream that uploads to the given bucket and key as a multipart upload, in parts ofMINIMUM_PART_SIZEbytes sent from a background thread.writeMultipartFile(String bucket, String filePath, String contentType) Same aswriteMultipartFile(String, String), but also sets the object's content type unlesscontentTypeis null or blank.Methods inherited from class com.northconcepts.datapipeline.internal.lang.FileSystem
credentialValue, getCredentialsResolver, getCurrentCredentials, refreshCredentials, resolveCredentialsMethods inherited from class com.northconcepts.datapipeline.core.Endpoint
addElapsedtime, addExceptionProperties, assertClosed, assertNotOpened, assertOpened, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getNestedEndpoint, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getRootEndpoint, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime, setDescription, toString
-
Field Details
-
FOLDER_DELIMITER
Pass tosetDelimiter(String)to list one folder level at a time.- See Also:
-
MINIMUM_PART_SIZE
public static final int MINIMUM_PART_SIZE- See Also:
-
DEFAULT_QUEUE_SIZE
public static final int DEFAULT_QUEUE_SIZE- See Also:
-
CREDENTIALS_EXPIRY_SKEW_MILLISECONDS
public static final long CREDENTIALS_EXPIRY_SKEW_MILLISECONDSHow long before a credentials snapshot's reported expiry theAmazonS3FileSystem.CredentialsResolverBridgere-resolves from theCredentialsResolver.- See Also:
-
-
Constructor Details
-
AmazonS3FileSystem
public AmazonS3FileSystem()Constructs an Amazon S3 file system instance. By default it authenticates using the standard AWS credentials provider chain (environment variables, system properties, profile files, container/instance credentials, etc.). Use one of the credential methods below to override.
-
-
Method Details
-
setCredentialsResolver
Sets the backend-neutral resolver supplying this file system's credentials (seeCredentialsResolver). Resolved atopen()— never at construction — so rotated secrets are picked up on the next connection; expiring credentials (for example STS session tokens) are additionally re-resolved mid-session when they report expiry withinCREDENTIALS_EXPIRY_SKEW_MILLISECONDS.When a resolver is set, it takes precedence over
setCredentialsProvider(AwsCredentialsProvider)and the default provider chain.- Overrides:
setCredentialsResolverin classFileSystem
-
getCredentialsProvider
public AwsCredentialsProvider getCredentialsProvider()Returns the AWS SDK credentials provider used when noCredentialsResolveris set (defaults to the standard AWS credentials provider chain). -
setCredentialsProvider
Sets the AWS SDK credentials provider used when noCredentialsResolveris set (defaults to the standard AWS credentials provider chain). -
useProfileCredentialsProvider
Authenticates using the named profile from the AWS profile files (instead of the default profile used by the default credentials provider chain), unless aCredentialsResolveris set, which takes precedence. -
setBasicAWSCredentials
Authenticates with the given access key and secret key, replacing the configured credentials provider, unless aCredentialsResolveris set, which takes precedence. -
getRegion
Returns the AWS region set bysetRegion(String), ornullif it is resolved when connecting. -
setRegion
Sets the AWS region (e.g."us-east-1"). When unset, the region is resolved from the default region provider chain, falling back tous-east-1. -
getDelimiter
The delimiter sent with every listing. Unset by default, so listings include nested keys;FOLDER_DELIMITERlists one folder level at a time. -
setDelimiter
Sets the delimiter sent with every listing (unset by default, so listings include nested keys);FOLDER_DELIMITERlists one folder level at a time. -
getEndpointOverride
Returns the S3 service endpoint override ornullif none is set. -
setEndpointOverride
Overrides the S3 service endpoint (e.g. for S3-compatible stores or a specific region endpoint). Combine withsetRegion(String)when signing requires it. -
isDebug
public boolean isDebug()Indicates if multipart uploads log each part and their completion or abort at debug level (default is false). -
setDebug
Indicates if multipart uploads log each part and their completion or abort at debug level (default is false). -
open
Opens a connection to the Amazon S3 file system. It stays open untilclose(): sources, sinks, and size lookups sharing this file system use the connection without closing it.Credentials are chosen in this order: the
CredentialsResolver(if set), then the configured(which defaults to the standard provider chain).invalid reference
AwsCredentialsProvider- Overrides:
openin classEndpoint- Throws:
DataException
-
close
Closes the Amazon S3 connection — or, while a source or sink stream or a size lookup is still using it, as soon as the last of them finishes. Clients built byopen()are closed and released so the nextopen()rebuilds one — picking up rotated credentials; clients supplied throughsetClient(S3Client)are left untouched — their lifecycle belongs to the caller — and remain usable if this file system is reopened.- Overrides:
closein classFileSystem- Throws:
DataException
-
finalize
Disconnects even with leases outstanding: this file system being unreachable, the streams holding them were dropped without being closed. -
getClient
public S3Client getClient()Returns the AWS SDK client, ornullif none was supplied and this file system is not connected. -
setClient
Uses the given AWS SDK client instead of building one from this file system's credentials, region and endpoint settings; the caller remains responsible for closing it. -
readFile
Reads the file in the S3 bucket. To specify a nested folder, use "/" as the path separator. -
exists
Checks if an S3 object exists in the specified bucket.- Parameters:
bucket- the name of the S3 bucketkey- the key (path) of the S3 object- Returns:
- true if the object exists, false otherwise
- Throws:
DataException- if bucket or key is empty, or if an error occurs while checking existence
-
getFileSize
Returns the size of an S3 object in bytes.- Parameters:
bucket- the name of the S3 bucketkey- the key (path) of the S3 object- Returns:
- the object's size in bytes, or -1 if the key is absent
- Throws:
DataException- if bucket or key is empty, if the bucket does not exist, or if an error occurs while reading the object's metadata
-
deleteFile
Deletes an S3 object from the specified bucket.- Parameters:
bucket- the name of the S3 bucketkey- the key (path) of the S3 object to delete- Throws:
DataException- if bucket or key is empty, or if an error occurs while deleting the object
-
writeMultipartFile
Returns a stream that uploads to the given bucket and key as a multipart upload, in parts ofMINIMUM_PART_SIZEbytes sent from a background thread. Closing the stream completes the upload; a failed upload is aborted and its error rethrown by the next write or close. -
writeMultipartFile
Same aswriteMultipartFile(String, String), but also sets the object's content type unlesscontentTypeis null or blank. -
listBuckets
Returns the buckets owned by the authenticated user.- Throws:
Throwable
-
listRootFolder
Returns the objects in the specified bucket, including nested keys. With adelimiterset, keys containing it are rolled up intoS3ObjectListing.getCommonPrefixes()instead.- Throws:
Throwable
-
listFolder
Returns the objects under the specified folder, including nested keys. With adelimiterset, keys containing it below the folder are rolled up intoS3ObjectListing.getCommonPrefixes()instead.- Throws:
Throwable
-
nextBatch
Returns the next set of paged objects.- Throws:
Throwable
-