Skip to content

Instantly share code, notes, and snippets.

@pl0xy
Last active July 18, 2022 13:31
Show Gist options
  • Save pl0xy/1cbfd5679ffb9033600509e396431075 to your computer and use it in GitHub Desktop.
Save pl0xy/1cbfd5679ffb9033600509e396431075 to your computer and use it in GitHub Desktop.
Typescript promise wrapper for s3.getSignedUrl with correct type definitions
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 ) );
} );
}
@ColdFire87
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment