All guides

Method 1: Using requests library (HTTP upload)

import requests

def upload_pdf_to_server(file_path, upload_url):
    """
    Upload a PDF file to a server via HTTP POST request

    Args:
        file_path (str): Path to the PDF file
        upload_url (str): URL of the upload endpoint
    """
    try:
        # Open the file in binary mode
        with open(file_path, 'rb') as file:
            files = {'file': (file_path, file, 'application/pdf')}

            # Optional: Add additional form data if needed
            data = {
                'description': 'PDF document upload',
                'upload_type': 'document'
            }

            # Send POST request
            response = requests.post(upload_url, files=files, data=data)

            # Check if upload was successful
            if response.status_code == 200:
                print("PDF uploaded successfully!")
                print(f"Response: {response.json()}")
            else:
                print(f"Upload failed with status code: {response.status_code}")
                print(f"Error: {response.text}")

    except FileNotFoundError:
        print(f"Error: File {file_path} not found")
    except Exception as e:
        print(f"Error during upload: {str(e)}")

# Example usage
if __name__ == "__main__":
    pdf_file = "document.pdf"
    upload_url = "https://example.com/upload"

    upload_pdf_to_server(pdf_file, upload_url)

Method 2: Using FTP

import ftplib
import os

def upload_pdf_via_ftp(file_path, ftp_host, username, password, remote_path):
    """
    Upload a PDF file to an FTP server

    Args:
        file_path (str): Path to the PDF file
        ftp_host (str): FTP server hostname
        username (str): FTP username
        password (str): FTP password
        remote_path (str): Remote directory path on FTP server
    """
    try:
        # Connect to FTP server
        ftp = ftplib.FTP(ftp_host)
        ftp.login(username, password)

        # Change to target directory
        ftp.cwd(remote_path)

        # Get filename from path
        filename = os.path.basename(file_path)

        # Upload file in binary mode
        with open(file_path, 'rb') as file:
            ftp.storbinary(f'STOR {filename}', file)

        print(f"PDF uploaded successfully to FTP server: {filename}")
        ftp.quit()

    except Exception as e:
        print(f"FTP upload error: {str(e)}")

# Example usage
# upload_pdf_via_ftp("document.pdf", "ftp.example.com", "user", "pass", "/uploads")

Method 3: Using Google Drive API

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

def upload_pdf_to_google_drive(file_path, credentials_file, folder_id=None):
    """
    Upload PDF to Google Drive using service account

    Args:
        file_path (str): Path to PDF file
        credentials_file (str): Path to service account JSON credentials
        folder_id (str): Optional folder ID in Google Drive
    """
    try:
        # Authenticate with service account
        credentials = service_account.Credentials.from_service_account_file(
            credentials_file,
            scopes=['https://www.googleapis.com/auth/drive.file']
        )

        # Create Drive service
        service = build('drive', 'v3', credentials=credentials)

        # File metadata
        file_metadata = {
            'name': os.path.basename(file_path),
            'mimeType': 'application/pdf'
        }

        if folder_id:
            file_metadata['parents'] = [folder_id]

        # Upload file
        media = MediaFileUpload(file_path, mimetype='application/pdf')
        file = service.files().create(
            body=file_metadata,
            media_body=media,
            fields='id'
        ).execute()

        print(f"PDF uploaded to Google Drive with ID: {file.get('id')}")

    except Exception as e:
        print(f"Google Drive upload error: {str(e)}")

# Example usage
# upload_pdf_to_google_drive("document.pdf", "credentials.json", "folder_id_here")

Method 4: Using AWS S3

import boto3
from botocore.exceptions import ClientError

def upload_pdf_to_s3(file_path, bucket_name, object_name=None):
    """
    Upload a PDF file to AWS S3 bucket

    Args:
        file_path (str): Path to PDF file
        bucket_name (str): S3 bucket name
        object_name (str): S3 object name (optional)
    """
    # If S3 object_name is not specified, use file name
    if object_name is None:
        object_name = os.path.basename(file_path)

    try:
        # Create S3 client (ensure AWS credentials are configured)
        s3_client = boto3.client('s3')

        # Upload file
        s3_client.upload_file(
            file_path,
            bucket_name,
            object_name,
            ExtraArgs={'ContentType': 'application/pdf'}
        )

        print(f"PDF uploaded successfully to S3: s3://{bucket_name}/{object_name}")

    except ClientError as e:
        print(f"S3 upload error: {str(e)}")

# Example usage
# upload_pdf_to_s3("document.pdf", "my-bucket-name")

Installation Requirements

# For HTTP uploads
pip install requests

# For FTP uploads (built-in, no extra install needed)

# For Google Drive
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib

# For AWS S3
pip install boto3

Key Points:

  1. HTTP Upload: Most common method for web applications
  2. FTP: Traditional file transfer protocol
  3. Google Drive: Cloud storage integration
  4. AWS S3: Cloud object storage

Choose the method that best fits your specific use case and infrastructure requirements. Remember to handle exceptions appropriately and secure your credentials in production environments.