Opening Problem Statement
Meet Sarah, a freelance graphic designer juggling multiple client projects simultaneously. With every project, she constantly uploads high-resolution images to a Google Drive folder to share with clients and collaborators. However, these images often take up significant storage space, causing syncing delays and sometimes even making it difficult for clients with slower internet connections to access files promptly.
Every week, Sarah spends hours manually compressing images before uploading them again to Google Drive to manage her storage and maintain quick access. This tedious manual process leads to wasted time, frustration, and sometimes errors when wrong or uncompressed files get shared. What if there was a way to automate this entire process seamlessly, saving Sarah hours of repetitive work and ensuring all images in her Drive are optimized automatically?
What This Automation Does
This n8n workflow automates the optimization of images uploaded to a specific Google Drive folder by integrating Google Drive and TinyPNG services. Here’s what happens when the workflow runs:
- Automatically triggers when a new image file is added to a chosen Google Drive folder.
- Downloads the newly uploaded image file from Google Drive.
- Sends the image to TinyPNG’s API for compression and optimization.
- Retrieves the optimized, smaller version of the image from TinyPNG.
- Uploads the optimized image back to a specified Google Drive folder, making it easy to organize compressed images separately.
- Runs this entire process automatically every minute, ensuring images are optimized in near real-time without manual intervention.
Thanks to this workflow, Sarah can save hours weekly that she used to spend manually compressing files. It also reduces storage space usage and speeds up file sharing with clients.
Prerequisites ⚙️
- 📁 Google Drive account with OAuth 2.0 credentials set up in n8n for seamless access to your Drive files.
- 🔑 API key for TinyPNG from tinypng.com.
- ⏱️ n8n account, either cloud-hosted or self-hosted (self-hosted option available for advanced users; check out this guide).
Step-by-Step Guide
Step 1: Create Google Drive Credentials in n8n
Navigate to Settings → API Credentials in your n8n dashboard. Select Google OAuth2 and follow the instructions to link your Google Drive account. This credential allows n8n to monitor and access your Drive folders and files securely.
Common Mistake: Forgetting to enable the appropriate Google Drive API scopes can cause authorization errors.
Step 2: Set Up the Google Drive Trigger Node
In your workflow canvas, drag in the Google Drive Trigger node (n8n-nodes-base.googleDriveTrigger), name it “Check GDrive for new images.”
Configure it to watch for new files created (fileCreated) in a specific folder you want to monitor for image uploads. Use the folder picker to select this folder in Google Drive.
This node checks for new images every minute to trigger the automation promptly.
Common Mistake: Leaving the folder field blank will monitor the entire Drive, which can be inefficient.
Step 3: Download the New Image from Google Drive
Add a Google Drive node (n8n-nodes-base.googleDrive) named “Download image.” Set its operation to download. For the fileId parameter, use the expression {{$json.id}} from the trigger node output.
Link the trigger node’s output to this node.
When triggered, this node fetches the actual binary image data.
Step 4: Optimize the Image Using TinyPNG API
Add an HTTP Request node (n8n-nodes-base.httpRequest) named “Optimise – Send image to TinyPNG.” Configure it as follows:
- Method: POST
- URL:
https://api.tinify.com/shrink - Content-Type: binaryData
- Authorization Header: Set as
Basic YOUR_API_KEY_BASE64. - Input Data Field: Select the binary image data from the previous node.
This sends the image to TinyPNG for compression.
Common Mistake: Not encoding your API key in Base64 format will cause authentication failures.
Step 5: Retrieve the Optimized Image from TinyPNG
Add another HTTP Request node named “Get optimised image from TinyPNG.” Use the URL set dynamically from the location header returned by TinyPNG in the previous step. Set method to GET (default).
This node downloads the compressed image.
Step 6: Upload Optimized Image Back to Google Drive
Add a Google Drive node (n8n-nodes-base.googleDrive) to upload the optimized image. Configure these parameters:
- Specify the target Drive and folder ID to save optimized images separately.
- Set the file name to original name appended with “-optimised” for easy identification (e.g.,
photo-optimised.png). - Use binary data from the TinyPNG download node.
Link the nodes so this runs after the optimized image retrieval.
Customizations ✏️
- Change Watch Folder: In the “Check GDrive for new images” node, pick a different Google Drive folder to monitor based on your project needs.
- File Type Filters: Add filters at the trigger or in-between nodes to only process specific image formats like PNG, JPG, or JPEG.
- Split Optimized Images into Subfolders: Alter the upload node to save compressed images in dynamically created subfolders by date or client name.
- Rename Optimized Files: Modify the upload node’s file naming pattern to suit company branding or file versioning.
Troubleshooting 🔧
Problem: HTTP Request to TinyPNG fails with 401 Unauthorized
Cause: Incorrect or missing API key or improperly Base64 encoded.
Solution: Confirm your TinyPNG API key, encode it properly using a Base64 encoder, and update the Authorization header accordingly in the HTTP Request node.
Problem: Google Drive Trigger doesn’t detect new files
Cause: Trigger not set to the correct folder or permissions issue.
Solution: Double-check that the trigger node folder ID matches the intended Google Drive folder and that n8n’s Google OAuth credentials have sufficient permissions.
Pre-Production Checklist ✅
- Verify Google Drive OAuth credentials are active and authorized.
- Confirm TinyPNG API key is valid and reflected in the HTTP Request header.
- Test uploading a small image to the watched Google Drive folder and ensure it triggers the workflow.
- Check logs for each node execution to confirm proper data flow.
- Ensure the optimized images appear correctly in the target Google Drive folder.
Deployment Guide
Once tested successfully, activate the workflow by toggling the switch in the top-right corner of the n8n editor. This will start the workflow to listen for new image uploads and optimize them automatically.
Monitor executions periodically via n8n’s log panel to track success or catch errors early.
FAQs
Can I use other image optimization services instead of TinyPNG?
Yes, you can replace the HTTP Request nodes with others that connect to APIs like ImageOptim or Kraken.io, adjusting the request parameters accordingly.
Does this consume TinyPNG API credits?
Yes, each image optimization counts as one API request on your TinyPNG plan.
Is my image data safe during this process?
Your image files are sent securely over HTTPS to TinyPNG, which follows industry best practices for data protection. However, review their privacy policy if handling sensitive content.
Conclusion
By completing this tutorial, you’ve built a powerful n8n workflow that automatically optimizes all new images added to a Google Drive folder using TinyPNG’s API. This automation saves Sarah and anyone else hours of manual compression tasks, reduces Drive storage costs, and speeds up file sharing with clients.
Next steps could include automating image renaming, adding notifications in Slack or email when optimizations complete, or extending this to other cloud storage platforms.
With this workflow, managing image-heavy projects just became much more efficient and less stressful!