Skip to content

Commit 1c4917b

Browse files
RajeshGovosqrrrlanuraggoogler
authored
Gmail : Creates a folder and replace the file (googleworkspace#276)
* Creates a folder and replace the file Co-authored-by: Steve Bazyl <[email protected]> Co-authored-by: anuraggoogler <[email protected]>
1 parent 7d6ebda commit 1c4917b

2 files changed

Lines changed: 116 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', 'v2', 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().insert(body=file_metadata,
45+
fields='id').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: 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', 'v2', credentials=creds)
40+
file_id = real_file_id
41+
media = MediaFileUpload('photo.jpg',
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='1M4xjYwPynOk5TsIWN7hcGYkFdBkPTd5F')
59+
# [END drive_upload_revision]

0 commit comments

Comments
 (0)