Skip to main content

API to get an S3 presigned url for file uploads

Project description

cdk-s3-upload-presignedurl-api

npmjs PyPI Maven Central

cdk-s3-upload-presignedurl-api is AWS CDK construct library that create an API to get a presigned url to upload a file in S3.

Background

In web and mobile applications, it's common to provide the ability to upload data (documents, images, ...). Uploading files on a web server can be challenging and AWS recommends to upload files directly to S3. To do that securely, you can use pre-signed URLs. This blog post provides some more details.

Architecture

Architecture

  1. The client makes a call to the API, specifying the "contentType" of the file to upload in request parameters (eg. ?contentType=image/png in the URL)
  2. API Gateway handles the request and execute the Lambda function.
  3. The Lambda function makes a call to the getSignedUrl api for a putObject operation.
  4. The Lambda function returns the generated URL and the key of the object in S3 to API Gateway.
  5. The API returns the generated URL and the key of the object in S3 to the client.
  6. The client can now use this URL to upload a file, directly to S3.

Getting Started

TypeScript

Installation

$ npm install --save cdk-s3-upload-presignedurl-api

Usage

import * as cdk from '@aws-cdk/core';
import { S3UploadPresignedUrlApi } from 'cdk-s3-upload-presignedurl-api';

const app = new cdk.App();
const stack = new cdk.Stack(app, '<your-stack-name>');

new S3UploadPresignedUrlApi(stack, 'S3UploadSignedUrl');

Python

Installation

$ pip install cdk-s3-upload-presignedurl-api

Usage

import aws_cdk.core as cdk
from cdk_s3_upload_presignedurl_api import S3UploadPresignedUrlApi

app = cdk.App()
stack = cdk.Stack(app, "<your-stack-name>")

S3UploadPresignedUrlApi(stack, 'S3UploadSignedUrl')

Java

Maven configuration

<dependency>
    <groupId>io.github.jeromevdl.awscdk</groupId>
    <artifactId>s3-upload-presignedurl-api</artifactId>
    <version>...</version>
</dependency>

Usage

import software.amazon.awscdk.App;
import software.amazon.awscdk.Stack;
import io.github.jeromevdl.awscdk.s3uploadpresignedurlapi.S3UploadPresignedUrlApi;

App app = new App();
Stack stack = new Stack(app, "<your-stack-name>");

new S3UploadPresignedUrlApi(stack, "S3UploadSignedUrl");

Configuration

By default and without any property, the S3UploadPresignedUrlApi construct will create:

  • The S3 Bucket, with the appropriate CORS configuration
  • The Lambda function, that will genereate the pre-signed URL
  • The REST API, that will expose the Lambda function to the client
  • The Cognito User Pool and User Pool Client to secure the API

You can shoose to let the construct do everything or you can reuse existing resources:

  • An S3 Bucket (existingBucketObj). Be carefull to configure CORS properly (doc)
  • A Cognito User Pool (existingUserPoolObj).

You can also customize the construct:

  • You can define the properties for the REST API (apiGatewayProps). Note that you cannot reuse an existing API.
  • You can configure the allowed origins (allowedOrigins) when configuring CORS. Default is *.
  • You can configure the expiration of the generated URLs, in seconds (expiration).
  • You can choose to let the API open, and remove Cognito, by setting secured to false.
  • You can choose the log retention period (logRetention) for Lambda and API Gateway.

See API reference for the details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-s3-upload-presignedurl-api-0.0.5.tar.gz (546.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_s3_upload_presignedurl_api-0.0.5-py3-none-any.whl (544.9 kB view details)

Uploaded Python 3

File details

Details for the file cdk-s3-upload-presignedurl-api-0.0.5.tar.gz.

File metadata

File hashes

Hashes for cdk-s3-upload-presignedurl-api-0.0.5.tar.gz
Algorithm Hash digest
SHA256 4a0d4c7ca4fcff6461fcdc8aedfc81f51d64b964b824942931836afac0ddb646
MD5 752eca521008793396aaecac7e12edba
BLAKE2b-256 c5e99b1fb0a6b10d2b5b271a50580e55dcbf45f63337206bebf164d1ddbabe60

See more details on using hashes here.

File details

Details for the file cdk_s3_upload_presignedurl_api-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_s3_upload_presignedurl_api-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 be312a8335ab9de5c5c4c331ce2755299a694c809b231083fae1227020113e4a
MD5 ca59a8670f2cdc2d5090ca1616fb8d17
BLAKE2b-256 7a5863ea54e1b547b183f952bb45b1eb9f3a6eb71b0c3944ec36ac5cd9f65137

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page