Skip to content

Commit eb3655e

Browse files
Drive-v3 : file snippet (googleworkspace#302)
* File snippet test cases * Drive: file snippet Co-authored-by: anuraggoogler <[email protected]>
1 parent 62c36ea commit eb3655e

8 files changed

Lines changed: 477 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_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 create_folder():
26+
""" Create a folder and prints the folder ID
27+
Returns : Folder Id
28+
29+
Load pre-authorized user credentials from the environment.
30+
TODO(developer) - See https://developers.google.com/identity
31+
for guides on implementing OAuth2 for the application.
32+
"""
33+
creds, _ = google.auth.default()
34+
35+
try:
36+
# create gmail api client
37+
service = build('drive', 'v3', credentials=creds)
38+
file_metadata = {
39+
'title': 'Invoices',
40+
'mimeType': 'application/vnd.google-apps.folder'
41+
}
42+
43+
# pylint: disable=maybe-no-member
44+
file = service.files().create(body=file_metadata, fields='id'
45+
).execute()
46+
print(F'Folder has created with ID: "{file.get("id")}".')
47+
48+
except HttpError as error:
49+
print(F'An error occurred: {error}')
50+
file = None
51+
52+
return file.get('id')
53+
54+
55+
if __name__ == '__main__':
56+
create_folder()
57+
# [END drive_create_folder]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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', 'v3', credentials=creds)
37+
file_metadata = {
38+
'title': 'Project plan',
39+
'mimeType': 'application/vnd.google-apps.drive-sdk'
40+
}
41+
42+
# pylint: disable=maybe-no-member
43+
file = service.files().create(body=file_metadata,
44+
fields='id').execute()
45+
print(F'File ID: {file.get("id")}')
46+
47+
except HttpError as error:
48+
print(F'An error occurred: {error}')
49+
return file.get('id')
50+
51+
52+
if __name__ == '__main__':
53+
create_shortcut()
54+
# [END drive_create_shortcut]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_search_file]
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 search_file():
26+
"""Search file in drive location
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', 'v3', credentials=creds)
37+
files = []
38+
page_token = None
39+
while True:
40+
# pylint: disable=maybe-no-member
41+
response = service.files().list(q="mimeType='image/jpeg'",
42+
spaces='drive',
43+
fields='nextPageToken, '
44+
'files(id, name)',
45+
pageToken=page_token).execute()
46+
for file in response.get('files', []):
47+
# Process change
48+
print(F'Found file: {file.get("name")}, {file.get("id")}')
49+
files.extend(response.get('files', []))
50+
page_token = response.get('nextPageToken', None)
51+
if page_token is None:
52+
break
53+
54+
except HttpError as error:
55+
print(F'An error occurred: {error}')
56+
files = None
57+
58+
return files
59+
60+
61+
if __name__ == '__main__':
62+
search_file()
63+
# [END drive_search_file]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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', 'v3', credentials=creds)
43+
44+
file_metadata = {
45+
'modifiedTime': datetime.utcnow().isoformat() + 'Z'
46+
}
47+
# pylint: disable=maybe-no-member
48+
file_id = real_file_id
49+
file_metadata['modifiedTime'] = real_timestamp
50+
file = service.files().update(fileId=file_id, body=file_metadata,
51+
fields='id, modifiedTime').execute()
52+
print(F'Modified time: {file.get("modifiedTime")}')
53+
54+
except HttpError as error:
55+
print(F'An error occurred: {error}')
56+
file = None
57+
58+
return file.get('modifiedDate')
59+
60+
61+
if __name__ == '__main__':
62+
touch_file(real_file_id='17EqlSf7FpPU95SS00sICyVzQHpeET1cz',
63+
real_timestamp='2022-03-02T05:43:27.504Z')
64+
# [END drive_touch_file]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_upload_basic]
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+
from googleapiclient.http import MediaFileUpload
24+
25+
26+
def upload_basic():
27+
"""Insert new file.
28+
Returns : Id's of the file uploaded
29+
30+
Load pre-authorized user credentials from the environment.
31+
TODO(developer) - See https://developers.google.com/identity
32+
for guides on implementing OAuth2 for the application.
33+
"""
34+
creds, _ = google.auth.default()
35+
36+
try:
37+
# create gmail api client
38+
service = build('drive', 'v3', credentials=creds)
39+
40+
file_metadata = {'name': 'download.jpeg'}
41+
media = MediaFileUpload('download.jpeg',
42+
mimetype='image/jpeg')
43+
# pylint: disable=maybe-no-member
44+
file = service.files().create(body=file_metadata, media_body=media,
45+
fields='id').execute()
46+
print(F'File ID: {file.get("id")}')
47+
48+
except HttpError as error:
49+
print(F'An error occurred: {error}')
50+
file = None
51+
52+
return file.get('id')
53+
54+
55+
if __name__ == '__main__':
56+
upload_basic()
57+
# [END drive_upload_basic]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_upload_revision]
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+
from googleapiclient.http import MediaFileUpload
24+
25+
26+
def upload_revision(real_file_id):
27+
"""Replace the old file with new one on same file ID
28+
Args: ID of the file to be replaced
29+
Returns: file ID
30+
31+
Load pre-authorized user credentials from the environment.
32+
TODO(developer) - See https://developers.google.com/identity
33+
for guides on implementing OAuth2 for the application.
34+
"""
35+
creds, _ = google.auth.default()
36+
37+
try:
38+
# create gmail api client
39+
service = build('drive', 'v3', credentials=creds)
40+
file_id = real_file_id
41+
media = MediaFileUpload('download.jpeg',
42+
mimetype='image/jpeg',
43+
resumable=True)
44+
# pylint: disable=maybe-no-member
45+
file = service.files().update(fileId=file_id,
46+
body={},
47+
media_body=media,
48+
fields='id').execute()
49+
print(F'File ID: {file.get("id")}')
50+
51+
except HttpError as error:
52+
print(F'An error occurred: {error}')
53+
54+
return file.get('id')
55+
56+
57+
if __name__ == '__main__':
58+
upload_revision(real_file_id='1jJTiihczk_xSNPVLwMySQBJACXYdpGTi')
59+
# [END drive_upload_revision]

0 commit comments

Comments
 (0)