public class GoogleDriveFileSystem extends FileSystem
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:
GoogleDriveFileSystem(String)
and register a CredentialsResolver via setCredentialsResolver(CredentialsResolver)
that supplies Credentials.TOKEN. The interactive flow is skipped entirely and the token
is re-resolved on each open(), so rotated tokens are picked up automatically.GoogleDriveFileSystem(String, String, InputStream, java.io.File) passing the client
secrets JSON and a folder to persist the granted credentials. The first open() 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.
FileSystem,
CredentialsResolver| Modifier and Type | Field and Description |
|---|---|
static String |
GOOGLE_FOLDER_TYPE
The Google Drive MIME type identifying an entry as a folder rather than a file.
|
BUFFER_SIZE, captureElapsedTime, DEFAULT_READ_BUFFER_SIZEid, log, name, TIMESTAMP_FORMAT| Constructor and Description |
|---|
GoogleDriveFileSystem(String appName)
Enables access to Google Drive with an OAuth access token supplied through a
CredentialsResolver (see setCredentialsResolver(CredentialsResolver)) —
no client secrets, credentials folder, or interactive authorization required. |
GoogleDriveFileSystem(String appName,
String userName,
InputStream clientSecrets,
File credentialsFolder)
Enables access to Google Drive.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the Google Drive connection.
|
String |
getFileId(String fileName)
Returns the first occurrence of the Google Drive File Id with the matching
fileName. |
String |
getFolderId(String folderName)
Returns the first occurrence of the Google Drive Folder Id with the matching
folderName. |
List<File> |
listFolder(String folderName)
Returns a
List of all Google Drive files and folders under the specified folder. |
List<File> |
listRootFolder()
Returns a
List of all Google Drive files and folders. |
void |
open()
Opens a connection to the Google Drive file system.
|
InputStream |
readFile(String fileName)
Reads the first occurrence of a Google Drive File with matching
fileName. |
InputStream |
readFile(String folderName,
String fileName)
Reads the first occurrence of a Google Drive File with matching
fileName
in the specified folderName. |
GoogleDriveFileSystem |
setCredentialsResolver(CredentialsResolver credentialsResolver)
Sets the resolver supplying this file system's OAuth access token (
Credentials.TOKEN)
— resolved on each open(), never at construction, so refreshed/rotated tokens are
picked up on the next connection. |
OutputStream |
writeFile(String fileName)
Writes the file to the Google Drive File System.
|
OutputStream |
writeFile(String folderName,
String fileName)
Write the file to the specified folder.
|
credentialValue, getCredentialsResolver, getCurrentCredentials, refreshCredentials, resolveCredentialsaddElapsedtime, addExceptionProperties, assertClosed, assertNotOpened, assertOpened, finalize, getClosedOn, getDescription, getElapsedTime, getElapsedTimeAsString, getNestedEndpoint, getOpenedOn, getOpenElapsedTime, getOpenElapsedTimeAsString, getRootEndpoint, getSelfTime, getSelfTimeAsString, getState, isCaptureElapsedTime, isClosed, isOpen, setCaptureElapsedTime, setDescription, toStringpublic static final String GOOGLE_FOLDER_TYPE
public GoogleDriveFileSystem(String appName, String userName, InputStream clientSecrets, File credentialsFolder)
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 Credentialspublic GoogleDriveFileSystem(String appName)
CredentialsResolver (see setCredentialsResolver(CredentialsResolver)) —
no client secrets, credentials folder, or interactive authorization required.appName - application name to use when connecting to Google Drivepublic GoogleDriveFileSystem setCredentialsResolver(CredentialsResolver credentialsResolver)
Credentials.TOKEN)
— resolved on each open(), 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.setCredentialsResolver in class FileSystempublic void open()
throws DataException
open in class EndpointDataExceptionpublic void close()
throws DataException
close in class FileSystemDataExceptionpublic List<File> listFolder(String folderName) throws Throwable
List of all Google Drive files and folders under the specified folder.folderName - name of the folderList<File> of the folder's immediate children (possibly empty if the
folder has none), or null if folderName is emptyThrowable - if the folder does not exist or the Drive request failspublic List<File> listRootFolder() throws Throwable
List of all Google Drive files and folders. Including files in the trash.List<File>Throwablepublic InputStream readFile(String fileName) throws Throwable
fileName. It's possible that
multiple files will have the same file name but different file ids.fileName - the file name to read.InputStreamThrowablepublic InputStream readFile(String folderName, String fileName) throws Throwable
fileName
in the specified folderName. It's possible that
multiple files will have the same file name but different file ids.folderName - the folder the file belongs tofileName - the name of the fileInputStreamThrowablepublic OutputStream writeFile(String fileName) throws Throwable
fileName - the name of the fileOutputStream for writingThrowablepublic OutputStream writeFile(String folderName, String fileName) throws Throwable
folderName - the name of the folderfileName - the name of the fileOutputStream for writingThrowablepublic String getFolderId(String folderName) throws Throwable
folderName.folderName - the name of the folder to search forfolderName, or null if
folderName is emptyThrowable - if no folder named folderName exists or the Drive request failspublic String getFileId(String fileName) throws Throwable
fileName.fileName - the name of the file to search forfileName, or null if
fileName is emptyThrowable - if no file named fileName exists or the Drive request failsCopyright (c) 2006-2026 North Concepts Inc. All Rights Reserved.