Make a copy of a Google Drive file from Python

Introduction

Due to some circumstances, it became necessary to automatically duplicate the files on Google Drive, so this is a memo that investigated how to do it.

Version

Method

Preparation: Enable Google Drive API

http://wwld.jp/2015/11/07/spreadsheet-api.html As introduced in, you need to enable the Drive API, so enable it.

Preparation: Get JSON for authentication

Credentials are required as it uses the Google Drive API. The method of obtaining authentication information seems to change little by little depending on the time, I think that it is generally okay to use a method like the above site http://wwld.jp/2015/11/07/spreadsheet-api.html.

Sample code

SECRET_JSON = '<PATH_TO_SECRET_JSON>'
FILE_ID = '<FILE ID on Google Drive>'

# ---- get credentials

from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds', 
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(SECRET_JSON, scope)


# ---- create Google Drive Service

from apiclient.discovery import build
import httplib2

http = httplib2.Http()
http = credentials.authorize(http)
service = build('drive', 'v3', http=http)

# ---- Copy the file on Google Drive

new_file_body = {
    'name': 'new_data',  #File name of the new file.Can be omitted
    'parents': ['0Bxxxx-abcdefghijklmnopqrstu'],  #Folder ID of the copy destination.Can be omitted
}

# ref: https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html#copy

new_file = service.files().copy(
    fileId=FILE_ID, body=new_file_body
).execute()
print(new_file)

Output example

{
  'kind': 'drive#file', 
  'mimeType': 'application/vnd.google-apps.spreadsheet', 
  'name': 'new_data', 
  'id': '1zzzzzzzzzzz-xxxxxxxxxxxxxx-xxxxxxxxx'
}

at the end

I believe I can do it, and once it's done, it's easy, but it's hard to find a way. ..

Recommended Posts

Make a copy of a Google Drive file from Python
Make a copy of the list in Python
python / Make a dict from a list.
Python> Read from a multi-line string instead of a file> io.StringIO ()
Python: Extract file information from shared drive with Google Drive API
Make a relation diagram of Python module
Copy of python
Create a deb file from a python package
A memorandum of calling Python from Common Lisp
Make a joyplot-like plot of R in python
Run a Python file from html using Django
Read line by line from a file with Python
Google Drive file move
Copy of python preferences
Around the authentication of PyDrive2, a package that operates Google Drive with Python
Upload JPG file using Google Drive API in Python
I tried running python etc. from a bat file
Installation of TensorFlow, a machine learning library from Google
Python points from the perspective of a C programmer
Python script to create a JSON file from a CSV file
[Python] Start a batch file from Python and pass variables.
Make the initial directory of JupyterLab a Google Drive mounted on an external HDD
Make MeCab available from Python3
Make a bookmarklet in Python
Make a fortune with Python
[Python] Copy of multidimensional list
Access Google Drive with Python
Google Drive Api Tips (Python)
Extract template of EML file saved from Thunderbird with python3.7
[Python] How to make a list of character strings character by character
Make a table of multiplication of each element in a spreadsheet (Python)
Make a gif animation from a serial number file with matplotlib
Different from the import type of python. from A import B meaning
I want to start a lot of processes from python
[Note] Import of a file in the parent directory in Python
Python: Create a dictionary from a list of keys and values
Google search for the last line of the file in Python
Extract only the sound of a specific instrument from a MIDI file and make it a separate file
A record of patching a python package
Execute Python script from batch file
Call a Python function from p5.js.
Create an instance of a predefined class from a string in Python
A good description of Python decorators
Make a Santa classifier from a Santa image
Touch a Python object from Elixir
Easy encryption of file contents (Python)
Let's make a GUI with python.
I tried to make a regular expression of "amount" using Python
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
I tried to make a regular expression of "time" using Python
Make a cat detector with Google Colabratory (Part 2) [Python] ~ Use OpenCV ~
Existence from the viewpoint of Python
Download Google Drive files in Python
Create a binary file in Python
From a book that programmers can learn ... (Python): Review of arrays
I tried to make a regular expression of "date" using Python
Forcibly use Google Translate from python
[Python] Get the update date of a news article from HTML
[Python] Make the function a lambda function
Make a recommender system with python