Code Monkey home page Code Monkey logo

Comments (4)

shcheklein avatar shcheklein commented on July 30, 2024

@jayofelony so, what exactly doesn't work in this case?

it should be possible to get a ID by its name and it should be possible to create a new directory. So the approach makes sense overall. We just need to see where exactly it breaks.

from pydrive2.

jayofelony avatar jayofelony commented on July 30, 2024

My bad, I am searching in the drive for a folder that does not exist. I edited the post a little with errors thrown.

SO basiscally I know the folder does not exist, so I need it to be created. Because a user can change the backup folder if they want.

from pydrive2.

shcheklein avatar shcheklein commented on July 30, 2024

Could you check this example https://github.com/iterative/PyDrive2/blob/main/examples/Upload-and-autoconvert-to-Google-Drive-Format-Example/upload.py#L38 and try to "play" with a query a bit? I think you might need to pass something like this:

query = {'q': f"title = '{filename}' and mimeType='{mimetype}'"}

Check also this doc. And also I would try to run it first on the online sandbox here.

from pydrive2.

jayofelony avatar jayofelony commented on July 30, 2024

Right, I have come to a succesful edit.

def get_folder_id_by_name(self, drive, folder_name):
        file_list = drive.ListFile({'q': "mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()
        for file in file_list:
            if file['title'] == folder_name:
                return file['id']
            return None
            
 if not self.backup:
    # Use self.options['backup_folder'] as the folder ID where backups are stored
    backup_folder_id = self.get_folder_id_by_name(self.drive, self.options['backup_folder'])
     if backup_folder_id is None:
         # If the folder doesn't exist, create it
         folder = self.drive.CreateFile(
                        {'title': self.options['backup_folder'], 'mimeType': 'application/vnd.google-apps.folder'})
         folder.Upload()
         backup_folder_id = folder['id']
         logging.info(f"[gDriveSync] Created folder '{self.options['backup_folder']}' with ID: {backup_folder_id}")

     # Continue with the rest of the code using backup_folder_id
     file_list = self.drive.ListFile({'q': f"'{backup_folder_id}' in parents and mimeType = 'application/vnd.google-apps.folder' and trashed=false"}).GetList()

from pydrive2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.