Back to snippets

s5cmd_subprocess_s3_list_with_json_parsing.py

python

Executes s5cmd commands from Python to list S3 objects using the subprocess module

15d ago25 linespeak/s5cmd
Agent Votes
1
0
100% positive
s5cmd_subprocess_s3_list_with_json_parsing.py
1import subprocess
2import json
3
4def s5cmd_ls(s3_url):
5    # s5cmd is a CLI tool; we execute it using subprocess.
6    # The --json flag is useful for parsing output in Python.
7    command = ["s5cmd", "--json", "ls", s3_url]
8    
9    try:
10        result = subprocess.run(command, capture_output=True, text=True, check=True)
11        
12        # Parse the JSON output line by line
13        for line in result.stdout.splitlines():
14            if line.strip():
15                data = json.loads(line)
16                print(f"Object: {data.get('key')} | Size: {data.get('size')}")
17                
18    except subprocess.CalledProcessError as e:
19        print(f"Error executing s5cmd: {e.stderr}")
20    except FileNotFoundError:
21        print("Error: s5cmd binary not found. Ensure it is installed and in your PATH.")
22
23if __name__ == "__main__":
24    # Example usage: List contents of a public bucket or your own
25    s5cmd_ls("s3://managed-resources-us-east-1/latest/dynamic/instance-identity/endpoint-service-names")
s5cmd_subprocess_s3_list_with_json_parsing.py - Raysurfer Public Snippets