다음 코드를 사용하여 API를 통해 업로드하고 있습니다:
# 그리고 Discourse에 어디로 전송할지 요청합니다.
r = requests.post(
f"https://{DISCOURSE}/uploads/generate-presigned-put", json=file_info, headers=HEADERS)
if r.status_code != 200:
print(
f"이미지 업로드 위치 요청 중 오류 발생: {r.status_code} 수신됨", file=sys.stderr)
sys.exit(1)
upload_url = r.json()['url']
upload_uid = r.json()['unique_identifier']
# 이제 지시받은 위치에 업로드합니다.
r = requests.put(upload_url, data=image_data)
if r.status_code != 200:
print(
f"외부 저장소로 이미지 업로드 중 오류 발생: {r.status_code} 수신됨", file=sys.stderr)
sys.exit(1)
# 그리고 Discourse에 성공했음을 알리고 나중에 참조할 수 있는 id를 가져옵니다.
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"업로드 완료 처리 중 오류 발생: {r.status_code} 수신됨", file=sys.stderr)
sys.exit(1)
image_id = r.json()['id']
이 코드는 “전체 액세스(all access)” API 키를 사용할 때는 정상 작동하지만, “uploads: create” 범위를 가진 세분화된 API 키를 사용하려고 하면 /uploads/generate-presigned-put에 대한 post 요청 시 403 오류가 발생합니다.