MarkyDD
MarkyDD

Reputation: 272

Can you get the fileId from the filename/path with the Google.Apis.Drive.v3 DriveService

I'm writing a service that I would like to be able to delete files from Google Drive. But I would like the file identifier to be the filename and path. It looks like the Google.Apis.Drive.v3 API only accepts the fileId, which I don't know how to get without using the UI. Is there a way to get the fileId for a file in Drive with only the file name and path?

Or is there another way to find the fileId without using the UI?

Here's the API docs: https://googleapis.dev/dotnet/Google.Apis.Drive.v3/latest/api/Google.Apis.Drive.v3.FilesResource.html

Upvotes: 2

Views: 69

Answers (1)

Giselle Valladares
Giselle Valladares

Reputation: 2261

You should be able to do a query with the name of the file, mime type, and other options to search the files. For example:

var request = service.Files.List();
request.PageSize = 1000;
request.Q = "mimeType = 'application/vnd.google-apps.folder' and name = 'test_folder'"
var results = await request.ExecuteAsync();

foreach (var driveFile in results.Files)
      {
       Console.WriteLine($"{driveFile.Name}  {driveFile.MimeType}  {driveFile.Id}");
       }

Which will search folders with the name "test_folder". You can see other queries available in the Google documentation here, and the queries operators available here. Lastly, you can review the Google Drive supported MIME types here, so you can change the query to other files, not just folders.

With this sample, you get the fields File.Id, MimeType and Name. You can also get the fields:

  • kind
  • id
  • name
  • mimeType
  • etc. The full list can be seen in the overview

I found the following blog with more information about searching files on Google Drive; it has some other samples that you can use.

Reference:

Upvotes: 1

Related Questions