A Rust console application that uploads files to Google Drive using a service account for authentication.
- Upload files to Google Drive using service account authentication
- Support for custom file names in Google Drive
- Optional parent folder specification
- Automatic MIME type detection
- Clean command-line interface with clap
- Comprehensive error handling
-
Google Cloud Project: You need a Google Cloud Project with the Google Drive API enabled.
-
Service Account: Create a service account in your Google Cloud Project and download the JSON key file.
-
Enable Google Drive API:
- Go to the Google Cloud Console
- Navigate to "APIs & Services" > "Library"
- Search for "Google Drive API" and enable it
- Go to the Google Cloud Console
- Select your project (or create a new one)
- Navigate to "IAM & Admin" > "Service Accounts"
- Click "Create Service Account"
- Fill in the details and click "Create and Continue"
- Skip the optional steps and click "Done"
- Click on your newly created service account
- Go to the "Keys" tab
- Click "Add Key" > "Create new key"
- Select "JSON" format and click "Create"
- Save the downloaded JSON file securely
If you want to upload to a specific folder:
- Create a folder in Google Drive
- Share the folder with your service account email (found in the JSON key file)
- Copy the folder ID from the URL when viewing the folder
# Clone or create the project
cargo build --release
# Run the application
cargo run -- --helpUpload a file to the root of Google Drive:
cargo run -- \
--service-account-key /path/to/service-account-key.json \
--file-path /path/to/your/file.txtUpload with custom name and parent folder:
cargo run -- \
--service-account-key /path/to/service-account-key.json \
--file-path /path/to/your/file.txt \
--name "My Custom File Name.txt" \
--parent-folder-id "1ABCDEfghIJKLmnoPQRstUVwxYZ"-s, --service-account-key <PATH>: Path to the service account JSON key file (required)-f, --file-path <PATH>: Path to the file to upload (required)-n, --name <NAME>: Custom name for the file in Google Drive (optional)-p, --parent-folder-id <ID>: Parent folder ID in Google Drive (optional)
Authenticating with Google Drive...
Uploading file: /home/user/document.pdf
✓ File uploaded successfully!
File ID: 1XYZabcDEFghiJKLmnoPQRstUVwxyz
File Name: document.pdf
View Link: https://drive.google.com/file/d/1XYZabcDEFghiJKLmnoPQRstUVwxyz/view
- Keep your service account key file secure and never commit it to version control
- Consider using environment variables or secure secret management for production use
- The service account only has access to files it creates unless explicitly shared
-
"Failed to decode private key": The private key in your JSON file might be malformed. Re-download the service account key.
-
"Token request failed": Check that your service account key is valid and the Google Drive API is enabled.
-
"File not found": Verify the file path is correct and the file exists.
-
"Upload failed": Check your internet connection and that the service account has the necessary permissions.
To get a folder ID for the --parent-folder-id option:
- Open Google Drive in your browser
- Navigate to the desired folder
- Look at the URL:
https://drive.google.com/drive/folders/FOLDER_ID_HERE - Copy the folder ID from the URL
tokio: Async runtimereqwest: HTTP client with multipart supportserde: Serialization/deserializationjsonwebtoken: JWT token creation for OAuthclap: Command-line argument parsinganyhow: Error handlingchrono: Date/time handlingmime_guess: MIME type detection
This project is provided as-is for educational and practical purposes.