create a folder in google drive and uploading files to it - python

How I can create a folder inside the google drive and upload files into that folder? (in python)
I have tried with the google tutorials but it is giving me errors
creating a folder, not working giving me errors like 'drive_service not defined'
file_metadata = {
'title': 'Files',
'mimeType': 'application/vnd.google-apps.folder'
}
file = drive_service.files().insert(body=file_metadata,
fields='id').execute()
print 'Folder ID: %s' % file.get('id')
Used this code for uploading a file and it is working, how I can modify it for uploading a file into a folder if that exists, if not create one and upload.
import json
import requests
headers = {"Authorization": "Bearer Token"}
para = {
"name": "index.jpeg",
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open("./index.jpeg", "rb")
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)

Here is an example of what I think you're looking for (its a self-contained example, but I think you can modify it to your own existing code):
from __future__ import print_function
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from httplib2 import Http
from oauth2client import file, client, tools
# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/drive.file'
def main():
# Access the Drive service
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
# Check if folder exists (simply matching by name)
folder_name = "Photos"
folder_id = None
query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='" + folder_name + "'"
results = service.files().list(
pageSize=1, q=query, fields="files(id, name)").execute()
folders = results.get('files', [])
if folders:
folder_id = folders[0]['id']
# If folder not found, then create it.
else:
file_metadata = {
'name': folder_name,
'mimeType': 'application/vnd.google-apps.folder'
}
folder_file = service.files().create(body=file_metadata,
fields='id').execute()
folder_id = folder_file.get('id')
# Add file to folder.
file_metadata = {
'name': 'photo.png',
'parents': [folder_id]
}
media = MediaFileUpload('photo.png',
mimetype='image/png',
resumable=True)
image_file = service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
if __name__ == '__main__':
main()

Related

How to upload the files to google drive only once?

Using this code, I am uploading the files to google drive successfully. But each time when I run this code, it uploads the same files to google drive again. What I want is to stop this repetition of uploading the data and just upload the newly created file.
import os
import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
http = httplib2.Http()
SCOPES = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('thermal-highway.json', SCOPES)
drive_service = build('drive', 'v3', http=credentials.authorize(http))
def upload_folders(name, folder_id):
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder',
'parents': folder_id
}
file = drive_service.files().create(body=file_metadata, fields='id').execute()
print('Folder ID: %s' % file.get('id'))
def list_files(path):
folderId = ['62bewyZXHy9JfSLG9N96U1e548luuCAWR']
for files in os.listdir(path):
d = os.path.join(path, files)
if os.path.isdir(d):
upload_folders(files, folderId)
path = "/home/bilal/Videos/folder1/"
list_files(path)
Your code uses file.create which creates a new file each time. If you want to update an existing file then you should be using file.update
# File's new content.
media_body = MediaFileUpload(
new_filename, mimetype=new_mime_type, resumable=True)
# Send the request to the API.
updated_file = service.files().update(
fileId=file_id,
body=file,
media_body=media_body).execute()

How can I insert/upload files to a specific folder in Google Drive API v3 using Python

Everyday I download many pdf files which are stored onto my local PC. To store all of them, I need to create a folder onto Google drive daily basis and store all the pdf files under the current date folder.
My challenge over here is I have successfully completed coding in Python to create folder using GDrive API v3 but stuck in uploading all the files onto the folder just created. Below is my coding how I achieved to create folder by current date:
import pickle
import os.path, time
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient.http import MediaFileUpload
from datetime import date
SCOPES = ['https://www.googleapis.com/auth/drive.file']
CLIENT_SECRET_FILE = 'e:\\Python Programs\\credentials.json'
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
# main folder ID under which we are going to create subfolder
parents_ID = '19nKlHGCypKPr40f3vjaEq22kgVkS7OCE'
# creates sub folder under main folder ID
fldr_name = date.today().strftime('%d%b%Y')
mimetype = 'application/vnd.google-apps.folder'
file_metadata = {'name': fldr_name, 'parents': [parents_ID],'mimeType': mimetype}
# with following line I could successfully create folder without any problem
service.files().create(body=file_metadata, fields='id').execute()
# with following lines, i tried to get the folder ID which was recently created so that I can start coding to upload pdf files onto this. Here im stuck
page_token = None
response = service.files().list(q="mimeType='application/vnd.google-apps.folder' and name = '27Apr2020' and trashed = false", spaces='drive', fields='nextPageToken, files(id, name)', pageToken=page_token).execute()
for file in response.get('files', []):
print(file.get('name'), file.get('id'))
time.sleep(5)
I am getting blank screen for 5 sec then it gets vanished. Please help me to upload all my files to my recently created folder.
Thanks
From your above Code, the following line already returns the id of the folder just created(fields='id').
service.files().create(body=file_metadata, fields='id').execute()
you can also return other values (name, parents, etc.) using the fields parameter while creating the folder,
service.files().create(body=file_metadata, fields='id, name, parents').execute()
so if you catch the return value in a variable like below,
folder = service.files().create(body=file_metadata, fields='id').execute()
the returned value looks like {'id': '17w2RS1H7S8no6X0oGtkieY'}
you can then upload file by the commands below.
file_metadata = {
'name': "test.pdf", <Name of the file to be in drive>
'parents': [folder["id"]],
'mimeType': 'application/pdf'
}
media = MediaFileUpload(<local path to file>, resumable=True)
file = service.files().create(body=file_metadata, media_body=media, fields='id,name').execute()
you need to import the following,
from googleapiclient.http import MediaFileUpload

Add folder if it doesn't exist and add file in Google Drive in Python

I'm trying to create a folder if it doesn't exist, and then add a file under that directory. The script below runs without errors and seems successful, however no folders are created and no files added to the gdrive.
def SyncDrive(newFile, parentFolderId, subFolder):
#auth stuff here
SCOPES = 'https://www.googleapis.com/auth/drive'
credentials = ServiceAccountCredentials.from_json_keyfile_name('myserver.json', SCOPES)
http_auth = credentials.authorize(Http())
DRIVE = discovery.build('drive', 'v3', http=http_auth)
# create subfolder if it doesn't exist
subdir_metadata = {
'name': subFolder,
'mimeType': 'application/vnd.google-apps.folder',
'parents':[{'id': parentFolderId}]
}
result = DRIVE.files().create(body=subdir_metadata,fields='id').execute()
subdir_id = result.get('id')
file_metadata = {
'name': newFile,
'parents': [{'id': subdir_id}]
}
result = DRIVE.files().create(body=file_metadata, media_body=file_path, fields='id').execute()

How to upload file to google drive with service account credential

