You can save files to Amazon with Python using library called boto. Boto is a Python interface to Amazon Web Services and we need just a tiny part of it. If you have very large files you have to check into "multipart upload".
Source code viewer
import os
from boto import s3
from boto.s3.key import Key
from boto.s3.connection import OrdinaryCallingFormat
# Directory for uploaded files.
upload_dir = '/var/www/upload_dir'
# Amazon cloud connection.
s3_connection = s3.connect_to_region(
'region',
calling_format=OrdinaryCallingFormat(),
aws_access_key_id='access_key',
aws_secret_access_key='access_key_secret'
)
bucket = s3_connection.get_bucket('bucket_name')
# Save directory to Amazon cloud.
for root, dirs, files in os.walk(upload_dir):
for name in files:
# Count the directory depth.
dir_count = upload_dir.count('/') + 1
# Get current directory.
name_path = root.split(os.path.sep)[dir_count:]
# Add name to current directory.
name_path.append(name)
# Join the directory list to string.
name_path = os.path.join(*name_path)
# Upload a file.
k = Key(bucket=bucket, name=name_path)
k.set_contents_from_filename(os.path.join(root, name))Programming Language: Python