Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API – DZone Cloud | xxxIs Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API – DZone Cloud – xxx
菜单

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API – DZone Cloud

一月 28, 2019 - MorningStar

Over a million developers have joined DZone.
Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API - DZone Cloud

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API

DZone’s Guide to

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API

Is Google Drive hiding something? Take a look at the internal structure of the tool through the lense of an API.

Feb. 18, 19 · Cloud Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Learn how to migrate and modernize stateless applications and run them in a Kubernetes cluster.

Google Drive is one of the most popular consumer and business cloud storage SaaS apps today. Like many cloud storage providers, Google Drive has a desktop app users use to sync files between their local file-system and their cloud account. This mirrors neatly with the organization of files in Google Drive, which are located in folders, or folder-equivalent Team Drives.

However, there is one major difference that quickly becomes apparent when accessing Google Drive through an API: files and folders in Google Drive can each have multiple parent folders!

You can try this for yourself via the web UI by selecting a file in Google Drive and typing Shift+Z on your keyboard. A menu opens that allows you to add the file or folder to another folder. In the screenshots below, the folder C has been added to folders B1 as well as B2. The metadata of C shows both those folders as “parent” folders.

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API - DZone Cloud

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API - DZone Cloud

The secret shortcut and lack of equivalent menu options indicates that the Google Drive UI prefers to discourage users from creating these types of organizational structure. However, it still supports it—possibly for backwards compatibility. While Google Drive may have started out as a cloud service, the presence of a desktop sync app necessitates a solution compatible with traditional filesystems (ignoring symlinks).

The API makes this even clearer. The file/folder update endpoint accepts the query parameters addParents and removeParents that each allow multiple parent IDs. Here’s an example of it in action in Python from the Google Drive API guide:

file_id = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ' folder_id = '0BwwA4oUTeiV1TGRPeTVjaWRDY1E'   # Retrieve the existing parents to remove file = drive_service.files().get(     fileId=file_id, fields='parents').execute() previous_parents = ",".join(file.get('parents'))   # Move the file to the new folder file = drive_service.files().update(     fileId=file_id,     addParents=folder_id,     removeParents=previous_parents,     fields='id, parents').execute()  


A Tree or A Graph?

This raises the question as to whether the Google Drive layout is a tree or closer to a directed acyclic graph (DAG). The Google Drive layout resembles a directed acyclic graph since it does not permit any item to be the parent of an item it is already within.

Recreating the User Experience via The API

API-based uploads and folder creations require additional steps as a result of the ability to add multiple parents to an item.

The Google web UI attempts to conceal this functionality by overwriting files with the same name by default as shown in the screenshot below.

Uploading the same file twice:

Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API - DZone Cloud

Selecting the option to keep the files separate actually proceeds to rename the file instead of preserve the file name. This helps maintain the illusion that Google Drive behaves similarly to a file-system tree that requires unique names for each item in a folder.
Is Google Drive a Tree or A Graph?: Working with Folders via The Google Drive API - DZone Cloud
The file automatically renames!

To mirror this behavior via the API, first query Google Drive (docs) for files within the target parent folder that match the new file or folder name, and then either overwrite that item or create a new one with a different name.

While each cloud storage provider has unique features, Google Drive’s graph-based filesystem structure under the hood stands out. Gmail’s innovative use of labels as folders, and message threads, parallels the approach to folders in Google Drive. 

Join us in exploring application and infrastructure changes required for running scalable, observable, and portable apps on Kubernetes.

Topics:
google drive ,file system api ,cloud storage ,cloud integration ,api ,cloud api ,cloud

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51