-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_gdrive_file.py
83 lines (65 loc) · 2.69 KB
/
copy_gdrive_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
This script is used to copy a specified template file to a specified destination folder in Google Drive.
"""
import sys
import helpers.imports as helpers
from apiclient import errors
def copy_file_request(service, origin_file_id, file_parent_id, file_name):
"""
Creates and executes a request to copy a file to a specified directory.
:param service: Google Drive v3 authentication object.
:param origin_file_id: string id of original file to copy.
:param file_parent_id: string id of folder to copy file to.
:param file_name: string name for newly copied file.
:return: copied file, if successful. none otherwise.
"""
# setup request body
copy_request_body = {
'name': file_name,
'parents': [file_parent_id]
}
# attempt to copy file
try:
return service.files().copy(fileId=origin_file_id, body=copy_request_body).execute()
except errors.HttpError as error:
print('An error occurred: {}'.format(error))
# return none if file copy failed
return None
def copy_file(service, file_url, folder_url, file_name):
"""
Copies a file to a specified direction given a file and folder url.
:param service: Google Drive v3 authentication object.
:param file_url: string url of original file to copy.
:param folder_url: string url of folder to copy file to.
:param file_name: string name for newly copied file.
:return: copied file, if successful. none otherwise.
"""
# parse out file and folder ids for specified URLs
file_id = helpers.get_file_id_from_url(file_url)
folder_id = helpers.get_folder_id_from_url(folder_url)
return copy_file_request(service, file_id, folder_id, file_name)
def main(file_url, folder_url, file_name):
"""
Generates auth token and copies file.
:param file_url: string url of original file to copy.
:param folder_url: string url of folder to copy file to.
:param file_name: string name for newly copied file.
:return: copied file, if successful. none otherwise.
"""
# auth client
service = helpers.auth_gdrive()
# attempt to make a file copy
return copy_file(service, file_url, folder_url, file_name)
if __name__ == '__main__':
# get command line args
arg_count = len(sys.argv) - 1
# check for correct number of arguments
if arg_count != 3:
raise Exception("Invalid number of arguments. Expected 3 (file URL, folder URL, file name) got {}."
.format(arg_count))
# parse each argument
input_file_url = sys.argv[1]
input_folder_url = sys.argv[2]
input_file_name = sys.argv[3]
# copy file to destination
main(input_file_url, input_folder_url, input_file_name)