The discussion revolves around an issue with the uploads: create API key scope, which previously resulted in a 403 error when attempting to upload via the API. mattdm detailed the upload process and noted the error with a granular API key. sam acknowledged the issue, explaining it was due to the complexity of the direct upload flow to S3. mattdm confirmed the process was standard for his use case. martin resolved the issue by updating the API key scope to include the necessary actions and confirmed the fix was deployed. mattdm confirmed the issue was resolved and thanked the team.
I’m using the following code to upload through the API:
# And ask Discourse where to send it.
r = requests.post(
f"https://{DISCOURSE}/uploads/generate-presigned-put", json=file_info, headers=HEADERS)
if r.status_code != 200:
print(
f"Error asking where to upload the image: got {r.status_code}", file=sys.stderr)
sys.exit(1)
upload_url = r.json()['url']
upload_uid = r.json()['unique_identifier']
# Now put it where we were told to.
r = requests.put(upload_url, data=image_data)
if r.status_code != 200:
print(
f"Error uploading image to external storage: got {r.status_code}", file=sys.stderr)
sys.exit(1)
# And tell Discourse that it worked, and get back an id we can reference later.
r = requests.post(f"https://{DISCOURSE}/uploads/complete-external-upload",
data=f'unique_identifier={upload_uid}', headers=HEADERS)
if r.status_code != 200:
print(f"Error completing upload: got {r.status_code}", file=sys.stderr)
sys.exit(1)
image_id = r.json()['id']
This works with an “all access” API key, but when I try to use a granular one with the “uploads: create” scope, I get a 403 error on post to /uploads/generate-presigned-put.
Hi @mattdm I’ve updated the API key scope to cover these new actions for our direct upload flow, and I’ve deployed your site too, please let me know if you still have further issues.