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

}

Comments

  1. how can we give file here I mean how can we connect google cloud storage and get those filename location

    ReplyDelete
  2. You can use /gs and google cloud storage api for that.

    ReplyDelete
  3. Describe your problem with more description, we can help you with more accuracy.

    ReplyDelete
  4. How to read an image file

    ReplyDelete

Post a Comment

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Read Excel using Apache POI - Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'