Skip to content

Instantly share code, notes, and snippets.

@hariby
Created October 8, 2024 00:24
Show Gist options
  • Save hariby/45f5d720313492f8897e4a8e5195a4b9 to your computer and use it in GitHub Desktop.
Save hariby/45f5d720313492f8897e4a8e5195a4b9 to your computer and use it in GitHub Desktop.
Retrieve Amazon Braket task results from S3
import boto3
import json
client = boto3.client('s3')
# list bucket which starts with 'amazon-braket-'
[b['Name'] for b in client.list_buckets()['Buckets'] if b['Name'].startswith('amazon-braket-')]
# find the bucket and set the name
bucket_name = 'amazon-braket-xxxx'
# retrieve task ids such as 'tasks/22200c2a-a593-42b3-aeef-dee49db1a0dc/results.json'
tasks = [o['Key'] for o in client.list_objects_v2(Bucket=bucket_name)['Contents'] if o['Key'].startswith('tasks')]
# read object body and return JSON
results = []
for t in tasks:
b = client.get_object(Bucket=bucket_name, Key=t)['Body'].read()
j = json.loads(b)
results.append({'task': t, "result": j})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment