I am doing a service automatic generate file and upload it to google drive using Spring boot and java 8. This is an external service of another bigger service.
The Google environment I'm using:
- Google account A with 1 folder name: reports
- Service account B with the highest access and modifier to folder reports in google account A
- Service account key C was created by service account B
There was some file and folder inside folder reports that I was deleted normally by UI using google account A (remove then empty trash). While I'm query using service account key C, those file and folder that I was deleted still show up in the result. I have wait 1 day but the result still contain deleted file.
Can somebody explain for me about this behavior of Google Drive API?
Here are the code :
public List<GoogleFileItem> getAllFile() {
try {
if (!serviceAccountKey.exists()) {
throw new Exception("key file don't exist");
}
Drive drive = createDrive();
List<GoogleFileItem> responseList = new ArrayList<>();
FileList result = drive.files().list()
.setQ("trashed=false")
.setFields("nextPageToken, files(id, name, kind, mimeType, parents, trashed)")
.execute();
List<File> files = result.getFiles();
int i =0 ;
for (File file : files) {
if (file.getTrashed()) i++;
GoogleFileItem item = new GoogleFileItem();
item.setId(file.getId());
item.setName(file.getName());
item.setThumbnailLink(file.getThumbnailLink());
item.setKind(file.getKind());
item.setMimeType(GoogleMimeType.find(file.getMimeType()));
if (file.getParents() != null) {
item.setParents(file.getParents());
} else {
item.setParents(Collections.singletonList(""));
}
responseList.add(item);
}
return responseList;
} catch (Exception e) {
e.printStackTrace();
logger.error("Exception: " + e);
return null;
}
}
Edit:
- One important point I'm missing is that the file I deleted was upload to Google drive using API use service account key C so it might be the cause of problem but I'm not sure