Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@handy-common-utils/aws-utils

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@handy-common-utils/aws-utils

AWS related utilities

  • 3.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
decreased by-38.24%
Maintainers
1
Weekly downloads
 
Created
Source

@handy-common-utils/aws-utils

AWS related utilities that are compatible with both AWS Javascript SDK v3 and v2, and also some utilities that require v3.

Version Downloads/week CI

How to use

Core

First add it as a dependency:

npm install @handy-common-utils/aws-utils

Then you can use it in the code:

import { AwsUtils } from '@handy-common-utils/aws-utils';

const domainNameObjects = await AwsUtils.repeatFetchingItemsByPosition(
  pagingParam => apig.getDomainNames({ limit: 100, ...pagingParam }).promise(),
);

You can either import and use the class as shown above, or you can import individual functions directly like below:

import { repeatFetchingItemsByNextToken, repeatFetchingItemsByMarker, parseArn } from '@handy-common-utils/aws-utils';

S3

S3 related utility functions can be imported and used in this way:

import { S3Client } from '@aws-sdk/client-s3';
import { decodeS3ObjectKey, deleteS3Object } from '@handy-common-utils/aws-utils/s3';

const srcEncodedKey = record.s3.object.key;
const srcKey = decodeS3ObjectKey(srcEncodedKey);
const destKey = srcKey.replace('/src-dir/', '/dest-dir/')

const s3 = new S3Client();
await copyS3Object(s3, bucket, srcEncodedKey, destKey);
await deleteS3Object(s3, bucket, srcKey);

To use S3 related utilities, you need to add @aws-sdk/client-s3 as a dependency of your project because it is not included as a dependency of this package.

For uploading content to S3, there are two functions available. putS3Object(...) is simpler but can't handle stream content with unknown length. uploadS3Object(...) can handle stream content and supports concurrent uploading.

If you need uploadS3Object(...), remember to also add @aws-sdk/lib-storage as a dependency of your project.

SSM

SSM related utility functions can be imported and used in this way:

import { SSM } from '@aws-sdk/client-ssm';
import { getSsmParameter, getSsmParameterParsed } from '@handy-common-utils/aws-utils/ssm';

const ssm = new SSM();
const workDir = await getSsmParameter(ssm, '/my-config/work-dir', '/tmp');
const config = await getSsmParameterParsed<Config>(ssm, '/my-config/config');

To use SSM related utilities, you need to add @aws-sdk/client-ssm as a dependency of your project because it is not included as a dependency of this package.

API

@handy-common-utils/aws-utils

Modules

Classes

Class: AwsUtils

aws-utils.AwsUtils

Constructors
constructor

new AwsUtils()

Methods
dynamodbLocalClientOptions

Static dynamodbLocalClientOptions(endpoint?): Object

Build an object that can be passed into DynamoDB.DocumentClient(...) for DynamoDB Local (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html)

Parameters
NameTypeDefault valueDescription
endpointstring'http://localhost:8000'if omitted, the endpoint will be 'http://localhost:8000' which is the default
Returns

Object

the options object

NameType
accessKeyIdstring
endpointstring
regionstring
secretAccessKeystring

Example

const ddbClient = new DynamoDB.DocumentClient(process.env.IS_OFFLINE === 'true' ? AwsUtils.dynamodbLocalClientOptions() : undefined);

fetchAllByContinuationToken

