This is a two-step process for your front-end application. In the first step of the process, the API Gateway endpoint invokes the Lambda Function to make a signed URL request. Once the frontend application receives the API Gateway endpoint response, it will then send a request to the signed URL. Uploading directly to an S3 bucket, you must first request a signed URL from the Amazon S3 service. The request contains the key of the uploaded object, the content type, and action that is equal to upload. You can then upload directly using the signed URL. To make the S3 object downloadable, you must change the response Content-Disposition header value to attachment.
You can test the following by using the provided HTML file that is in the web/static directory. After deploying the stack, remember to update the API Gateway Endpoint on the said HTML file.
NOTE:
- You can use either the
$default
orprod
stage for the API Gateway endpoint. - The S3 Bucket and its objects are configured to be deleted once the stack is destroyed/deleted.
- When uploading objects to S3 from a web application, the S3 must be configured for CORS (Cross-Origin Resource Sharing). The frontend application uses binary data to upload directly to the signed URL.
let array = [],
binary = atob(content.split(',')[1]);
for (var i = 0; i < binary.length; i++)
{
array.push(binary.charCodeAt(i));
}
let blob = new Blob([ new Uint8Array(array) ], { type: type });
fetch(response.url, {
method: 'PUT',
body: blob
}).catch(error => {
console.error("Upload error: ", error);
});
- Allowed Headers: '*'
- Allowed Origins: '*'
- Allowed Methods: GET, PUT, HEAD
The preceding policy allows all headers and origins - it is recommended that you use a more restrictive policy for production workloads.
- Amazon S3
- AWS Lambda
- Amazon API Gateway v2
- Amazon API Gateway v2 Integrations
- HTTP API Example in AWS CDK [API Gateway V2]
- CORS configuration
- Downloading an object
- Bucket restrictions and limitations
- Using cross-origin resource sharing (CORS)
- Uploading to Amazon S3 directly from a web or mobile application
- How can I resolve the "Bucket name already exists" or "BucketAlreadyExists" error from Amazon S3?
The cdk.json
file tells the CDK Toolkit how to execute your app.
npm install
install projects dependenciesnpm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation templatecdk bootstrap
deployment of AWS CloudFormation template to a specific AWS environment (account and region)cdk destroy
destroy this stack from your default AWS account/region
-
Install all the dependencies, bootstrap your project, and synthesized CloudFormation template.
# Without passing "profile" parameter dev@dev:~:aws-cdk-samples/s3/s3-presigned-urls$ make init # With "profile" parameter dev@dev:~:aws-cdk-samples/s3/s3-presigned-urls$ make init profile=[profile_name]
-
Deploy the project.
# Without passing "profile" parameter dev@dev:~:aws-cdk-samples/s3/s3-presigned-urls$ make deploy # With "profile" parameter dev@dev:~:aws-cdk-samples/s3/s3-presigned-urls$ make deploy profile=[profile_name]