Class GoogleDriveFileSystem
FileSystem that reads and writes files stored in
Google Drive.
Like every FileSystem, this class exposes raw file operations only —
readFile, writeFile and
listFolder/listRootFolder — and returns
plain InputStream/OutputStream handles that an ordinary DataPipeline reader or
writer operates on. It does not parse records itself. Because Google Drive identifies files by
an opaque id rather than a path, the String name accessors resolve a name to the
first matching file or folder; multiple entries may share a name but have distinct ids.
Authorization. Two credential modes are supported:
- Resolver-supplied OAuth access token — construct with
GoogleDriveFileSystem(String)and register aCredentialsResolverviasetCredentialsResolver(CredentialsResolver)that suppliesCredentials.TOKEN. The interactive flow is skipped entirely and the token is re-resolved on eachopen(), so rotated tokens are picked up automatically. - Interactive installed-app flow — construct with
GoogleDriveFileSystem(String, String, InputStream, java.io.File)passing the client secrets JSON and a folder to persist the granted credentials. The firstopen()triggers a browser consent prompt; subsequent runs reuse the stored credentials.
Lifecycle. The constructor only stores configuration; open() builds the transport,
resolves credentials and creates the Drive service. A write runs the upload
on a background thread fed by a piped stream; close the returned OutputStream to let that thread
finish, after which close() releases the piped streams and shuts the transport down. Note that
close() does not itself block waiting for an in-flight upload — if the write thread has not yet
signalled completion it only delegates to super.close(). Instances are not thread-safe and are
intended to be used by a single pipeline at a time.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe Google Drive MIME type identifying an entry as a folder rather than a file.Fields 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
ConstructorsConstructorDescriptionGoogleDriveFileSystem(String appName) Enables access to Google Drive with an OAuth access token supplied through aCredentialsResolver(seesetCredentialsResolver(CredentialsResolver)) — no client secrets, credentials folder, or interactive authorization required.GoogleDriveFileSystem(String appName, String userName, InputStream clientSecrets, File credentialsFolder) Enables access to Google Drive. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the Google Drive connection.Returns the first occurrence of the Google Drive File Id with the matchingfileName.getFolderId(String folderName) Returns the first occurrence of the Google Drive Folder Id with the matchingfolderName.List<File> listFolder(String folderName) Returns aListof all Google Drive files and folders under the specified folder.List<File> Returns aListof all Google Drive files and folders.voidopen()Opens a connection to the Google Drive file system.Reads the first occurrence of a Google Drive File with matchingfileName.Reads the first occurrence of a Google Drive File with matchingfileNamein the specifiedfolderName.setCredentialsResolver(CredentialsResolver credentialsResolver) Sets the resolver supplying this file system's OAuth access token (Credentials.TOKEN) — resolved on eachopen(), never at construction, so refreshed/rotated tokens are picked up on the next connection.Writes the file to the Google Drive File System.Write the file to the specified folder.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, finalize, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getNestedEndpoint, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getRootEndpoint, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime, setDescription, toString
-
Field Details
-
GOOGLE_FOLDER_TYPE
The Google Drive MIME type identifying an entry as a folder rather than a file.- See Also:
-
-
Constructor Details
-
GoogleDriveFileSystem
public GoogleDriveFileSystem(String appName, String userName, InputStream clientSecrets, File credentialsFolder) Enables access to Google Drive.- Parameters:
appName- application name to use when connecting to Google DriveuserName- the username of the Google Drive accountclientSecrets- the JSON file client secret provided by Google DrivecredentialsFolder- the folder to store the Google Drive Credentials
-
GoogleDriveFileSystem
Enables access to Google Drive with an OAuth access token supplied through aCredentialsResolver(seesetCredentialsResolver(CredentialsResolver)) — no client secrets, credentials folder, or interactive authorization required.- Parameters:
appName- application name to use when connecting to Google Drive
-
-
Method Details
-
setCredentialsResolver
Sets the resolver supplying this file system's OAuth access token (Credentials.TOKEN) — resolved on eachopen(), never at construction, so refreshed/rotated tokens are picked up on the next connection. When a token is resolved, the interactive authorization flow (client secrets + stored credentials + browser consent) is skipped entirely; when it isn't, the constructor-configured flow runs unchanged.- Overrides:
setCredentialsResolverin classFileSystem
-
open
Opens a connection to the Google Drive file system.- Overrides:
openin classEndpoint- Throws:
DataException
-
close
Closes the Google Drive connection.- Overrides:
closein classFileSystem- Throws:
DataException
-
listFolder
Returns aListof all Google Drive files and folders under the specified folder.- Parameters:
folderName- name of the folder- Returns:
- a
List<File>of the folder's immediate children (possibly empty if the folder has none), ornulliffolderNameis empty - Throws:
Throwable- if the folder does not exist or the Drive request fails
-
listRootFolder
Returns aListof all Google Drive files and folders. Including files in the trash.- Returns:
- the contents as a
List<File> - Throws:
Throwable
-
readFile
Reads the first occurrence of a Google Drive File with matchingfileName. It's possible that multiple files will have the same file name but different file ids.- Parameters:
fileName- the file name to read.- Returns:
- an
InputStream - Throws:
Throwable
-
readFile
Reads the first occurrence of a Google Drive File with matchingfileNamein the specifiedfolderName. It's possible that multiple files will have the same file name but different file ids.- Parameters:
folderName- the folder the file belongs tofileName- the name of the file- Returns:
- an
InputStream - Throws:
Throwable
-
writeFile
Writes the file to the Google Drive File System. It's possible that multiple files will have the same file name but different file ids.- Parameters:
fileName- the name of the file- Returns:
- an
OutputStreamfor writing - Throws:
Throwable
-
writeFile
Write the file to the specified folder. It's possible in Google Drive that multiple files or folders will have the same name but different file ids.- Parameters:
folderName- the name of the folderfileName- the name of the file- Returns:
- an
OutputStreamfor writing - Throws:
Throwable
-
getFolderId
Returns the first occurrence of the Google Drive Folder Id with the matchingfolderName.- Parameters:
folderName- the name of the folder to search for- Returns:
- the folder id of the first occurrence of
folderName, ornulliffolderNameis empty - Throws:
Throwable- if no folder namedfolderNameexists or the Drive request fails
-
getFileId
Returns the first occurrence of the Google Drive File Id with the matchingfileName.- Parameters:
fileName- the name of the file to search for- Returns:
- the file id of the first occurrence of
fileName, ornulliffileNameis empty - Throws:
Throwable- if no file namedfileNameexists or the Drive request fails
-