No error thrown when uploading to non-existent Google Drive folder

Hey everyone,

I’m working on an Android app that uploads files to Google Drive folders. It’s going fine when the folder exists, but I’ve run into a weird issue.

If I delete the target folder in Google Drive and then try to upload a file, the app acts like everything’s okay. No errors, no exceptions. The upload seems successful, but the file is nowhere to be found in Drive.

Is this normal behavior? Should I check if the folder exists before each upload, or is there a better approach to handle this?

Here’s a simplified version of my upload code:

DriveFile file = new DriveFile();
file.setName(fileName);
file.setDescription(fileDesc);
file.setType(fileType);

file.setParentFolder(targetFolderId);

FileData content = new FileData(filePath);
FileUploader uploader = new FileUploader(file, content);

try {
    uploader.start();
} catch (UploadException e) {
    Log.e(TAG, "Upload failed", e);
}

Any ideas? Thanks!

I’ve dealt with this scenario in my projects too. It’s a tricky behavior of the Google Drive API that can catch you off guard. Instead of checking for folder existence before each upload, which can slow things down, I’ve found a more efficient approach. Implement error handling and recovery in your upload process.

Here’s what I do: I attempt the upload as normal, but then verify the file’s location afterward. If it’s not in the expected folder, I move it to the correct place or recreate the folder if needed. This method is more robust and handles various edge cases.

Additionally, consider caching folder IDs locally and refreshing them periodically. This reduces API calls and improves overall performance. Just remember to handle cases where cached IDs might become invalid.

ive seen this before. its a weird quirk with the drive API. what i do is check the upload result after it finishes. if the file isnt where it should be, i move it to the right folder or make a new one. its not perfect but it works pretty well for me. also, caching folder IDs can help speed things up a bit.

I’ve encountered this exact issue in one of my projects. It’s a quirk of the Google Drive API that can be pretty frustrating.

Here’s what I found: when you try to upload to a non-existent folder, the API silently creates a new file in the root of your Drive instead. No errors, no warnings - it just does its own thing.

To handle this, I implemented a folder existence check before each upload. It adds a bit of overhead, but it’s saved me from a lot of headaches. Something like:

Drive.Files.Get request = drive.files().get(folderId);
try {
    File folder = request.execute();
    if (folder == null || !folder.getMimeType().equals("application/vnd.google-apps.folder")) {
        // Handle non-existent folder
        return;
    }
    // Proceed with upload
} catch (IOException e) {
    // Handle error
}

This approach has worked well for me, ensuring files always end up where they’re supposed to. Hope this helps!