Skip to content

Commit d2fc30b

Browse files
RajeshGovosqrrrlanuraggoogler
authored
Drive: create shortcut, move file to folder and touch file (#281)
* Added pull request template Added pull request template * updated header updated header * Update pull_request_template.md * Update pull_request_template.md * Add unit/integration test to checklist * Gmail snippets original code from devral repo. * Gmail snippets original code from devral repo. * Create and send an email message with and without attachment * Update send_message.py * Update send_message_with_attachment.py * Update send_message.py * Delete send_message.py * Delete send_message_with_attachment.py * Create an smimeInfo resource for a certificate from file * Upload an S/MIME certificate for the user * delint * Delint * Updated arguments * Insert new file * Downloads a file and Download document file in PDF format * Create a third party shortcut * Move specified file to the specified folder * Change the file's modification timestamp Co-authored-by: Steve Bazyl <[email protected]> Co-authored-by: anuraggoogler <[email protected]>
1 parent 5b67c7c commit d2fc30b

3 files changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
# [START drive_create_shortcut]
17+
18+
from __future__ import print_function
19+
20+
import google.auth
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
23+
24+
25+
def create_shortcut():
26+
"""Create a third party shortcut
27+
28+
Load pre-authorized user credentials from the environment.
29+
TODO(developer) - See https://developers.google.com/identity
30+
for guides on implementing OAuth2 for the application.
31+
"""
32+
creds, _ = google.auth.default()
33+
34+
try:
35+
# create gmail api client
36+
service = build('drive', 'v2', credentials=creds)
37+
file_metadata = {
38+
'title': 'Project plan',
39+
'mimeType': 'application/vnd.google-apps.drive-sdk'
40+
}
41+
# pylint: disable=maybe-no-member
42+
file = service.files().insert(body=file_metadata,
43+
fields='id').execute()
44+
print(F'File ID: {file.get("id")}')
45+
46+
except HttpError as error:
47+
print(F'An error occurred: {error}')
48+
return file.get('id')
49+
50+
51+
if __name__ == '__main__':
52+
create_shortcut()
53+
# [END drive_create_shortcut]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Copyright 2022 Google LLC
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
"""
15+
16+
# [START drive_move_file_to_folder]
17+
18+
from __future__ import print_function
19+
20+
import google.auth
21+
from googleapiclient.discovery import build
22+
from googleapiclient.errors import HttpError
23+
24+
25+
def move_file_to_folder(real_file_id, real_folder_id):
26+
"""Move specified file to the specified folder.
27+
Args:
28+
real_file_id: Id of the file to move.
29+
real_folder_id: Id of the folder
30+
Print: An object containing the new parent folder and other meta data
31+
32+
Load pre-authorized user credentials from the environment.
33+
TODO(developer) - See https://developers.google.com/identity
34+
for guides on implementing OAuth2 for the application.
35+
"""
36+
creds, _ = google.auth.default()
37+
38+
try:
39+
# call drive api client
40+
service = build('drive', 'v2', credentials=creds)
41+
42+
file_id = real_file_id
43+
folder_id = real_folder_id
44+
45+
# Retrieve the existing parents to remove
46+
# pylint: disable=maybe-no-member
47+
file = service.files().get(fileId=file_id, fields='parents').execute()
48+
previous_parents = ",".join(
49+
[parent["id"] for parent in file.get('parents')])
50+
# Move the file to the new folder
51+
file = service.files().update(fileId=file_id, addParents=folder_id,
52+
removeParents=previous_parents,
53+
fields='id, parents').execute()
54+
new_parent_folder_id = [parent["id"] for parent in file.get('parents')]
55+
print(F'file with ID : {file.get("id")} has moved to folder : '
56+
F'{new_parent_folder_id}')
57+
58+
except HttpError as error:
59+
print(F'An error occurred: {error}')
60+
61+
return [parent["id"] for parent in file.get('parents')]
62+
63+
64+
if __name__ == '__main__':
65+
move_file_to_folder(real_file_id='14fesChjgzDA7lUu9ZeGqXOuXMPgaVkxS',
66+
real_folder_id='1KzT9gjq-AHfciwNzKjh7nUd6prrQOA4')
67+
# [END drive_move_file_to_folder]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Copyright 2022 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
# [START drive_touch_file]
17+
18+
from __future__ import print_function
19+
20+
from datetime import datetime
21+
22+
import google.auth
23+
from googleapiclient.discovery import build
24+
from googleapiclient.errors import HttpError
25+
26+
27+
def touch_file(real_file_id, real_timestamp):
28+
"""Change the file's modification timestamp.
29+
Args:
30+
real_file_id: ID of the file to change modified time
31+
real_timestamp: Timestamp to override Modified date time of the file
32+
Returns : Modified Date and time.
33+
34+
Load pre-authorized user credentials from the environment.
35+
TODO(developer) - See https://developers.google.com/identity
36+
for guides on implementing OAuth2 for the application.
37+
"""
38+
creds, _ = google.auth.default()
39+
40+
try:
41+
# create gmail api client
42+
service = build('drive', 'v2', credentials=creds)
43+
44+
file_metadata = {
45+
'modifiedDate': datetime.utcnow().isoformat() + 'Z'
46+
}
47+
48+
file_id = real_file_id
49+
file_metadata['modifiedDate'] = real_timestamp
50+
# pylint: disable=maybe-no-member
51+
file = service.files().update(fileId=file_id, body=file_metadata,
52+
setModifiedDate=True,
53+
fields='id, modifiedDate').execute()
54+
print(F'Modified time: {file.get("modifiedDate")}')
55+
56+
except HttpError as error:
57+
print(F'An error occurred: {error}')
58+
file = None
59+
60+
return file.get('modifiedDate')
61+
62+
63+
if __name__ == '__main__':
64+
touch_file(real_file_id='1KuPmvGq8yoYgbfW74OENMCB5H0n_2Jm9',
65+
real_timestamp='2022-03-02T05:43:27.504Z')
66+
# [END drive_touch_file]

0 commit comments

Comments
 (0)