Write a Python code block that calls a REST API endpoint, receives a JSON response, and uploads a file to an S3 bucket.
1Times asked
Jun 2026Last seen
Jun 2026First seen
π‘ Model Answer
You can use the requests library to call the API and boto3 to upload to S3. The code below demonstrates a simple flow: call the API, parse the JSON, write the data to a temporary file, and upload that file to S3.
python
import requests
import boto3
import tempfile
import json
# 1. Call the REST API
api_url = "https://api.example.com/get-data"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
resp = requests.get(api_url, headers=headers)
resp.raise_for_status()
# 2. Parse JSON response
data = resp.json()
# 3. Write JSON to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as tmp:
json.dump(data, tmp)
tmp_path = tmp.name
# 4. Upload to S3
s3 = boto3.client("s3")
bucket_name = "my-bucket"
object_key = "data/api_response.json"
s3.upload_file(tmp_path, bucket_name, object_key)
print(f"Uploaded {tmp_path} to s3://{bucket_name}/{object_key}")Key points:
- Use
raise_for_status()to catch HTTP errors. tempfile.NamedTemporaryFileensures the file is cleaned up after use.boto3.client('s3').upload_filehandles multipart upload for large files.- For large JSON payloads, consider streaming the response and uploading directly without storing the entire file in memory.
Complexity: Network I/O dominates; the upload is O(m) where m is the file size. The code is straightforward and works for most small to medium payloads.
This answer was generated by AI for study purposes. Use it as a starting point β personalize it with your own experience.
π€ Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers β invisible to screen sharing.
Get Assisting AI β Starts at βΉ500