I wanted to upload the files to my Google Drive using a Google service account credentials.
I downloaded credential as a JSON file from Google Developer Console, and got credential from it.
Here is my code snippet.
google_drive_service = discovery.build('drive', 'v3',
credentials=ServiceAccountCredentials.from_json_keyfile_name
os.path.join(settings.CLIENT_PATH, settings.CLIENT_SECRET_FILE),
scopes=settings.SCOPES))
media = MediaFileUpload(tmp_file_path, mimetype=tmp_file.content_type, resumable=True)
google_drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
The code runs and there is no error, however I can't find out the files uploaded to my Google Drive account. I am not sure why files are not uploaded. Would you like to help me to fix this problem?
The issue you are having is that a service account is not you. you have uploaded a file to the service accounts Google drive account not your personal drive account. Try doing a file list you should see the file.
Suggestion. Take the service account email address and share a directory on your personal Google drive account with it like you would share with any other user. The Service account will then be able to upload to this directory. Just make sure to set the permissions on the file after you upload it granting your personal drive account access to the file. When the file is uploaded it will be owned by the service account.
Try this Python terminal client for Google Drive for easy uploading, deleting, listing, sharing files or folders.
client_secret.json
{"installed":{"client_id":"698477346386-5kbs1fh3c6eu46op4qvf30ehp6md8o56.apps.googleusercontent.com","project_id":"proven-dryad-122714","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"9j4oMk4HI0ZyPvQrz0jFFA4q","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}}
GDrive.py
from __future__ import print_function
import sys
import io
import pip
import httplib2
import os
from mimetypes import MimeTypes
try:
from googleapiclient.errors import HttpError
from apiclient import discovery
import oauth2client
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from oauth2client import client
from oauth2client import tools
except ImportError:
print('goole-api-python-client is not installed. Try:')
print('sudo pip install --upgrade google-api-python-client')
sys.exit(1)
import sys
class Flag:
auth_host_name = 'localhost'
noauth_local_webserver = False
auth_host_port = [8080, 8090]
logging_level = 'ERROR'
try:
import argparse
# flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
flags = Flag()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'GDrive'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
# if flags:
credentials = tools.run_flow(flow, store, flags)
# else: # Needed only for compatibility with Python 2.6
# credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def upload(path, parent_id=None):
mime = MimeTypes()
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)
file_metadata = {
'name': os.path.basename(path),
# 'mimeType' : 'application/vnd.google-apps.spreadsheet'
}
if parent_id:
file_metadata['parents'] = [parent_id]
media = MediaFileUpload(path,
mimetype=mime.guess_type(os.path.basename(path))[0],
resumable=True)
try:
file = service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
except HttpError:
print('corrupted file')
pass
print(file.get('id'))
def share(file_id, email):
def callback(request_id, response, exception):
if exception:
# Handle error
print(exception)
else:
print(response.get('id'))
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)
batch = service.new_batch_http_request(callback=callback)
user_permission = {
'type': 'user',
'role': 'reader',
'emailAddress': email
}
batch.add(service.permissions().create(
fileId=file_id,
body=user_permission,
fields='id',
))
batch.execute()
def listfiles():
results = service.files().list(fields="nextPageToken, files(id, name,mimeType)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
print('Filename (File ID)')
for item in items:
print('{0} ({1})'.format(item['name'].encode('utf-8'), item['id']))
print('Total=', len(items))
def delete(fileid):
service.files().delete(fileId=fileid).execute()
def download(file_id, path=os.getcwd()):
request = service.files().get_media(fileId=file_id)
name = service.files().get(fileId=file_id).execute()['name']
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(int(status.progress() * 100))
f = open(path + '/' + name, 'wb')
f.write(fh.getvalue())
print('File downloaded at', path)
f.close()
def createfolder(folder, recursive=False):
if recursive:
print('recursive ON')
ids = {}
for root, sub, files in os.walk(folder):
par = os.path.dirname(root)
file_metadata = {
'name': os.path.basename(root),
'mimeType': 'application/vnd.google-apps.folder'
}
if par in ids.keys():
file_metadata['parents'] = [ids[par]]
print(root)
file = service.files().create(body=file_metadata,
fields='id').execute()
id = file.get('id')
print(id)
ids[root] = id
for f in files:
print(root+'/'+f)
upload(root + '/' + f, id)
else:
print('recursive OFF')
file_metadata = {
'name': os.path.basename(folder),
'mimeType': 'application/vnd.google-apps.folder'
}
file = service.files().create(body=file_metadata,
fields='id').execute()
print(file.get('id'))
if __name__ == '__main__':
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)
method = sys.argv[1]
if method == 'upload':
if os.path.isdir(sys.argv[2]):
if len(sys.argv) == 4 and sys.argv[3] == 'R':
createfolder(sys.argv[2], True)
else:
createfolder(os.path.basename(sys.argv[2]))
else:
upload(sys.argv[2])
elif method == 'list':
listfiles()
elif method == 'delete':
delete(sys.argv[2])
elif method == 'download':
download(sys.argv[2], sys.argv[3])
elif method == 'share':
share(sys.argv[2], sys.argv[3])
elif method == 'folder':
createfolder(sys.argv[2])
elif method == 'debug':
print(os.getcwd())

Create Google Sheet in Python

I am using the Google Drive API to create a .csv, and I see it in my "Drive" display but I don't see it in "Sheets." How can I make it show in Sheets? Here is my code:
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)
results = service.files().create(body={"name":"Test7.csv"}, media_body='/tmp/inputfile.csv', keepRevisionForever=None, useContentAsIndexableText=None, supportsTeamDrives=None, ocrLanguage=None, ignoreDefaultVisibility=None).execute()
Looks like you are just adding a CSV file to Drive.
You need to specify the mime type as Google Spreadsheet:
from apiclient.http import MediaFileUpload
file_metadata = {
'name' : 'My Report',
'mimeType' : 'application/vnd.google-apps.spreadsheet'
}
media = MediaFileUpload('test7.csv',
mimetype='text/csv',
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
https://developers.google.com/drive/v3/web/manage-uploads#importing_to_google_docs_types_wzxhzdk18wzxhzdk19

Categories

Resources