Last active
July 18, 2022 13:31
-
-
Save pl0xy/1cbfd5679ffb9033600509e396431075 to your computer and use it in GitHub Desktop.
Typescript promise wrapper for s3.getSignedUrl with correct type definitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { S3 } from 'aws-sdk'; | |
const s3 = new S3(); | |
interface GetSignedUrlExpires { | |
/** | |
* Number of seconds before the URL expires | |
* | |
* Defaults to `900` | |
* | |
* @type {number} | |
* @memberof GetSignedUrlExpires | |
*/ | |
Expires?: number; | |
} | |
export type GetObjectParams = S3.GetObjectRequest & GetSignedUrlExpires; | |
export type PutObjectParams = S3.PutObjectRequest & GetSignedUrlExpires; | |
export async function getSignedUrl( operation: 'getObject', params: GetObjectParams ): Promise<string>; | |
export async function getSignedUrl( operation: 'putObject', params: PutObjectParams ): Promise<string>; | |
export async function getSignedUrl( operation: 'getObject' | 'putObject', params: GetObjectParams | PutObjectParams ): Promise<string> { | |
return new Promise<string>( ( resolve, reject ) => { | |
s3.getSignedUrl( operation, params, ( err, url ) => err ? reject( err ) : resolve( url ) ); | |
} ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