Google Drive API - 按文件ID检查文件是否存在 [英] Google Drive API - Check if file exists by file ID

查看:293
本文介绍了Google Drive API - 按文件ID检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下载文件之前,我无法找到检查文件是否存在的好方法。
似乎API没有提供一种方法来检查文件是否通过ID获取文件存在。

I could not find a good way to check if the file exists before downloading it. It seems the API doesnt provide a way to check if the file exists by getting the file by ID.

基本上,我检查生成的outPutStream大小是否> 0来处理文件,但我不喜欢解决方案。

Basically Im checking if the generated outPutStream size is > 0 to process the file, but I didnt like the solution.

Drive driveService = new Drive.Builder(buildHttpTransport(), JSON_FACTORY, googleCredential).build();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
  driveService.files().get(this.fileId).executeMediaAndDownloadTo(outputStream);

  if(outputStream.size() == 0)
   processFile()

想法是可喜的!

Ideas are welcome!

推荐答案

好吧,如果你只是想检查文件是否存在,那么您可以使用 Files:list 在您的Google云端硬盘上列出该文件。您可以在结果中获取文件的ID。

Well, if you just want to check if the file is existed or not, then you can use the Files: list to list the file on your Google Drive. You can get the id of the file in the results.

如果您已经知道文件ID,则可以使用 Files:get

If you already know the file id, then you can verify it by using the Files: get.

GET https://www.googleapis.com/drive/v3/files/fileId

如果文件ID存在,您将在此处收到响应200

You will get a response 200 here if the File id is existed.

对于您想要的Java代码,请检查相关的 SO问题,如果它可以帮助你的话。

For the Java code that you want, check this related SO question if it can help you.

这篇关于Google Drive API - 按文件ID检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