Sftp To Google Drive

Sftp To Google Drive

Reading time1 min
#Cloud#Security#Automation#SFTP#GoogleDrive#Python

Seamless Integration: How to Set Up Automated SFTP Transfers to Google Drive

Most IT teams treat SFTP and Google Drive as separate silos, manually juggling uploads and downloads. What if you could automate and secure this entire flow end-to-end, cutting manual errors and accelerating data availability without compromising compliance?

SFTP (Secure File Transfer Protocol) remains a gold standard in enterprises for securely exchanging sensitive files. On the other hand, Google Drive has become ubiquitous for collaborative cloud storage and file sharing. However, these two powerful tools don’t natively talk to each other, forcing teams into time-consuming manual interventions or error-prone workarounds.

In this practical guide, I’ll walk you through setting up an automated pipeline to transfer files from an SFTP server directly into Google Drive. This integration preserves your strong security posture, ensures auditability, and frees your team from repetitive upload/download cycles.


Why Automate SFTP to Google Drive Transfers?

  • Security: Maintain encrypted transfers via SFTP while leveraging Google’s secure cloud infrastructure.
  • Efficiency: Eliminate manual moves that slow down workflows and invite human error.
  • Auditability: Track file movements for compliance using logging within the automation.
  • Scalability: Easily handle growing volumes of files without adding manual overhead.

Tools You’ll Need

  • An SFTP server with access credentials.
  • A Google Cloud Platform project with Google Drive API enabled.
  • A server or cloud VM where you can run scripts (Linux-based is easiest for SSH/SFTP commands).
  • Python 3 installed, along with some helper libraries.

Step 1: Set Up Google Drive API Access

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. Enable the Google Drive API.
  4. Create OAuth 2.0 credentials or a Service Account:
    • For unattended automation, a Service Account is preferred.
    • Download the JSON key file for the service account.
  5. Share the target Google Drive folder with the service account’s email to grant write permissions.

Step 2: Create a Python Script to Download from SFTP

You'll use the popular paramiko library to connect and pull files securely from the SFTP server.

import paramiko
import os

def download_files_from_sftp(host, port, username, password, remote_dir, local_dir):
    transport = paramiko.Transport((host, port))
    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    os.makedirs(local_dir, exist_ok=True)

    for filename in sftp.listdir(remote_dir):
        remote_path = f"{remote_dir}/{filename}"
        local_path = os.path.join(local_dir, filename)
        sftp.get(remote_path, local_path)
        print(f"Downloaded {filename} to {local_path}")

    sftp.close()
    transport.close()

# Example usage
download_files_from_sftp(
    host='sftp.example.com',
    port=22,
    username='your_user',
    password='your_password',
    remote_dir='/remote/path',
    local_dir='./downloaded_files'
)

Step 3: Upload Files to Google Drive Using the API

Next, install Google’s client library:

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

Here’s a simple function to upload a file to a specific folder in Google Drive:

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

def upload_to_gdrive(service_account_file, folder_id, file_path):
    SCOPES = ['https://www.googleapis.com/auth/drive.file']
    creds = service_account.Credentials.from_service_account_file(service_account_file, scopes=SCOPES)
    service = build('drive', 'v3', credentials=creds)

    file_metadata = {
        'name': os.path.basename(file_path),
        'parents': [folder_id]
    }
    media = MediaFileUpload(file_path, resumable=True)
    file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    print(f'Uploaded "{file_path}" with file ID: {file.get("id")}')

# Example usage
upload_to_gdrive('service_account.json', 'your_google_drive_folder_id', './downloaded_files/example.csv')

Step 4: Combine the Download and Upload Steps

Create a main workflow that downloads files from SFTP and uploads each to Google Drive, then optionally deletes processed files locally or on the SFTP server for cleanup.

def automate_sftp_to_gdrive(sftp_config, gdrive_config):
    local_dir = './temp_files'
    download_files_from_sftp(
        sftp_config['host'],
        sftp_config['port'],
        sftp_config['username'],
        sftp_config['password'],
        sftp_config['remote_dir'],
        local_dir
    )

    for filename in os.listdir(local_dir):
        file_path = os.path.join(local_dir, filename)
        upload_to_gdrive(
            gdrive_config['service_account_file'],
            gdrive_config['folder_id'],
            file_path
        )
        # Optional cleanup
        os.remove(file_path)

# Configuration details
sftp_config = {
    'host': 'sftp.example.com',
    'port': 22,
    'username': 'user',
    'password': 'pass',
    'remote_dir': '/data/incoming'
}

gdrive_config = {
    'service_account_file': 'service_account.json',
    'folder_id': '0B_xxx_your_folder_id_here'
}

if __name__ == "__main__":
    automate_sftp_to_gdrive(sftp_config, gdrive_config)

Step 5: Schedule Your Script

To automate this regularly:

  • On Linux/macOS, use cron to run the script at intervals (e.g., every hour).
  • On Windows, use Task Scheduler.

Example cron entry to run every hour:

0 * * * * /usr/bin/python3 /path/to/your/script.py >> /var/log/sftp_to_gdrive.log 2>&1

Additional Tips for Production

  • Add Logging: Use Python’s logging module to create audit trails.
  • Error Handling: Wrap critical calls with try/except to catch and alert on failures.
  • File Integrity: Consider verifying checksums before and after transfer.
  • Security: Prefer SSH keys over passwords and secure your service account key files.
  • Cleanup: Optionally archive or delete files on the SFTP server once uploaded.

Conclusion

By bridging SFTP with Google Drive through automation, you eliminate manual hand-offs, reduce errors, and speed data availability — all while maintaining enterprise-grade security and compliance. With just a few lines of Python and Google API setup, your IT workflows become more seamless and scalable.

Ready to stop juggling files manually? Try this approach to fully automate your secure file transfers from SFTP straight into Google Drive!


If you found this useful or have questions about customizations, leave a comment or connect with me on [LinkedIn/Twitter]!