Back to blogYouTube Video

Published December 11, 2025

Syncing projects with Google Drive - Part 13 (Overwriting files)

Can't play the video or having issues? Here's the direct link.

AI Summary

In this video, the developer implements a mechanism to overwrite existing files on Google Drive during the synchronization process. Previously, the application created duplicate files when changes were detected via checksums instead of updating the existing ones.

Key Takeaways

  • Implemented a search query using `files.list` to check if a file with the same name already exists within a specific parent folder on Google Drive.
  • Integrated the `files.update` method to overwrite the raw bytes of existing files when a match is found, rather than calling the create/upload function.
  • Added logging to track which specific files are being overridden during the sync process.
  • Verified the implementation by updating the source code of the project and confirming that the changes were reflected in the existing Google Drive file without creating duplicates.

Description

Book a call: https://calendly.com/itshassanaziz/discuss-a-project ==== ==== ==== In this video, we'll begin overwriting existing files with new changes. Currently, modified files don't get properly synced. We're going to fix that now. If you're learning something useful from these videos, make sure to subscribe to the channel. I've got a lot of amazing content planned for the future. :) ==== ==== ==== Github: https://github.com/hassanaziz0012/drive-syncer-video Full Playlist: https://youtube.com/playlist?list=PLTGiYd8gFivjVHK2xB7TLf7XLW8rmKXRM&si=L97UpFIHgfHbvLMT LINKS Website: https://www.hassandev.me Portfolio: https://www.hassandev.me/work YouTube: https://www.youtube.com/@itshassanaziz?sub_confirmation=1 My Book: https://www.hassandev.me/designing-websites X / Twitter: https://x.com/nothassanaziz

Transcript

Auto-generated transcript
Welcome back everyone to the DriveSinker project. In the previous video we built a couple more functions in the tracker implementation over here. We built the walk project tree function. This is going to walk the entire project tree on Google Drive and build a mapping of every single file and folder on Google Drive with our local files and folders. We also fixed a major issue in the subfolders implementation that we had before where we were using the subfolder name instead of the subfolder path and that was introducing a couple of bugs in our program and so we fixed that and we also built this map subfolders function over here to make sure that we're reusing subfolders that already exist on google drive now we're like 80 to 90 percent done with this new sync algorithm that we've been building for the last couple of videos but there's still a couple of more things we need to implement so right now we use the project tree function and the should upload function over here to check whether we should upload new files or not right to upload new files that have not been uploaded yet but we're not overriding any files even though we use the should upload function over here to compare the file checksums and see whether we should upload the file or not we're not overriding the file we're creating a new file we're uploading a new file in its place right if the checksums don't match over here then we'll get sure upload equals true right and then we'll upload the new file over there instead of uploading or instead of overwriting the existing file on google drive so that's one thing we need to fix we need to overwrite existing files with new changes instead of creating new duplicate files right that's one thing and the second thing is we need to delete ghost files now ghost files ghost files are basically just files that have been left on google drive but they don't have a a replica on our local machine so basically if i upload my entire project over here to google drive and then let's say i delete a couple of files over here right they would not get deleted on google drive not with our current implementation they would just end up being ghost files that just sit in the project on Google Drive not doing anything at all, right? And so to fix that we need to also make sure that we delete ghost files from the project every time we sync. So depending on how much time I have in this video I going to implement all of these things and hopefully we can keep the video shorter than like 10 minutes I try not to go longer than that but with all that being said that was my intro let's get started. First thing I'm going to do is just make sure that everything in my Google Drive is empty and we're starting fresh. I'm gonna upload everything to Google Drive. Okay and since they since my Google Drive is empty right now this is going to upload every single file and not skip anything so we're gonna let that run for a bit the first thing we're going to implement is overriding existing files and to do that we're going to go to api sync.go down here in the walkthrough function um we're going to go down down down where we upload files here in our upload file function this is where we actually create new files and upload the file contents right and this is where we actually have to implement overriding the existing files first of all let me just move all of the file opening stuff up here at the very top and then let me just create a query over here to search for the file that already exists so we've done this plenty of times guys you should know how this works by now fmt.sprintf name that's not name name wrapped in single quotes is equal to this and the parent name over here wrapped in single quotes in parents and trash equals false and then let's pass in our arguments over here we want to pass in the name of the file and also its parent id which we get in the arguments up here so we can just use that and i'll even i'll even add a comment over here saying something like if it already exists overwrite it now we're going to use this query to search for the file if it already exists right so let's do drive.api dot dot files dot list dot q passing the query dot page size there we go and this will be one and then dot do to run the entire thing and then let's create a variable to hold this. I'll call it searchResp. And of course, before anything else, make sure to handle the error. If there is an error, let's just return it and let the caller decide how to deal with it Now let see if a file actually exists and to do that we can just do if length search resp is equal to one and then we can add a log over here and say something like file and then the file name already exists overriding just so we know what's going on right and of course make sure to add a new line as well and pass in the file name over here or actually instead of the file name we can actually just pass in the path and that will probably be a lot better because the file names can be duplicates right but the path is going to be unique per file so let's pass that in instead and then let's fetch the fetch the actual file on google drive i'll call it drive file and this will be search rest dot files and then the first element in here and then let's create a new file with new metadata this will be a reference to drive.file and this will just be empty over here and then i'll say if underscore error equals drive.api.files.update and here we need to pass in the first of all the file id of the file we're trying to update so that i guess is going to be drive.drive file.id and then we need to pass the new metadata of the file and that's just going to be new file right we're not changing any of the metadata so this is just an empty drive.file struct over here because i'm not changing any metadata i don't want to change any metadata i just want to update the file content right like the raw bytes in the file so we're going to pass in media f that's the file that we have open up here and then we'll say dot do to run the entire thing and we'll say if error does not equal to nil then for now i just want to print the error on screen not log it but just print it if there is an actual error then i can actually see it on the screen and we'll also return the error there we go and then over here if everything went smoothly we'll just return nil otherwise over here if the file doesn't exist then we'll just create a new one right we'll create the file metadata we'll upload it on google drive and everything just like we did before right but if the file already exists then we're going to update the file contents directly right we're going to overwrite the file and that's pretty much all we need to do to implement the overriding functionality and by the way the project upload has already finished that we started in the at the beginning of the video and all of the stuff over here has been updated let actually do some testing over here let try to upload the entire project again and see if it works perfectly or not now since we have changed pretty much nothing in this application except for the sync and tracker files i guess it should not upload any new files except for the sync.go file because we actually added some new code over here right in this upload file function and you can already see it says file sync dot go over here already exists overriding and we can actually go over to the project on Google Drive in API sync dot go and see if it actually did that so this is the sync dot go file this is the code over here if I keep scrolling all the way down to the upload file function you can see it actually actually added all of the new code over here as well, right? Maybe I should zoom in so you guys can see. Oops, I think I just zoomed in a bit too much. But yeah, you can see that in the upload file function over here, it added all of the new code as well that we just wrote literally just now, right? Is that not amazing? So as you can see, overwriting files works. You can literally make changes to the files over here and then you can upload the project onto Google Drive and it's going to overwrite the files with the new code that you've written. So this is amazing and we've already done like some of the work over here. Now the next thing I wanted to build was functionality to delete ghost files. Now sadly the video is getting a bit too long as well so I'm gonna have to stop over here. I'm trying to keep every video in like the 10 to 15 minute range. As you can see we're about to go past that now. So I'm gonna stop the recording over here and in the very next video we're going to build the delete ghosts functionality so i want to be able to delete the ghost files if i delete some files in my local project i do not want to see them in google drive just taking up empty space right we're going to build all of that stuff in the very next video but um thank you for watching this one and i hope you learned something new this was a very short video i guess we just built the overriding files functionality so if you enjoyed this press like if you want to subscribe press subscribe guys we're going to be building all sorts of cool stuff on this channel so if you haven't subscribed already go make sure you do that scroll down click the subscribe button and see you in the next video

Share this article

All great things started with a conversation

If you've got a cool project or opportunity and you want me to be a part of it, set up a free meeting with me here, and let's talk. 😊