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();
}
}
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();
}
}
how can we give file here I mean how can we connect google cloud storage and get those filename location
ReplyDeleteYou can use /gs and google cloud storage api for that.
ReplyDeleteDescribe your problem with more description, we can help you with more accuracy.
ReplyDeleteHow to read an image file
ReplyDelete