Skip to content

Instantly share code, notes, and snippets.

@misirov
Last active January 2, 2025 16:44
Show Gist options
  • Save misirov/2a24648896b4f9299bea28d3292f0089 to your computer and use it in GitHub Desktop.
Save misirov/2a24648896b4f9299bea28d3292f0089 to your computer and use it in GitHub Desktop.
upload files to gdrive with python
"""Google Drive File Upload Utility
Required auth files:
- credentials.json: OAuth 2.0 Client credentials from Google Cloud Console
• Go to Google Cloud Console (https://console.cloud.google.com)
• Create a new project or select existing
• Enable Google Drive API
• Configure OAuth consent screen
• Go to Credentials → Create Credentials → OAuth Client ID
• Select "Web Application"
• Download JSON and rename to credentials.json
- auth.json: OAuth tokens obtained through authentication flow
• Use Google OAuth 2.0 Playground (https://developers.google.com/oauthplayground)
• Configure with your client ID and secret
• Select Drive API v3 scope: https://www.googleapis.com/auth/drive.file
• Exchange authorization code for tokens
• Save refresh_token and access_token in auth.json format
"""
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import os
import json
def setup_google_drive():
"""Setup Google Drive API using existing auth tokens"""
# Load credentials and tokens
with open('credentials.json', 'r') as f:
cred_data = json.load(f)['web']
with open('auth.json', 'r') as f:
token_data = json.load(f)
# Create credentials object
creds = Credentials(
token=token_data['access_token'],
refresh_token=token_data['refresh_token'],
token_uri=cred_data['token_uri'],
client_id=cred_data['client_id'],
client_secret=cred_data['client_secret'],
scopes=['https://www.googleapis.com/auth/drive.file']
)
return build('drive', 'v3', credentials=creds)
def upload_file_to_drive(file_path: str, file_type: str) -> str:
"""Upload single file and return its URL"""
service = setup_google_drive()
file_metadata = {'name': os.path.basename(file_path)}
media = MediaFileUpload(file_path, mimetype=f'image/{file_type}')
file = service.files().create(
body=file_metadata,
media_body=media,
fields='id'
).execute()
# Make file public
service.permissions().create(
fileId=file['id'],
body={'type': 'anyone', 'role': 'reader'},
fields='id'
).execute()
return f"https://drive.google.com/file/d/{file['id']}/view"
def process_directory(directory_path: str, file_type: str):
"""Process all files in directory and save URLs to file"""
results_dir = "results"
if not os.path.exists(results_dir):
os.makedirs(results_dir)
with open(os.path.join(results_dir, 'file_urls.txt'), 'w') as url_file:
for filename in os.listdir(directory_path):
if filename.endswith('.file'):
file_path = os.path.join(directory_path, filename)
try:
url = upload_file_to_drive(file_path, file_type)
print(f"Uploaded {filename}: {url}")
url_file.write(f"{filename}: {url}\n")
except Exception as e:
error_msg = f"Failed to upload {filename}: {str(e)}"
print(error_msg)
url_file.write(f"{filename}: ERROR - {str(e)}\n")
if __name__ == "__main__":
files_directory = "files_dir"
if not os.path.exists(files_directory):
print(f"Error: Directory '{files_directory}' not found!")
else:
print(f"Processing files in {files_directory}...")
process_directory(files_directory, "png")
print("Done! Check results/file_urls.txt for the URLs.")
{
"access_token": "",
"expires_in": 3599,
"refresh_token": "",
"scope": "https://www.googleapis.com/auth/drive.file",
"token_type": "Bearer"
}
{
"web": {
"client_id": "",
"project_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "",
"redirect_uris": [
"https://localhost:8080"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment