How to Upload File to Mediafire Using Script

When working on big data projects, extracting data from Google drive into orchestration workflows to initially store the data in data lakes followed by a series of operations like data validation, cleansing, and transformation is widely used to get together business concern insights from the data. In this Python Sample Lawmaking, nosotros are going to upload files to google drive using python and use them in data catamenia orchestration processes.

Steps for Uploading files on Google Drive using Python

Table of Contents

  • Steps for Uploading files on Google Drive using Python
    • Pre-Requisites
    • Step 1: Import the libraries
    • Step 2: OAuth made easy
    • Step three : Upload files to your Google Drive
    • Footstep 4 : List out files from Google Drive
    • Stride v : Download the files from Google Drive
    • Step 6 : Create the Text files in Google Drive
    • Step 7: Read the content of the text file directly from Google Bulldoze

Pre-Requisites

  • Install the pydrive python module as follows: pip install pydrive
  • The below codes can exist run in Jupyter notebook or any python panel

Become Closer To Your Dream of Becoming a Data Scientist with 70+ Solved Terminate-to-End ML Projects

Step 1: Import the libraries

from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive

Footstep 2: OAuth made easy

Follow the steps to Go Authentication for Google Service API in the below link: Get Authentication for Google Service API

Download client_secrets.json from Google API Panel and OAuth2.0 is done in ii lines. Y'all tin customize the behavior of OAuth2 in one settings file settings.yaml

gauth = GoogleAuth() drive = GoogleDrive(gauth)

To a higher place steps together as follows :

from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive gauth = GoogleAuth() drive = GoogleDrive(gauth)

Step 3 : Upload files to your Google Drive

upload_file_list = ['1.jpg', '2.jpg'] for upload_file in upload_file_list: gfile = drive.CreateFile({'parents': [{'id': '1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t'}]}) # Read file and set it as the content of this instance. gfile.SetContentFile(upload_file) gfile.Upload() # Upload the file.

Output of the above code:

  • The above lawmaking uploads my 2 local files 1.jpg and 2.jpg to my Google Drive folder test/. To practice that, the pydrive library will create two files in Google Drive and then read and upload the two files to the respective folder.
  • Notation that we demand to provide the id of the corresponding Google Drive folder. In this instance, the test binder'south ID is 1pzschX3uMbxU0lB5WZ6IlEEeAUE8MZ-t. You tin can get the Google Drive folder ID from the browser.
  • For example: when we open the test folder in my Google Bulldoze, the browser shows the accost as https://drive.google.com/drive/folders/1cIMiqUDUNldxO6Nl-KVuS9SV-cWi9WLi. Then the respective ID for the test folder is the part afterwards the last \ symbol, which is 1cIMiqUDUNldxO6Nl-KVuS9SV-cWi9WLi.

Step 4: List out files from Google Drive

We tin besides list all files from the specific folder in google bulldoze as follows :

file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format('1cIMiqUDUNldxO6Nl-KVuS9SV-cWi9WLi')}).GetList() for file in file_list: impress('championship: %s, id: %southward' % (file['title'], file['id']))

Output of the above lawmaking:

Step 5: Download the files from Google Drive

We can also download the files from Google Drive as follows. Note - after listing the files merely we tin download the file.

for i, file in enumerate(sorted(file_list, cardinal = lambda x: x['title']), start=1): impress('Downloading {} file from GDrive ({}/{})'.format(file['title'], i, len(file_list))) file.GetContentFile(file['championship'])

The output of the above code:

In the above snapshot files are downloaded from the specific folder, Notation here files volition download where the lawmaking will be executed.

Step 6: Create the Text files in Google Bulldoze

We can also write files directly to Google Bulldoze using the following code:

# Create a GoogleDriveFile case with title 'test.txt'. file1 = bulldoze.CreateFile({'parents': [{'id': '1cIMiqUDUNldxO6Nl-KVuS9SV-cWi9WLi'}],'title': 'test.txt'}) # Set content of the file from the given string. file1.SetContentString('Hello World!') file1.Upload()

The output of the to a higher place code: test.txt file is created in google drive.

Step 7: Read the content of the text file straight from Google Drive

Also, nosotros tin read the file directly from Google Drive using the beneath code :

file2 = drive.CreateFile({'id': file1['id']}) file2.GetContentString('test.txt')

The output of the in a higher place code:

In the above snapshot reading the content of the file as "Hullo world"

clemonshingthat.blogspot.com

Source: https://www.projectpro.io/recipes/upload-files-to-google-drive-using-python

Belum ada Komentar untuk "How to Upload File to Mediafire Using Script"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel