This is a demo application for image uploading and downloading. It uses S3 prs-signed URL to upload to S3 and CloudFront signed URL to download from CloudFront.
This project uses SAM to build and deploy, please follow the AWS document Installing the AWS SAM CLI to install.
Before deployment, please configure the following items.
If your API Gateway doesn't have permissions to write CloudWatch logs, please enable it on API Gateway console.
You can also enable it by deploying the CloudFormation file cloudformation/apigateway-global-settings.yaml
.
Because signing CloudFront URL needs a key pair, you need to use root account to create one.
On AWS console, select My Security Credentials
in menu bar, select CloudFront key pairs
, click Create New Key Pair
.
Then you need to store the generate key ID and private key in SSM parameter store.
To store CloudFront key ID in SSM parameter store, go to Systems Manager
console, select Parameter Store
,
save it with name /applications/ServerlessDemo/CloudFront/KeyId
and type String
.
To store private key in parameter store, save it with name /applications/ServerlessDemo/CloudFront/PrivateKey
and type SecureString
.
You can use cURL to test these APIs.
To get a S3 pre-signed URL for uploading, you need to provide a file name. If you want to resize the image, you have to provide content type, width and height.
curl -X POST -H "Content-Type: application/json" https://{API_GATEWAY_ENDPOINT}/get-upload-url
-d '{"file": "{FILE_NAME}", "contentType": "image/jpg", "width": 2048, "height": 1024}'
You will get the following response, the url
is a S3 pre-signed URL you can use to upload file to S3.
Please note the response headers need to be passed back to S3 when uploading file:
{
"headers": {
"content-type": "image/jpg",
"x-amz-meta-height": "1024",
"x-amz-meta-width": "2048"
},
"url": "{UPLOAD_URL}"
}
To upload file to S3 using pre-signed URL, you need to pass back the headers you got from calling get-upload-url API.
curl -X PUT -H "content-type: image/jpg" -H "x-amz-meta-heigh: 1024" -H "x-amz-meta-width: 2048" {UPLOAD_URL}
--data-binary '@{PATH_TO_IMAGE}.jpg'
To get a CloudFront signed URL for downloading, you need to provide the file name you want to download.
curl -X POST -H "Content-Type: application/json" https://{API_GATEWAY_ENDPOINT}/get-download-url
-d '{"file": "{FILE_NAME}"'
You will get the following response, the url
is a CloudFront signed URL you can use to download file from CloudFront.
{
"url": "{DOWNLOAD_URL}"
}
curl -X GET {DOWNLOAD_URL}