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();
        }

}

Popular posts from this blog

Android: where to run keytool command in android

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine