Last active
October 4, 2022 17:56
-
-
Save ebuildy/7ac807fd017452dfaf3b9c9b10ff3b52 to your computer and use it in GitHub Desktop.
Play with AWS S3 API on javascript
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
public async deleteKeys(keys: string[]): Promise<any[]> { | |
function spliceIntoChunks(arr: any[], chunkSize: number) { | |
const res = []; | |
while (arr.length > 0) { | |
const chunk = arr.splice(0, chunkSize); | |
res.push(chunk); | |
} | |
return res; | |
} | |
const allKeysToRemovePromises = keys.map(k => this.listObjectsRecursive(k)) | |
const allKeysToRemove = (await Promise.all(allKeysToRemovePromises)).flatMap(k => k) | |
const allKeysToRemoveGroups = spliceIntoChunks(allKeysToRemove, 3) | |
const deletePromises = allKeysToRemoveGroups.map(group => { | |
return this.client.send( | |
new DeleteObjectsCommand({ | |
Bucket: this.bucket.name, | |
Delete: { | |
Objects: group.map(({Key}) => { | |
return { | |
Key | |
} | |
}) | |
} | |
}) | |
) | |
}) | |
const results = await Promise.all(deletePromises) | |
return results.flatMap(({$metadata, Deleted}) => { | |
return Deleted.map(({Key}) => { | |
return { | |
status: $metadata.httpStatusCode, | |
key: Key | |
} | |
}) | |
}) | |
} |
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 { ListObjectsV2Command, S3Client, S3ClientConfig } from "@aws-sdk/client-s3" | |
/** | |
* Get all keys recurively | |
* @param Prefix | |
* @returns | |
*/ | |
public async listObjectsRecursive(Prefix: string, ContinuationToken?: string): Promise< | |
any[] | |
> { | |
// Get objects for current prefix | |
const listObjects = await this.client.send( | |
new ListObjectsV2Command({ | |
Delimiter: "/", | |
Bucket: this.bucket.name, | |
Prefix, | |
ContinuationToken | |
}) | |
); | |
let deepFiles, nextFiles | |
// Recurive call to get sub prefixes | |
if (listObjects.CommonPrefixes) { | |
const deepFilesPromises = listObjects.CommonPrefixes.flatMap(({Prefix}) => { | |
return this.listObjectsRecursive(Prefix) | |
}) | |
deepFiles = (await Promise.all(deepFilesPromises)).flatMap(t => t) | |
} | |
// If we must paginate | |
if (listObjects.IsTruncated) { | |
nextFiles = await this.listObjectsRecursive(Prefix, listObjects.NextContinuationToken) | |
} | |
return [ | |
...(listObjects.Contents || []), | |
...(deepFiles || []), | |
...(nextFiles || []) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment