<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhinav Anand</title>
    <description>The latest articles on DEV Community by Abhinav Anand (@abanand).</description>
    <link>https://dev.to/abanand</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F443493%2Fbf40c782-1d1b-489f-8ddb-8f7639328580.jpeg</url>
      <title>DEV Community: Abhinav Anand</title>
      <link>https://dev.to/abanand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abanand"/>
    <language>en</language>
    <item>
      <title>⚡️ Uploading &amp; Downloading files from Google Drive using Python</title>
      <dc:creator>Abhinav Anand</dc:creator>
      <pubDate>Fri, 04 Jun 2021 08:08:28 +0000</pubDate>
      <link>https://dev.to/abanand/uploading-downloading-files-from-google-drive-using-python-2pll</link>
      <guid>https://dev.to/abanand/uploading-downloading-files-from-google-drive-using-python-2pll</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Google Drive is the most used cloud storage platform. A need for some minimal yet effective tool to transfer contents to and from Drive is important!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There is a number of such libraries already present with &lt;a href="https://github.com/googleworkspace/PyDrive" rel="noopener noreferrer"&gt;Pydrive&lt;/a&gt; being the most amazing. Pydrive has a lot of functionalities but there are certain major functionalities that it lacks. Especially, when it comes to the most used feature i.e. Bulk uploading/downloading.&lt;br&gt;
Thus with a goal to address the bulk uploading/downloading functionality in a way that is easy-to-use and yet covers all the users’ requirements. I built this library &lt;a href="https://github.com/ab-anand/Zdrive" rel="noopener noreferrer"&gt;Zdrive&lt;/a&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  Zdrive
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;ZDrive is a lightweight and easy-to-use Python library to upload and download content from Google Drive.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let’s explore the feature it provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Upload folders in a specified folder in the Drive maintaining the &lt;strong&gt;same directory structure&lt;/strong&gt; as present locally.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download folders from a specified folder in the Drive to a specified folder in the PC maintaining the &lt;strong&gt;same directory structure&lt;/strong&gt; as present in the Drive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Downloading is done in parallel thus increasing the download speed significantly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can sustain minor network interruptions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download or Upload the whole directory in less than 5 lines of code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Support: * &lt;strong&gt;OS&lt;/strong&gt;: Linux, Windows, Mac * &lt;strong&gt;Language&lt;/strong&gt;: Python 2.x, 3.x&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Pydrive doesn’t enable you to download contents to a specified location. Also while downloading/uploading you can’t maintain the same directory structure as in the input folder.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Maintaining the same directory structure:
&lt;/h3&gt;

&lt;p&gt;Let’s take the case of uploading a folder to Drive!&lt;/p&gt;

&lt;p&gt;Suppose the input folder specified by the user that needs to be uploaded to Drive is: &lt;code&gt;/User/abhinavanand/Documents/parking_lot/&lt;/code&gt; (let’s call it &lt;code&gt;input_folder&lt;/code&gt; ). Also, suppose currently the structure of &lt;code&gt;input_folder&lt;/code&gt; is as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input_folder/
|-- README.md
|-- bin
|   |-- parking_lot
|   |-- parking_lot.sh
|   |-- run_functional_tests
|   |-- setup
|   `-- setup.sh
|-- file_inputs.txt
|-- requirements.txt
|-- src
|   |-- __init__.py
|   |-- lib
|   |   `-- utils.py
|   |   
|   |-- main.py
|   `-- models
|       |-- __init__.py
|       |-- car.py
|       |-- lot.py
|       `-- parking_lot.py
`-- tests
    |-- test_libs.py
    |-- test_main.py
    `-- test_models.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now suppose the user wants this &lt;code&gt;input_folder&lt;/code&gt; to be uploaded to a specific location(folder) in the drive, let’s call it &lt;code&gt;output_folder&lt;/code&gt;. So below is the final directory structure of the &lt;code&gt;output_folder&lt;/code&gt; once the upload completes.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output_folder/
|-- README.md
|-- bin
|   |-- parking_lot
|   |-- parking_lot.sh
|   |-- run_functional_tests
|   |-- setup
|   `-- setup.sh
|-- file_inputs.txt
|-- requirements.txt
|-- src
|   |-- __init__.py
|   |-- lib
|   |   `-- utils.py
|   |   
|   |-- main.py
|   `-- models
|       |-- __init__.py
|       |-- car.py
|       |-- lot.py
|       `-- parking_lot.py
`-- tests
    |-- test_libs.py
    |-- test_main.py
    `-- test_models.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So &lt;strong&gt;Zdrive&lt;/strong&gt; makes sure, that both directory structure is the same so that it is easy for the user to find the files in Drive and not being overwhelmed by the number of contents. Awesome, isn’t it?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vice-versa is done while downloading contents from Drive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, that we know the features of Zdrive, let’s jump into coding and see how easy it is to download and upload content using Zdrive.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initial Setup
&lt;/h2&gt;

&lt;p&gt;Follow the &lt;a href="https://medium.com/swlh/google-drive-api-with-python-part-i-set-up-credentials-1f729cb0372b" rel="noopener noreferrer"&gt;article&lt;/a&gt; to get your drive-API credentials. Once you have the &lt;code&gt;clients-secret.json file&lt;/code&gt;, rename it to &lt;code&gt;credentials.json&lt;/code&gt; and place it in the same folder where you'll be running the script.&lt;/p&gt;
&lt;h2&gt;
  
  
  Downloading
&lt;/h2&gt;

&lt;p&gt;Google Drive is semantic (also called &lt;code&gt;tag-based&lt;/code&gt;) file system meaning it stores files not based on their location, but based on an ID. Thus for performing any action related to a file/folder in Drive, we would need the IDs of the file/folder. Zdrive allows you to retrieve a list of files/folders present inside the Drive along with their IDs.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Downloader&lt;/code&gt; class in Zdrive allows you to download folder/files from the Drive as shown below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: The &lt;code&gt;destinationFolder&lt;/code&gt; is an optional parameter which specifies where the contents downloaded from Drive should go, if the parameter is not provided then Zdrive will create a folder named &lt;code&gt;drive_content&lt;/code&gt; in the folder where the script is run and download the contents there.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Uploading
&lt;/h2&gt;

&lt;p&gt;Similar to Downloader, there is an &lt;code&gt;Uploader&lt;/code&gt; class which allows you to upload folder/files from the local PC to drive. Uploading can be done from any specified location inside the local PC. Data can be uploaded at the ROOT level of the Drive or inside any specific folder in the Drive.&lt;/p&gt;

&lt;p&gt;Since the folder to be uploaded can contain multiple levels of child folders, one inside another which can significantly slow down the process thus the level of child directories to be uploaded can be decided by &lt;code&gt;max_depth&lt;/code&gt; parameter as shown in the example below. Although, this is an optional parameter with a default value of 5.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;Output:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'{
    "files":
        {
            "/Users/abhinavanand/Downloads/test/def.pdf": "1pJNIu-0oyzaUgjLvnf6-3mk81iwLBXyS"
        },
    "folders":
        {
        "/Users/abhinavanand/Downloads/test/test-level-1":
            {
                "files":
                {
                    "/Users/abhinavanand/Downloads/test/test-level-1/abc.pdf": "1YwZs__92yzWdM2e7Nc2atF5lzLnyYV9i"
                },
                "folders": {},
                "id": "1zzh_hGImg94SnzrMC8LdH1vgbO3LMksD"
            }
        }
}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;Note: The &lt;code&gt;parentId&lt;/code&gt; is also an optional parameter, if no &lt;code&gt;parentId&lt;/code&gt; is specified, then &lt;code&gt;Uploader()&lt;/code&gt; would upload the contents from local PC to the ROOT level in Drive. Also, In case of a minor internet interruption(~10–15 secs) the upload would be paused and once the internet connection is stable. The uploading will get resumed.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Since at the time of writing this blog Zdrive is just the initial phase where the goal was to address the major reasons for using a script to automate Google Drive operations, I’m sure there are a lot of improvements that can be done.&lt;/p&gt;

&lt;p&gt;If you guys have found it useful then do drop a ⭐️ (star) to the GitHub Repository. Do raise a PR in case you want to add some more amazing functionalities. Also, if you have an idea then you can just file an enhancement in the issue tracker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub Link:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/ab-anand" rel="noopener noreferrer"&gt;
        ab-anand
      &lt;/a&gt; / &lt;a href="https://github.com/ab-anand/ZDrive" rel="noopener noreferrer"&gt;
        ZDrive
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Seamless download/upload contents via Google Drive  📂
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PyPI Link:&lt;/strong&gt; &lt;a href="https://pypi.org/project/ZDrive" rel="noopener noreferrer"&gt;https://pypi.org/project/ZDrive&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“I will also soon be publishing it on &lt;a href="https://www.geeksforgeeks.org/" rel="noopener noreferrer"&gt;GeeksforGeeks&lt;/a&gt;”&lt;/p&gt;

</description>
      <category>python</category>
      <category>productivity</category>
      <category>googledrive</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
