IT Certificaions

Google Cloud Platform

Monday, March 11, 2013

Read file from google cloud storage using Java

You can read file from google cloud storage using following code....

public void readTextFileOnly(String fileName) { 
        log.info("Reading from google cloud storage,fileName:"+fileName);
        FileService fileService = FileServiceFactory.getFileService();
        String filename = "/gs/" + BUCKET_NAME + "/" + fileName;
        log.info("Reading from google cloud storag: filename:"+filename);
        AppEngineFile readableFile = new AppEngineFile(filename);
        FileReadChannel readChannel;
        try {
                readChannel = fileService.openReadChannel(readableFile, false);
                BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
                String line = reader.readLine();
                readChannel.close();
        } catch (FileNotFoundException e) {
            log.severe("FileNotFoundException:"+e.getMessage());
            e.printStackTrace();
        } catch (LockException e) {
            log.severe("LockException:"+e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.severe("IOException:"+e.getMessage());
            e.printStackTrace();
        }

}