代码示例:
import os import shutil
def check_disk_usage(disk): '''Return True if there is enough free disk space, False otherwise.''' du = shutil.disk_usage(disk) free = du.free / du.total * 100 return free > 20 # 至少保留20%的可用空间
def delete_files(path): '''Delete files that are older than 30 days''' for root, dirs, files in os.walk(path): for file in files: filename = os.path.join(root, file) if os.stat(filename).st_ctime < time.time() - 30 * 86400: # 文件创建时间超过30天 os.remove(filename)
def expand_storage(volume_id, size): '''Expand the storage volume to the specified size''' ec2 = boto3.client("ec2") ec2.modify_volume(VolumeId=volume_id, Size=size)
def s3_storage(bucket_name, file_path, s3_key): '''Upload file to S3''' s3 = boto3.resource("s3") bucket = s3.Bucket(bucket_name) bucket.upload_file(file_path, s3_key)