list all objects
This commit is contained in:
parent
b7a0376a0f
commit
2ab5640c12
45
src/s3.ts
45
src/s3.ts
@ -11,6 +11,8 @@ import {
|
|||||||
DeleteObjectCommand,
|
DeleteObjectCommand,
|
||||||
HeadObjectCommand,
|
HeadObjectCommand,
|
||||||
HeadBucketCommand,
|
HeadBucketCommand,
|
||||||
|
ListObjectsV2CommandInput,
|
||||||
|
ListObjectsV2CommandOutput,
|
||||||
} from "@aws-sdk/client-s3";
|
} from "@aws-sdk/client-s3";
|
||||||
|
|
||||||
import type { _Object } from "@aws-sdk/client-s3";
|
import type { _Object } from "@aws-sdk/client-s3";
|
||||||
@ -130,17 +132,42 @@ export const listFromRemote = async (
|
|||||||
s3Config: S3Config,
|
s3Config: S3Config,
|
||||||
prefix?: string
|
prefix?: string
|
||||||
) => {
|
) => {
|
||||||
if (prefix !== undefined) {
|
const confCmd = {
|
||||||
return await s3Client.send(
|
|
||||||
new ListObjectsV2Command({
|
|
||||||
Bucket: s3Config.s3BucketName,
|
Bucket: s3Config.s3BucketName,
|
||||||
Prefix: prefix,
|
} as ListObjectsV2CommandInput;
|
||||||
})
|
if (prefix !== undefined) {
|
||||||
);
|
confCmd.Prefix = prefix;
|
||||||
}
|
}
|
||||||
return await s3Client.send(
|
|
||||||
new ListObjectsV2Command({ Bucket: s3Config.s3BucketName })
|
const contents = [] as _Object[];
|
||||||
);
|
|
||||||
|
let isTruncated = true;
|
||||||
|
let continuationToken = "";
|
||||||
|
do {
|
||||||
|
const rsp = await s3Client.send(new ListObjectsV2Command(confCmd));
|
||||||
|
|
||||||
|
if (rsp.$metadata.httpStatusCode !== 200) {
|
||||||
|
throw Error("some thing bad while listing remote!");
|
||||||
|
}
|
||||||
|
contents.push(...rsp.Contents);
|
||||||
|
|
||||||
|
isTruncated = rsp.IsTruncated;
|
||||||
|
confCmd.ContinuationToken = rsp.NextContinuationToken;
|
||||||
|
if (
|
||||||
|
isTruncated &&
|
||||||
|
(continuationToken === undefined || continuationToken === "")
|
||||||
|
) {
|
||||||
|
throw Error("isTruncated is true but no continuationToken provided");
|
||||||
|
}
|
||||||
|
} while (isTruncated);
|
||||||
|
|
||||||
|
// ensemble fake rsp
|
||||||
|
return {
|
||||||
|
"$.metadata": {
|
||||||
|
httpStatusCode: 200,
|
||||||
|
},
|
||||||
|
Contents: contents,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user