Write to a Google Drive Folder

This example shows how to write a file to a folder in Google Drive with DataPipeline. It reads a local CSV file, builds an Excel workbook in memory, and saves it into an existing folder named First Folder on your Google Drive. Files are placed in a folder using the two-argument form of writeFile() on the GoogleDriveFileSystem.

 

Authentication & Setup

GoogleDriveFileSystem is constructed from four values: an APPLICATION_NAME, a USER_NAME, an InputStream of the OAuth 2.0 client-secrets file (example/data/input/client_id.json, downloaded from the Google Cloud console), and a CREDENTIALS_DIR (example/credentials) where the authorized credential is stored. On the first run Google prompts you to authorize access in a browser; once a credential has been stored, Google will not prompt again unless it is deleted.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.google.drive;

import java.io.File;
import java.io.FileInputStream;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.excel.ExcelDocument;
import com.northconcepts.datapipeline.excel.ExcelWriter;
import com.northconcepts.datapipeline.googledrive.GoogleDriveFileSystem;
import com.northconcepts.datapipeline.job.Job;

public class WriteToAGoogleDriveFolder {

    private static final String APPLICATION_NAME = "datapipeline";
    private static final String USER_NAME = "YOUR GOOGLE DRIVE USERNAME";
    private static final File CLIENT_SECRETS = new File("example/data/input/client_id.json");
    private static final File CREDENTIALS_DIR = new File("example", "credentials");

    // On first run you will be prompted by Google to authorize access in a browser.
    // Once a credential has been stored in CREDENTIALS_DIR, Google will not prompt again
    // unless the stored credential is deleted.
    // Writing a file to a folder is the same as writing to a subfolder.
    public static void main(String[] args) throws Throwable {
        GoogleDriveFileSystem fileSystem = new GoogleDriveFileSystem(APPLICATION_NAME, USER_NAME,
                new FileInputStream(CLIENT_SECRETS), CREDENTIALS_DIR);
        fileSystem.open();

        try {
            DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
                    .setFieldNamesInFirstRow(true);

            ExcelDocument document = new ExcelDocument();
            DataWriter writer = new ExcelWriter(document).setSheetName("balance");

            Job.run(reader, writer);

            // "First Folder" must already exist in your Google Drive.
            document.save(fileSystem.writeFile("First Folder", "credit-balance-01.xls"));

            System.out.println("Records written: " + writer.getRecordCount());
        } finally {
            fileSystem.close();
        }
    }

}

 

Code Walkthrough

  • A CSVReader reads the local file example/data/input/credit-balance-01.csv, using its first row as field names.
  • An ExcelWriter writes the records into an in-memory ExcelDocument, placing them on a sheet named balance.
  • After Job.run() finishes populating the document, document.save(...) writes it to Drive using writeFile("First Folder", "credit-balance-01.xls"). The two-argument form targets a file inside an existing folder.
  • fileSystem.close() runs in the finally block.
Mobile Analytics