Knowledge Center Monthly Newsletter - July 2025
Stay up to date with the latest from the Knowledge Center. See all new Knowledge Center articles published in the last month, and re:Post’s top contributors.
How do I upload an image or PDF file to Amazon S3 through API Gateway?
I want to use Amazon API Gateway to upload an image or PDF file to Amazon Simple Storage Service (Amazon S3), and retrieve it.
Short description
To use API Gateway to upload an image or PDF as a binary file to an Amazon S3 bucket, activate binary support.
To grant API Gateway access to your S3 bucket, create an AWS Identity and Access Management (IAM) role. The IAM role must include permissions for API Gateway to perform the PutObject and GetObject actions on your S3 bucket.
Resolution
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
Create an IAM role for API Gateway
Complete the following steps:
- Open the IAM console.
- In the navigation pane, choose Roles.
- Choose Create role.
- In the Select trusted entity type section, choose AWS service.
- In the Service or use case section, choose API Gateway.
- In the Use case section, choose API Gateway.
- Choose Next.
- For Role name, enter a name for your policy. For example, enter api-gateway-upload-to-s3.
- (Optional) Add tags to the IAM role.
- Choose Create role.
Create and attach an IAM policy to the API Gateway role
Complete the following steps:
- Open the IAM console.
- In the navigation pane, choose Roles.
- In the search box, enter the name of the IAM role for the API Gateway, and then select the role from the Role name column.
- On the Roles detail page tab, choose Add permissions.
- Choose Create inline policy.
- On the Visual editor tab, in the Select a service section, choose Choose a service.
- Enter S3, and then choose S3.
- In the Specify actions from the service to be allowed box, enter PutObject, and then choose PutObject.
- Enter GetObject, and then choose GetObject.
- Expand Resources, and then choose Specific.
- Choose Add ARN.
- For Resource bucket name, enter the name of your bucket. Include the prefix if applicable.
- For Resource object name, enter an object name.
Note: The bucket name specifies the location of the uploaded files. The object name specifies the pattern that the object must adhere to for policy alignment. For more information, see General purpose bucket naming rules and Amazon S3 objects overview. - Choose Add ARNs.
- Choose Next.
- For Name, enter the name of your policy.
- Choose Create policy.
Create an API Gateway REST API
To create an API to deliver your requests, complete the following steps:
- Open the API Gateway console.
- In the navigation pane, choose APIs.
- Choose Create API.
- In the Choose an API type section, for REST API, choose Build.
- For API Name, enter a name for your API, and then choose Create API.
To create resources for your API, complete the following steps:
- In the Resources panel of your API page, select /.
- Choose Create Resource.
- For Resource path, choose /.
- For Resource Path, enter {folder}.
- Choose Create resource.
- In the Resources panel, select the /{folder} resource.
- Choose Create resource.
- For Resource path, choose /{folder}/.
- For Resource name, enter {object}.
- Choose Create resource.
To create a PUT method for your API to upload images or PDF files, complete the following steps:
- In the Methods section, choose Create method.
- For Method type, choose PUT.
- Under the Integration type category, choose AWS Service.
- For AWS Region, choose us-east-1 or the AWS Region that's on the Bucket properties page.
- For AWS Service, choose Simple Storage Service (S3).
- Keep AWS Subdomain empty.
- For HTTP method, choose PUT.
- For Action type, choose Use path override.
- For Path override (optional), enter {bucket}/{key}.
- For Execution role, enter the ARN for the IAM role.
- For Content handling, choose Passthrough.
- Choose Create method.
Configure parameter mappings for the PUT method
Complete the following steps:
- In the Resources panel of your API page, choose PUT.
- Choose Integration Request, and then choose Edit.
- Expand URL path parameters.
- Choose Add path parameter.
- For Name, enter bucket.
- For Mapped from, enter method.request.path.folder.
- Repeat steps 4–6:
For step 5, set Name to key.
For step 6, set Mapped from to method.request.path.object. - Choose Save.
Create a GET method for your API to retrieve an image
Complete the following steps:
- In the Resources panel of your API page, choose /{object}.
- In the Methods section, choose Create method.
- For Method type, choose GET.
- Under the Integration type category, choose AWS Service.
- For AWS Region, choose us-east-1 or the Region that's on the Bucket properties page.
- For AWS Service, choose Simple Storage Service (S3).
- Keep AWS Subdomain empty.
- For HTTP method, choose GET.
- For Action type, choose Use path override.
- For Path override (optional), enter {bucket}/{key}.
- For Execution role, enter the ARN for the IAM role.
- For Content Handling, choose Passthrough.
- Choose Create method.
Configure parameter mappings for the GET method
Complete the following steps:
- In the Resources panel of your API page, choose GET.
- Choose Integration request, and then choose Edit.
- Expand URL Path parameters.
- Choose Add path parameters.
- For Name, enter bucket.
- For Mapped from, enter method.request.path.folder.
- Repeat steps 4–6:
For step 5, set Name to key.
For step 6, set Mapped from to method.request.path.object. - Choose Save.
Set up response mapping to see the image or PDF in browser
Complete the following steps:
- In the Resources panel of your API page, choose GET.
- Choose Method response.
- For Response 200, choose Edit.
- Under Response body, remove application/json.
- Under Header name, choose Add header.
- For Name, enter content-type.
- Choose Save.
- Choose Integration response, and then choose Edit.
- For Method response status code 200, enter image/jpeg for an image file or application/pdf for a PDF file.
- Choose Save.
Set up binary media types for the API
Complete the following steps:
- In the navigation pane of your API page, choose API settings.
- In the Binary media types section, choose Manage media types.
- Choose Add binary media type, and then add the / string in the text box.
Note: Don't put the string in quotes. You can substitute a wildcard for a specific Multipurpose Internet Mail Extensions (MIME) type to manage as a binary media type. For example, to have API Gateway manage JPEG images as binary media types, choose image/jpeg. If you add */*, then API Gateway manages all media types as binary media types. - Choose Save changes.
Resolve CORS error with binary settings for API
Note: If you use the PUT and GET APIs in a web application, then you might get a CORS error. For more information, see CORS errors on the Mozilla website.
To resolve the CORS error with binary settings turned on, complete the following steps:
-
Open the API Gateway console.
-
Select your API.
-
In the Resources panel of your API page, select /{object}.
-
Choose Enable CORS.
-
Choose Default 4xx, Default 5xx for Gateway responses, and then choose GET, PUT for Access-Control-Allow-Methods.
-
Choose Save.
-
Run the following update-integration command:
aws apigateway update-integration --rest-api-id API_ID --resource-id RESOURCE_id --http-method OPTIONS --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_TEXT' --region AWS_REGION
Note: Replace rest-api-id with your REST API ID, RESOURCE_id with your resource ID, and AWS_REGION with your Region. For API Gateway to run options requests, the update-integration and update-integration-response commands must be run one after the other.
-
Run the following update-integration-response command:
aws apigateway update-integration-response --rest-api-id API_ID --resource-id RESOURCE_id --http-method OPTIONS --status-code 200 --patch-operations op='replace',path='/contentHandling',value='CONVERT_TO_TEXT' --region AWS_REGION
Note: Replace rest-api-id with your REST API ID, RESOURCE_id with your resource ID, and AWS_REGION with your Region.
Deploy your API
Complete the following steps:
- In the navigation pane on your API page, choose Resources.
- Choose Deploy API.
- In the Deploy API window, for Stage, choose [New Stage].
- For Stage name, enter v1.
- Choose Deploy.
- In the navigation pane, choose Stages.
- Choose the v1 stage.
- Note the invoke URL that appears.
For more information, see Deploy REST APIs in API Gateway.
Invoke your REST API to upload an image file to S3
Complete the following steps:
- Add the bucket name and object's file name to your API's invoke URL.
- Use your preferred client to make a PUT HTTP request. For example, you can download and use Postman on the Postman website.
- Choose Body, and then choose binary. When the Select file button appears, select a local file to upload.
The following is an example curl command that you can run to make a PUT HTTP request to upload an image or PDF:
curl -i --location --request PUT 'https://abc12345.execute-api.us-west-2.amazonaws.com/v1/testfolder/testimage.jpeg' --header 'Content-Type: text/plain' --data-binary '@/Path/to/file/image.jpeg'
Note: Replace abc12345 with your API ID, testfolder with your S3 bucket, and testimage.jpeg with the local file that you upload.
If */* is included in the binary media types list, then you can make a PUT request to upload the file. If image.jpeg is included in the binary media types list, then add Content-Type header to your PUT request, and set it to image/jpeg. Because the web browser makes a GET request, you can view the image or PDF in a web browser with the same URL.
Related information
- Language
- English
Related videos


if you want to store files inside a folder then how do you do that? like dynamic folder path.
Thank you for your comment. We'll review and update the Knowledge Center article as needed.

Relevant content
- Accepted Answerasked 3 years ago
- asked 2 years ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 6 months ago