7

How do I get a single files name from a get request using the drive api?

I have made a request but there is not metadata about the file there, I can only download it.

var fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
var request = driveService.Files.Get(fileId);

Apparently this return a files.get in the response according to this doc

I just want to download a file and have its name displayed, not just its id

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
FillyPajo
  • 113
  • 1
  • 2
  • 8

3 Answers3

4

For Google Drive V3:

C#:
string f = driveService.Files.Get(fileId).Execute().Name;

VB:
Dim f As String = driveService.Files.Get(fileId).Execute().Name

Fadi
  • 3,302
  • 3
  • 18
  • 41
3

You can get the file name from the Title property in the File class:

string FileName = service.Files.Get(FileId).Execute().Title;

and for downloading,

// DriveService _service: a valid Authendicated DriveService
// Google.Apis.Drive.v2.Data.File _fileResource: Resource of the file to download. (from service.Files.Get(FileId).Execute();)  
// string _saveTo: Full file path to save the file

public static void downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
    if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
    {
        try
        {
            var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
            byte[] arrBytes = x.Result;
            System.IO.File.WriteAllBytes(_saveTo, arrBytes);
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message, "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.Exit(0);
        }
    }
}
Divins Mathew
  • 2,908
  • 4
  • 22
  • 34
0

Try this

    /// 
    /// Download a file
    /// Documentation: https://developers.google.com/drive/v2/reference/files/get
    /// 
    /// a Valid authenticated DriveService
    /// File resource of the file to download
    /// location of where to save the file including the file name to save it as.
    /// 
    public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
    {

        if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
        {
            try
            {
                var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
                byte[] arrBytes = x.Result;
                System.IO.File.WriteAllBytes(_saveTo, arrBytes);
                return true;                  
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
                return false;
            }
        }
        else
        {
            // The file doesn't have any content stored on Drive.
            return false;
        }
    }

Code ripped from my Google drive api C# download tutorial. Which I haven't updated in ages so if there are any issues let me know and I will fix them.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449