How can I use Python scripts to download and upload files to a cloud server?
2 min readJun 21, 2024
import requests
import paramiko
import os
# Replace placeholders with actual values
downloaded_url = "https://www.example.com/your_file.txt" # Replace with the actual URL
local_file_path = "/path/to/local/file.txt" # Replace with the desired local path
remotefile_path = "/path/to/remote/server/file.txt" # Replace with the remote server path
servers = [
{
"hostname": "server1.example.com", # Replace with actual server hostname or IP
"port": 22,
"username": "username", # Replace with actual username
"password": "password" # Replace with actual password (store securely)
},
# Add additional server configurations here if needed
]
def download_file(url, local_path):
"""Downloads a file from the specified URL to the local path.
Args:
url (str): The URL of the file to download.
local_path (str): The local path where the file will be saved.
Returns:
None
"""
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(local_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded file from {url} to {local_path}")
else:
print(f"Failed to download file: {response.status_code}")
def upload_to_remote_server(local_path, remote_path, server_info):
"""Uploads a file to a remote server using SSH.
Args:
local_path (str): The local path of the file to upload.
remote_path (str): The remote path where the file will be uploaded on the server.
server_info (dict): A dictionary containing server connection information.
Returns:
None
"""
try:
# Create a secure SSH client with improved password handling
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=server_info["hostname"], port=server_info["port"], username=server_info["username"])
# Use SFTP for secure file transfer
sftp_client = ssh_client.open_sftp()
sftp_client.put(local_path, remote_path)
sftp_client.close()
ssh_client.close()
print(f"Uploaded file {local_path} to {remote_path} on {server_info['hostname']}")
except Exception as e:
print(f"Upload failed: {e}")
# Download the file first
download_file(downloaded_url, local_file_path)
# Then upload the downloaded file to each server
for server_data in servers:
upload_to_remote_server(local_file_path, remotefile_path, server_data)
Option-2
import paramiko
import requests
# Replace with your server details and file paths
hostname = 'your_server.com'
username = 'your_username'
password = 'your_password' # Store securely!
remote_path = '/path/to/remote/file.txt'
local_path = '/path/to/local/file.txt'
# Upload a file using SSH
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=hostname, username=username, password=password)
sftp_client = ssh_client.open_sftp()
sftp_client.put(local_path, remote_path)
sftp_client.close()
ssh_client.close()
print(f"Uploaded file {local_path} to {remote_path}")
# Download a file using requests (if supported by cloud server)
response = requests.get(f'https://your_server.com/{remote_path}', stream=True)
if response.status_code == 200:
with open(local_path, 'wb') as file:
for chunk in response.iter_content(chunk_size=8192):
file.write(chunk)
print(f"Downloaded file from {remote_path} to {local_path}")
else:
print(f"Failed to download file: {response.status_code}")