Static fetchAllByContinuationToken<T, M>(fetchItemsByContinuationToken, itemsFieldName?, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with ContinuationToken/NextContinuationToken based pagination. This function is useful for client side pagination when the response from AWS API contains NextContinuationToken and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
Mstring-
Parameters
NameTypeDefault valueDescription
fetchItemsByContinuationTokenFetchItemsFunction<{ ContinuationToken?: M }, { NextContinuationToken?: M }>undefinedthe function for fetching one batch/page of items by ContinuationToken
itemsFieldNamestring'Contents'name of the field containing returned items in AWS API response
filterFunc?(entry: T) => booleanundefinedOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const objects = await fetchAllByContinuationToken(() => s3.send(new ListObjectsV2Command({Bucket: bucket})));

fetchAllByExclusiveStartKey

Static fetchAllByExclusiveStartKey<T, K>(fetchItemsByExclusiveStartKey, itemsFieldName?, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with ExclusiveStartKey/LastEvaluatedKey based pagination. This function is useful for client side pagination when the response from AWS API contains LastEvaluatedKey and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
K{ [key: string]: any; }-
Parameters
NameTypeDefault valueDescription
fetchItemsByExclusiveStartKeyFetchItemsFunction<{ ExclusiveStartKey?: K }, { LastEvaluatedKey?: K }>undefinedthe function for fetching one batch/page of items by ExclusiveStartKey
itemsFieldNamestring'Items'name of the field containing returned items in AWS API response, the default value is 'Items'
filterFunc?(entry: T) => booleanundefinedOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const allItemsInDynamoDbTable = await AwsUtils.fetchAllByExclusiveStartKey<MyTableItem>(
  pagingParam => dynamoDbDocumentClient.scan({...pagingParam, TableName: 'my-table', limit: 20}).promise(),
);

fetchAllByMarker

Static fetchAllByMarker<T, M>(fetchItemsByMarker, itemsFieldName, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with Marker/NextMarker based pagination. This function is useful for client side pagination when the response from AWS API contains NextMarker and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
Mstring-
Parameters
NameTypeDescription
fetchItemsByMarkerFetchItemsFunction<{ Marker?: M }, { NextMarker?: M }>the function for fetching one batch/page of items by Marker
itemsFieldNamestringname of the field containing returned items in AWS API response
filterFunc?(entry: T) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const functionConfigurations = await AwsUtils.fetchAllByMarker<Lambda.FunctionConfiguration>(
  pagingParam => withRetry(() => lambda.listFunctions({ ...pagingParam }).promise()),
  'Functions',
);

fetchAllByNextToken

Static fetchAllByNextToken<T, K>(fetchItemsByNextToken, itemsFieldName, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with NextToken based pagination. This function is useful for client side pagination when the response from AWS API contains NextToken and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
Kstring-
Parameters
NameTypeDescription
fetchItemsByNextTokenFetchItemsFunction<{ NextToken?: K }, { NextToken?: K }>the function for fetching one batch/page of items by NextToken
itemsFieldNamestringname of the field containing returned items in AWS API response
filterFunc?(entry: T) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const topics = await AwsUtils.fetchAllByNextToken<SNS.Topic>(
  pagingParam => sns.listTopics({...pagingParam}).promise(),
  'Topics',
);

fetchAllByNextTokenV3

Static fetchAllByNextTokenV3<T, K>(fetchItemsByNextToken, itemsFieldName, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with nextToken based pagination which is used in aws-sdk v3. This function is useful for client side pagination when the response from AWS API contains nextToken and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
Kstring-
Parameters
NameTypeDescription
fetchItemsByNextTokenFetchItemsFunction<{ nextToken?: K }, { nextToken?: K }>the function for fetching one batch/page of items by nextToken
itemsFieldNamestringname of the field containing returned items in AWS API response
filterFunc?(entry: T) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const executions = await AwsUtils.fetchAllByNextTokenV3<ExecutionListItem>(
  (pagingParam) => this.client.send(new ListExecutionsCommand({
    stateMachineArn,
    statusFilter: status,
    ...pagingParam,
  })),
  'executions',
);

fetchAllByPosition

Static fetchAllByPosition<T, P>(fetchItemsByPosition, itemsFieldName?, filterFunc?): Promise<T[]>

Fetch all items through repeatedly calling API with position based pagination. This function is useful for client side pagination when the response from AWS API contains position and items fields.

Type parameters
NameTypeDescription
TTtype of the items returned by AWS API
Pstring-
Parameters
NameTypeDefault valueDescription
fetchItemsByPositionFetchItemsFunction<{ position?: P }, { position?: P }>undefinedthe function for fetching one batch/page of items by position
itemsFieldNamestring'items'name of the field containing returned items in AWS API response, default value is 'items'
filterFunc?(entry: T) => booleanundefinedOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<T[]>

all items fetched

Example

const domainNameObjects = await AwsUtils.fetchingAllByPosition(
  pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(),
);

fetchAllWithPagination

Static fetchAllWithPagination<IT, RT, IFN, PFN, PFT>(fetchOnePageOfItems, itemsFieldName, paginationFieldName, shouldFetchNextPage?, filterFunc?): Promise<Exclude<RT[IFN], undefined>>

Fetch all items through repeatedly calling pagination based API. This function is useful for client side pagination when the calling AWS API.

Type parameters
NameTypeDescription
ITITtype of the items returned by AWS API
RTextends Record<IFN, undefined | IT[]> & Partial<Record<PFN, PFT>>type of the response returned by AWS API
IFNextends stringname of the field containing returned items in AWS API response
PFNextends stringname of the field containing the pagination token in AWS API response, such like "ExclusiveStartKey", "Marker", "NextToken", "nextToken"
PFTstringtype of the pagination token in AWS API response, usually it is string
Parameters
NameTypeDescription
fetchOnePageOfItemsFetchItemsFunction<Partial<Record<PFN, PFT>>, RT>the function for fetching one batch/page of items by nextToken
itemsFieldNameIFNname of the field containing returned items in AWS API response
paginationFieldNamePFNname of the field containing the pagination token in AWS API response, such like "ExclusiveStartKey", "Marker", "NextToken", "nextToken"
shouldFetchNextPage?(response: RT) => booleana function to determine if the fetch should continue, the default value is always true and will continue fetching items until the response does not contain nextToken field.
filterFunc?(entry: IT) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested items in a huge number of entries, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<Exclude<RT[IFN], undefined>>

all items fetched

Example

const executions = await AwsUtils.fetchAllWithPagination(
  (pagingParam) => this.client.send(new ListExecutionsCommand({
    stateMachineArn,
    statusFilter: status,
    ...pagingParam,
  })),
  'executions',
  'nextToken',
  (resp) => resp.executions != null && resp.executions.some((exe) => exe.startDate!.getTime() <= beginTime),
);

fibonacciRetryConfigurationOptions

Static fibonacciRetryConfigurationOptions(maxRetries, base?): PartialConfigurationOptions

Generate part of a ConfigurationOptions object having maxRetries as specified and a custom RetryDelayOptions for fibonacci sequence based retry delays.

Parameters
NameTypeDefault valueDescription
maxRetriesnumberundefinedThe maximum amount of retries to perform for a service request.
basenumber100The base number of milliseconds to use in the fibonacci backoff for operation retries. Defaults to 100 ms.
Returns

PartialConfigurationOptions

part of a ConfigurationOptions object that has maxRetries as specified and a customBackoff utilising fibonacci sequence for calculating delays


parseArn

Static parseArn(arn): undefined | null | Arn & { arn: string }

Parse ARN

Parameters
NameTypeDescription
arnundefined | null | stringthe ARN string that could be null or undefined
Returns

undefined | null | Arn & { arn: string }

null or undefined if the input is null or undefined, or parsed ARN including the original ARN string


promiseWithRetry

Static promiseWithRetry<Result, TError>(operation, backoff?, statusCodes?): Promise<Result>

Perform an AWS operation (returning a Request) with retry. This function is quite handy when you are using AWS SDK v2. If you are using AWS SDK v3, use withRetry(...) instead.

The retry would happen when the error coming from AWS indicates HTTP status code 429, and has property retryable/$retryable.throttling equals to true or property name/code equals to "ThrottlingException". If you want to customise the retry logic, use PromiseUtils.withRetry(...) directly.

Type parameters
NameType
ResultResult
TErrorany
Parameters
NameTypeDescription
operation(attempt: number, previousResult: undefined | Result, previousError: undefined | TError) => WithPromiseFunction<Result>the AWS operation that returns a Request, such like () => apig.getBasePathMappings({ domainName, limit: 500 })
backoff?number[] | (attempt: number, previousResult: undefined | Result, previousError: undefined | TError) => undefined | numberArray of retry backoff periods (unit: milliseconds) or function for calculating them. If retry is desired, before making next call to the operation the desired backoff period would be waited. If the array runs out of elements or the function returns undefined, there would be no further call to the operation. The attempt argument passed into backoff function starts from 2 because only retries need to backoff, so the first retry is the second attempt. If omitted or undefined, a default backoff array will be used. In case AWS has retryDelay property in the returned error, the larger one between retryDelay and the backoff will be used.
statusCodes?null | (undefined | number)[]Array of status codes for which retry should be done. If omitted or undefined, only 429 status code could result in a retry. If it is null, status code would not be looked into. If it is an empty array, retry would never happen.
Returns

Promise<Result>

result came out from the last attempt


withRetry

Static withRetry<Result, TError>(operation, backoff?, statusCodes?): Promise<Result>

Perform an AWS operation (returning a Promise) with retry. This function is quite handy when you are using AWS SDK v3. If you are using AWS SDK v2, promiseWithRetry(...) could be more convenient.

The retry would happen when the error coming from AWS indicates HTTP status code 429, and has property retryable/$retryable.throttling equals to true or property name/code equals to "ThrottlingException". If you want to customise the retry logic, use PromiseUtils.withRetry(...) directly.

Type parameters
NameType
ResultResult
TErrorany
Parameters
NameTypeDescription
operation(attempt: number, previousResult: undefined | Result, previousError: undefined | TError) => Promise<Result>the AWS operation that returns a Promise, such like () => apig.getBasePathMappings({ domainName, limit: 500 }).promise()
backoffnumber[] | (attempt: number, previousResult: undefined | Result, previousError: undefined | TError) => undefined | numberArray of retry backoff periods (unit: milliseconds) or function for calculating them. If retry is desired, before making next call to the operation the desired backoff period would be waited. If the array runs out of elements or the function returns undefined or either the array or the function returns a negative number, there would be no further call to the operation. The attempt argument passed into backoff function starts from 2 because only retries need to backoff, so the first retry is the second attempt. If omitted or undefined, a default backoff array will be used. In case AWS has retryDelay property in the returned error, the larger one between retryDelay and the backoff will be used.
statusCodesnull | (undefined | number)[]Array of status codes for which retry should be done. If omitted or undefined, only 429 status code could result in a retry. If it is null, status code would not be looked into. If it is an empty array, retry would never happen.
Returns

Promise<Result>

result came out from the last attempt

See

promiseWithRetry

Modules

Module: aws-utils

Re-exports
Functions
Exports
Classes
Type Aliases
PossibleAwsError

Ƭ PossibleAwsError: PossibleAwsV2Error | PossibleAwsV3Error


PossibleAwsV2Error

Ƭ PossibleAwsV2Error: Error & { cfId?: string ; code: string ; extendedRequestId?: string ; hostname?: string ; message: string ; originalError?: Error ; region?: string ; requestId?: string ; retryDelay?: number ; retryable?: boolean ; statusCode?: number ; time: Date }

Possibly an error thrown from by AWS SDK v2, about a service or networking error.


PossibleAwsV3Error

Ƭ PossibleAwsV3Error: Error & { $fault: "client" | "server" ; $metadata: { httpStatusCode: number } ; $retryable?: { throttling?: boolean } }

Possibly an error thrown from by AWS SDK v3

Variables
FIBONACCI_SEQUENCE_BACKOFFS

Const FIBONACCI_SEQUENCE_BACKOFFS: number[]

Functions
awsErrorRetryable

awsErrorRetryable(error): boolean

Check whether the error thrown from AWS SDK v2 or v3 is retryable.

Parameters
NameTypeDescription
errorPossibleAwsErrorAWS error
Returns

boolean

true of retryable


awsErrorStatusCode

awsErrorStatusCode(error): number | undefined

Get the status code of the error thrown from AWS SDK v2 or v3.

Parameters
NameTypeDescription
errorPossibleAwsErrorAWS error
Returns

number | undefined

status code


isPossibleAwsError

isPossibleAwsError(error): error is PossibleAwsError

Check whether it could be an error thrown from AWS SDK v2 or v3.

Parameters
NameTypeDescription
erroranyto be checked
Returns

error is PossibleAwsError

true if it could be an error thrown from AWS SDK v2 or v3


isPossibleAwsThrottlingError

isPossibleAwsThrottlingError(error): error is PossibleAwsError

Check whether the error thrown from AWS SDK v2 or v3 is a throttling error.

Parameters
NameTypeDescription
erroranyAWS error
Returns

error is PossibleAwsError

true if it is a throttling error


isPossibleAwsV2Error

isPossibleAwsV2Error(error): error is PossibleAwsV2Error

Check whether it could be an error thrown from AWS SDK v2. Normally you should use isPossibleAwsError(...) function instead for best compatibility.

Parameters
NameTypeDescription
erroranyto be checked
Returns

error is PossibleAwsV2Error

true if it could be an error thrown from AWS SDK v2


isPossibleAwsV3Error

isPossibleAwsV3Error(error): error is PossibleAwsV3Error

Check whether it could be an error thrown from AWS SDK v3. Normally you should use isPossibleAwsError(...) function instead for best compatibility.

Parameters
NameTypeDescription
erroranyto be checked
Returns

error is PossibleAwsV3Error

true if it could be an error thrown from AWS SDK v3

Module: s3

Type Aliases
S3ObjectSummary

Ƭ S3ObjectSummary: Exclude<ListObjectsV2CommandOutput["Contents"], undefined>[0]

Functions
copyS3Object

copyS3Object(s3, srcBucket, srcEncodedKey, destDecodedKey, metadata?, destBucket?, options?): Promise<CopyObjectCommandOutput>

Copy S3 object

Parameters
NameTypeDescription
s3S3ClientS3Client
srcBucketstringbucket of the source object
srcEncodedKeystringkey (URL encoded) of the source object
destDecodedKeystringkey (NOT URL encoded) of the destination object
metadata?Record<string, string>metadata to be set on the destination object, if it is not specified then the metadata from source object will be copied
destBucket?stringbucket of the destination object, if it is not specified then the source bucket will be used
options?Partial<CopyObjectCommandInput>Additional options for the CopyObjectCommand
Returns

Promise<CopyObjectCommandOutput>

S3 command output


decodeS3ObjectKey

decodeS3ObjectKey(key): string

Decode the raw object key which is URL encoded and could contain "+" as replacement of " "

Parameters
NameTypeDescription
keystringThe raw S3 object key which is URL encoded
Returns

string

Decoded key


deleteS3Object

deleteS3Object(s3, bucket, key, options?): Promise<DeleteObjectCommandOutput>

Delete an S3 object. No error would be thrown if the object does not exist.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringbucket name
keystringobject key (without URL encoding)
options?Partial<DeleteObjectCommandInput>Additional options for the DeleteObjectCommand
Returns

Promise<DeleteObjectCommandOutput>

S3 command output


encodeS3ObjectKey

encodeS3ObjectKey(key): string

URL encode the object key, and also replace "%20" with " " and "%2F with "/" which is the convention of AWS

Parameters
NameTypeDescription
keystringThe S3 object key before encoding
Returns

string

URL encoded object key


generatePresignedUrlForDownloading

generatePresignedUrlForDownloading(s3, bucket, key, expiresIn?, options?): Promise<string>

Generate a pre-signed URL for downloading the S3 object

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringName of the bucket
keystringKey of the object
expiresIn?numberThe number of seconds before the presigned URL expires
options?Omit<GetObjectCommandInput, "Bucket" | "Key">Additional options. For example, you can specify content-disposition and content-type in it.
Returns

Promise<string>

An URL that can be used to download the S3 object.


generatePresignedUrlForUploading

generatePresignedUrlForUploading(s3, bucket, key, expiresIn?, options?): Promise<string>

Generate a pre-signed URL for uploading content to the S3 object

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringName of the bucket
keystringKey of the object
expiresIn?numberThe number of seconds before the presigned URL expires
options?Omit<PutObjectCommandInput, "Bucket" | "Key">Additional options
Returns

Promise<string>

An URL that can be used to upload content to the S3 object.


getS3Object

getS3Object(s3, bucket, key, options?): Promise<GetObjectCommandOutput | undefined>

Get the details (including the content) of the S3 object.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringbucket of the source object
keystringobject key (without URL encoding)
options?Partial<GetObjectCommandInput>Additional options for the GetObjectCommand
Returns

Promise<GetObjectCommandOutput | undefined>

details (including the content) of the S3 object. If the object does not exist, undefined will be returned.


getS3ObjectContentByteArray

getS3ObjectContentByteArray(s3, bucket, key, range?, options?): Promise<Uint8Array | undefined>

Get the content of the S3 object as a Uint8Array.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringbucket of the source object
keystringobject key (without URL encoding)
range?stringSee https://www.rfc-editor.org/rfc/rfc9110.html#name-range
options?Partial<GetObjectCommandInput>Additional options for the GetObjectCommand
Returns

Promise<Uint8Array | undefined>

Content of the S3 object as a Uint8Array. If the object does not have content, an empty Uint8Array will be returned. If the object does not exist, undefined will be returned.


getS3ObjectContentString

getS3ObjectContentString(s3, bucket, key, encoding?, options?): Promise<string | undefined>

Get the content of the S3 object as a string.

Parameters
NameTypeDefault valueDescription
s3S3ClientundefinedS3Client
bucketstringundefinedbucket of the source object
keystringundefinedobject key (without URL encoding)
encodingstring'utf8'Text encoding of the content, if not specified then "utf8" will be used
options?Partial<GetObjectCommandInput>undefinedAdditional options for the GetObjectCommand
Returns

Promise<string | undefined>

Content of the S3 object as a string. If the object does not have content, an empty string will be returned. If the object does not exist, undefined will be returned.


headS3Object

headS3Object(s3, bucket, key, treat403AsNonExisting?, options?): Promise<HeadObjectCommandOutput | undefined>

Get details of the S3 object without downloading its content.

Parameters
NameTypeDefault valueDescription
s3S3ClientundefinedS3Client
bucketstringundefinedbucket of the source object
keystringundefinedobject key (without URL encoding)
treat403AsNonExistingbooleanfalseIf this flag is true, then 403 response from AWS is considered as the object does not exist. Otherwise, only 404 response from AWS is considered as the object does not exist. Background info: If the caller does not have s3:ListBucket permission, AWS responses with 403 when the object does not exists.
options?Partial<HeadObjectCommandInput>undefinedAdditional options for the HeadObjectCommand
Returns

Promise<HeadObjectCommandOutput | undefined>

S3 command output, or undefined if the object does not exist.


listS3Objects

listS3Objects(s3, bucket, options?, filterFunc?): Promise<{ commonPrefixes: CommonPrefix[] ; contents: S3ObjectSummary[] }>

Scan S3 bucket and return both normal objects and directory objects. Directory objects have keys ending with '/'. This function handles pagination automatically.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringName of the bucket
options?Partial<ListObjectsV2CommandInput>Optional settings for the scan
filterFunc?(entry: _Object | CommonPrefix) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested objects in a bucket having huge number of object, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<{ commonPrefixes: CommonPrefix[] ; contents: S3ObjectSummary[] }>

Array of normal and directory objects found


putS3Object

putS3Object(s3, bucket, key, content, options?): Promise<PutObjectOutput>

Store content into S3. Please note that the type of the content parameter can't be a Readable (stream) with unknown length. For uploading stream with unknown length, use uploadS3Object(...) instead.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringName of the bucket
keystringKey of the object
contentundefined | StreamingBlobPayloadInputTypesContent of the object, or undefined if the object is a directory.
options?Partial<PutObjectCommandInput>Additional options
Returns

Promise<PutObjectOutput>

PutObjectOutput


scanS3Bucket

scanS3Bucket(s3, bucket, options?, filterFunc?): Promise<S3ObjectSummary[]>

Scan S3 bucket and return both normal objects and directory objects. Directory objects have keys ending with '/'. This function handles pagination automatically.

Parameters
NameTypeDescription
s3S3ClientS3Client
bucketstringName of the bucket
options?Partial<ListObjectsV2CommandInput>Optional settings for the scan
filterFunc?(entry: _Object) => booleanOptional filter function to filter out objects based on certain conditions. This function is called for each paged output during pagination. For finding few interested objects in a bucket having huge number of object, utilising this function can avoid keeping too many useless array entries in memory.
Returns

Promise<S3ObjectSummary[]>

Array of normal and directory objects found


uploadS3Object

uploadS3Object(s3, bucket, key, content, options?, uploadOptions?, setupCallback?): Promise<void>

Parameters
NameType
s3S3Client
bucketstring
keystring
contentundefined | StreamingBlobPayloadInputTypes
options?Partial<PutObjectCommandInput>
uploadOptions?Partial<Options>
setupCallback?(upload: Upload) => void
Returns

Promise<void>

Module: ssm

Functions
getSsmParameter

getSsmParameter(ssm, parameterName, fallbackValue?): Promise<string | undefined>

Get a parameter from SSM Parameter Store. If the parameter is not found and a fallbackValue is given, the fallbackValue will be returned. If the parameter is not found and there is no fallbackValue given, undefined will be returned. If any other error occurs, the error will be thrown.

Parameters
NameTypeDescription
ssmSSMSSM
parameterNamestringName/path of the parameter to get from SSM Parameter Store.
fallbackValue?stringthe value to return if the parameter is not found. If not given, undefined will be returned when the parameter is not found.
Returns

Promise<string | undefined>

Value of the parameter, or the fallbackValue, or undefined


getSsmParameterParsed

getSsmParameterParsed<T>(ssm, parameterName, fallbackValue?): Promise<T | undefined>

Get a parameter from SSM Parameter Store and return it parsed as JSON. If the parameter is not found and a fallbackValue is given, the fallbackValue will be returned. If the parameter is not found and there is no fallbackValue given, undefined will be returned. If any other error occurs, the error will be thrown.

Type parameters
Name
T
Parameters
NameTypeDescription
ssmSSMSSM
parameterNamestringName/path of the parameter to get from SSM Parameter Store.
fallbackValue?Tthe value to return if the parameter is not found. If not given, undefined will be returned when the parameter is not found.
Returns

Promise<T | undefined>

Value of the parameter parsed as JSON, or the fallbackValue, or undefined

Keywords

FAQs

Package last updated on 09 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc