@sqlrooms/s3-browser 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-dev.log +50 -0
- package/.turbo/turbo-lint.log +4 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE.md +9 -0
- package/dist/S3FileBrowser.d.ts +13 -0
- package/dist/S3FileBrowser.d.ts.map +1 -0
- package/dist/S3FileBrowser.js +67 -0
- package/dist/S3FileOrDirectory.d.ts +31 -0
- package/dist/S3FileOrDirectory.d.ts.map +1 -0
- package/dist/S3FileOrDirectory.js +14 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/s3.d.ts +9 -0
- package/dist/s3.d.ts.map +1 -0
- package/dist/s3.js +74 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/eslint.config.js +4 -0
- package/package.json +28 -0
- package/src/S3FileBrowser.tsx +220 -0
- package/src/S3FileOrDirectory.ts +16 -0
- package/src/index.ts +3 -0
- package/src/s3.ts +103 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @sqlrooms/s3-browser@1.0.0 dev /Users/ilya/Workspace/sqlrooms/packages/s3-browser
|
|
4
|
+
> tsc -w
|
|
5
|
+
|
|
6
|
+
[2J[3J[H[[90m7:31:02 PM[0m] Starting compilation in watch mode...
|
|
7
|
+
|
|
8
|
+
[96msrc/s3.ts[0m:[93m7[0m:[93m8[0m - [91merror[0m[90m TS2307: [0mCannot find module '@aws-sdk/client-s3' or its corresponding type declarations.
|
|
9
|
+
|
|
10
|
+
[7m7[0m } from '@aws-sdk/client-s3';
|
|
11
|
+
[7m [0m [91m ~~~~~~~~~~~~~~~~~~~~[0m
|
|
12
|
+
|
|
13
|
+
[96msrc/S3FileBrowser.tsx[0m:[93m83[0m:[93m5[0m - [91merror[0m[90m TS2590: [0mExpression produces a union type that is too complex to represent.
|
|
14
|
+
|
|
15
|
+
[7m 83[0m <Flex position="relative" width="100%" height="100%" overflow="hidden">
|
|
16
|
+
[7m [0m [91m ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~[0m
|
|
17
|
+
[7m 84[0m <Flex
|
|
18
|
+
[7m [0m [91m~~~~~~~~~~~[0m
|
|
19
|
+
[7m...[0m
|
|
20
|
+
[7m245[0m </Flex>
|
|
21
|
+
[7m [0m [91m~~~~~~~~~~~~~[0m
|
|
22
|
+
[7m246[0m </Flex>
|
|
23
|
+
[7m [0m [91m~~~~~~~~~~~[0m
|
|
24
|
+
|
|
25
|
+
[96m../utils/src/color.ts[0m:[93m1[0m:[93m32[0m - [91merror[0m[90m TS7016: [0mCould not find a declaration file for module 'd3-color'. '/Users/ilya/Workspace/sqlrooms/node_modules/.pnpm/d3-color@3.1.0/node_modules/d3-color/src/index.js' implicitly has an 'any' type.
|
|
26
|
+
Try `npm i --save-dev @types/d3-color` if it exists or add a new declaration (.d.ts) file containing `declare module 'd3-color';`
|
|
27
|
+
|
|
28
|
+
[7m1[0m import {color as d3color} from 'd3-color';
|
|
29
|
+
[7m [0m [91m ~~~~~~~~~~[0m
|
|
30
|
+
|
|
31
|
+
[96m../utils/src/format.ts[0m:[93m1[0m:[93m27[0m - [91merror[0m[90m TS7016: [0mCould not find a declaration file for module 'd3-format'. '/Users/ilya/Workspace/sqlrooms/node_modules/d3-format/src/index.js' implicitly has an 'any' type.
|
|
32
|
+
Try `npm i --save-dev @types/d3-format` if it exists or add a new declaration (.d.ts) file containing `declare module 'd3-format';`
|
|
33
|
+
|
|
34
|
+
[7m1[0m import * as d3Format from 'd3-format';
|
|
35
|
+
[7m [0m [91m ~~~~~~~~~~~[0m
|
|
36
|
+
|
|
37
|
+
[96m../utils/src/helpers.ts[0m:[93m1[0m:[93m26[0m - [91merror[0m[90m TS7016: [0mCould not find a declaration file for module 'd3-time-format'. '/Users/ilya/Workspace/sqlrooms/node_modules/.pnpm/d3-time-format@3.0.0/node_modules/d3-time-format/dist/d3-time-format.js' implicitly has an 'any' type.
|
|
38
|
+
Try `npm i --save-dev @types/d3-time-format` if it exists or add a new declaration (.d.ts) file containing `declare module 'd3-time-format';`
|
|
39
|
+
|
|
40
|
+
[7m1[0m import {timeFormat} from 'd3-time-format';
|
|
41
|
+
[7m [0m [91m ~~~~~~~~~~~~~~~~[0m
|
|
42
|
+
|
|
43
|
+
[96m../utils/src/helpers.ts[0m:[93m2[0m:[93m20[0m - [91merror[0m[90m TS7016: [0mCould not find a declaration file for module 'seedrandom'. '/Users/ilya/Workspace/sqlrooms/node_modules/.pnpm/seedrandom@3.0.5/node_modules/seedrandom/index.js' implicitly has an 'any' type.
|
|
44
|
+
Try `npm i --save-dev @types/seedrandom` if it exists or add a new declaration (.d.ts) file containing `declare module 'seedrandom';`
|
|
45
|
+
|
|
46
|
+
[7m2[0m import {alea} from 'seedrandom';
|
|
47
|
+
[7m [0m [91m ~~~~~~~~~~~~[0m
|
|
48
|
+
|
|
49
|
+
[[90m7:31:06 PM[0m] Found 6 errors. Watching for file changes.
|
|
50
|
+
|
package/CHANGELOG.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Ilya Boyandin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { S3FileOrDirectory } from './S3FileOrDirectory';
|
|
3
|
+
type Props = {
|
|
4
|
+
files?: S3FileOrDirectory[];
|
|
5
|
+
selectedFiles: string[];
|
|
6
|
+
selectedDirectory: string;
|
|
7
|
+
onCanConfirmChange: (canConfirm: boolean) => void;
|
|
8
|
+
onChangeSelectedDirectory: (directory: string) => void;
|
|
9
|
+
onChangeSelectedFiles: (files: string[]) => void;
|
|
10
|
+
};
|
|
11
|
+
declare const S3FileBrowser: FC<Props>;
|
|
12
|
+
export default S3FileBrowser;
|
|
13
|
+
//# sourceMappingURL=S3FileBrowser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"S3FileBrowser.d.ts","sourceRoot":"","sources":["../src/S3FileBrowser.tsx"],"names":[],"mappings":"AAkBA,OAAO,EAAC,EAAE,EAAkC,MAAM,OAAO,CAAC;AAC1D,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AAEtD,KAAK,KAAK,GAAG;IACX,KAAK,CAAC,EAAE,iBAAiB,EAAE,CAAC;IAC5B,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,kBAAkB,EAAE,CAAC,UAAU,EAAE,OAAO,KAAK,IAAI,CAAC;IAClD,yBAAyB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IACvD,qBAAqB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;CAClD,CAAC;AAEF,QAAA,MAAM,aAAa,EAAE,EAAE,CAAC,KAAK,CA2L5B,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbSeparator, Button, Checkbox, Table, TableBody, TableCell, TableHead, TableHeader, TableRow, cn, } from '@sqlrooms/ui';
|
|
3
|
+
import { Undo2Icon, FolderIcon } from 'lucide-react';
|
|
4
|
+
import { formatBytes, formatTimeRelative } from '@sqlrooms/utils';
|
|
5
|
+
import { useCallback, useEffect, useMemo } from 'react';
|
|
6
|
+
const S3FileBrowser = (props) => {
|
|
7
|
+
const { files, selectedDirectory, selectedFiles, onCanConfirmChange, onChangeSelectedFiles, onChangeSelectedDirectory, } = props;
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
onCanConfirmChange(Boolean(selectedFiles?.length));
|
|
10
|
+
}, [selectedFiles, onCanConfirmChange]);
|
|
11
|
+
const handleSelectFile = useCallback((key) => {
|
|
12
|
+
if (selectedFiles.includes(key)) {
|
|
13
|
+
onChangeSelectedFiles(selectedFiles.filter((id) => id !== key));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
onChangeSelectedFiles([...selectedFiles, key]);
|
|
17
|
+
}
|
|
18
|
+
}, [onChangeSelectedFiles, selectedFiles]);
|
|
19
|
+
const handleSelectDirectory = useCallback((key) => {
|
|
20
|
+
onChangeSelectedDirectory(`${selectedDirectory}${key}/`);
|
|
21
|
+
}, [selectedDirectory, onChangeSelectedDirectory]);
|
|
22
|
+
const filesInDirectory = useMemo(() => files?.filter(({ isDirectory }) => !isDirectory) ?? [], [files]);
|
|
23
|
+
const handleSelectAll = useCallback(() => {
|
|
24
|
+
if (selectedFiles.length === filesInDirectory.length) {
|
|
25
|
+
onChangeSelectedFiles([]);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
onChangeSelectedFiles(filesInDirectory.map(({ key }) => key) ?? []);
|
|
29
|
+
}
|
|
30
|
+
}, [filesInDirectory, onChangeSelectedFiles, selectedFiles.length]);
|
|
31
|
+
const parentDirectory = useMemo(() => {
|
|
32
|
+
const dir = selectedDirectory.split('/').slice(0, -2).join('/');
|
|
33
|
+
return dir ? `${dir}/` : '';
|
|
34
|
+
}, [selectedDirectory]);
|
|
35
|
+
return (_jsx("div", { className: "relative w-full h-full overflow-hidden", children: _jsx("div", { className: "absolute w-full h-full overflow-x-auto overflow-y-auto flex flex-col py-0 items-start", children: _jsx("div", { className: "w-full rounded-lg border border-gray-600 overflow-y-auto", children: _jsxs(Table, { disableWrapper: true, children: [_jsxs(TableHeader, { children: [selectedDirectory ? (_jsx(TableRow, { children: _jsx(TableCell, { colSpan: 5, className: "py-3 text-gray-100 bg-gray-800", children: _jsxs("div", { className: "flex gap-2 items-center", children: [_jsxs(Button, { size: "sm", variant: "outline", onClick: () => onChangeSelectedDirectory(parentDirectory), children: [_jsx(Undo2Icon, { className: "w-3 h-3 mr-1" }), ".."] }), _jsx(Breadcrumb, { children: _jsxs(BreadcrumbList, { children: [_jsx(BreadcrumbItem, { children: _jsx(BreadcrumbLink, { onClick: () => onChangeSelectedDirectory(''), className: "text-xs text-blue-400", children: "Home" }) }), selectedDirectory.split('/').map((directory, i) => {
|
|
36
|
+
if (!directory)
|
|
37
|
+
return null;
|
|
38
|
+
const path = selectedDirectory
|
|
39
|
+
.split('/')
|
|
40
|
+
.slice(0, i + 1)
|
|
41
|
+
.join('/')
|
|
42
|
+
.concat('/');
|
|
43
|
+
const isCurrent = path === selectedDirectory;
|
|
44
|
+
return (_jsxs(BreadcrumbItem, { children: [_jsx(BreadcrumbSeparator, {}), _jsx(BreadcrumbLink, { className: cn('text-xs text-blue-400', isCurrent &&
|
|
45
|
+
'cursor-default hover:no-underline'), onClick: () => {
|
|
46
|
+
if (!isCurrent) {
|
|
47
|
+
onChangeSelectedDirectory(path);
|
|
48
|
+
}
|
|
49
|
+
}, children: directory })] }, i));
|
|
50
|
+
})] }) })] }) }) })) : null, _jsxs(TableRow, { className: "sticky top-0 z-[2] bg-gray-600", children: [_jsx(TableHead, { className: "w-[1%]", children: _jsx(Checkbox, { checked: selectedFiles.length === filesInDirectory.length, onCheckedChange: handleSelectAll }) }), _jsx(TableHead, { className: "py-2 text-white", children: "Name" }), _jsx(TableHead, { className: "py-2 text-white", children: "Type" }), _jsx(TableHead, { className: "text-white text-right", children: "Size" }), _jsx(TableHead, { className: "text-white text-right", children: "Modified" })] })] }), _jsx(TableBody, { children: files?.map((object) => {
|
|
51
|
+
const { key, isDirectory } = object;
|
|
52
|
+
return (_jsxs(TableRow, { className: "cursor-pointer text-blue-300 hover:bg-blue-700 hover:text-white", onClick: (evt) => {
|
|
53
|
+
if (isDirectory) {
|
|
54
|
+
handleSelectDirectory(key);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
handleSelectFile(key);
|
|
58
|
+
evt.preventDefault(); // prevent double change when clicking checkbox
|
|
59
|
+
}
|
|
60
|
+
}, children: [_jsx(TableCell, { children: _jsx(Checkbox, { disabled: isDirectory, checked: selectedFiles.includes(key) }) }), _jsx(TableCell, { className: "text-xs", children: isDirectory ? (_jsxs("div", { className: "flex items-center gap-2", children: [_jsx(FolderIcon, { className: "w-4 h-4" }), _jsx("span", { children: `${key}/` })] })) : (key) }), _jsx(TableCell, { className: "text-xs", children: isDirectory ? 'Directory' : object.contentType }), _jsx(TableCell, { className: "text-xs text-right", children: !isDirectory && object.size !== undefined
|
|
61
|
+
? formatBytes(object.size)
|
|
62
|
+
: '' }), _jsx(TableCell, { className: "text-xs text-right", children: !isDirectory && object.lastModified
|
|
63
|
+
? formatTimeRelative(object.lastModified)
|
|
64
|
+
: '' })] }, key));
|
|
65
|
+
}) })] }) }) }) }));
|
|
66
|
+
};
|
|
67
|
+
export default S3FileBrowser;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const S3FileOrDirectory: z.ZodUnion<[z.ZodObject<{
|
|
3
|
+
key: z.ZodString;
|
|
4
|
+
isDirectory: z.ZodLiteral<true>;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
key: string;
|
|
7
|
+
isDirectory: true;
|
|
8
|
+
}, {
|
|
9
|
+
key: string;
|
|
10
|
+
isDirectory: true;
|
|
11
|
+
}>, z.ZodObject<{
|
|
12
|
+
key: z.ZodString;
|
|
13
|
+
isDirectory: z.ZodLiteral<false>;
|
|
14
|
+
lastModified: z.ZodOptional<z.ZodDate>;
|
|
15
|
+
size: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
contentType: z.ZodOptional<z.ZodString>;
|
|
17
|
+
}, "strip", z.ZodTypeAny, {
|
|
18
|
+
key: string;
|
|
19
|
+
isDirectory: false;
|
|
20
|
+
size?: number | undefined;
|
|
21
|
+
lastModified?: Date | undefined;
|
|
22
|
+
contentType?: string | undefined;
|
|
23
|
+
}, {
|
|
24
|
+
key: string;
|
|
25
|
+
isDirectory: false;
|
|
26
|
+
size?: number | undefined;
|
|
27
|
+
lastModified?: Date | undefined;
|
|
28
|
+
contentType?: string | undefined;
|
|
29
|
+
}>]>;
|
|
30
|
+
export type S3FileOrDirectory = z.infer<typeof S3FileOrDirectory>;
|
|
31
|
+
//# sourceMappingURL=S3FileOrDirectory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"S3FileOrDirectory.d.ts","sourceRoot":"","sources":["../src/S3FileOrDirectory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;IAY5B,CAAC;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export const S3FileOrDirectory = z.union([
|
|
3
|
+
z.object({
|
|
4
|
+
key: z.string(),
|
|
5
|
+
isDirectory: z.literal(true),
|
|
6
|
+
}),
|
|
7
|
+
z.object({
|
|
8
|
+
key: z.string(),
|
|
9
|
+
isDirectory: z.literal(false),
|
|
10
|
+
lastModified: z.date().optional(),
|
|
11
|
+
size: z.number().optional(),
|
|
12
|
+
contentType: z.string().optional(),
|
|
13
|
+
}),
|
|
14
|
+
]);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,aAAa,EAAC,MAAM,iBAAiB,CAAC;AACzD,cAAc,MAAM,CAAC;AACrB,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
package/dist/s3.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import { S3FileOrDirectory } from './S3FileOrDirectory';
|
|
3
|
+
export declare function listFilesAndDirectoriesWithPrefix(S3: S3Client, bucket: string, prefix?: string): Promise<S3FileOrDirectory[]>;
|
|
4
|
+
/**
|
|
5
|
+
* Delete all files with the given prefix
|
|
6
|
+
* @param prefix
|
|
7
|
+
*/
|
|
8
|
+
export declare function deleteS3Files(S3: S3Client, bucket: string, prefix: string): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=s3.d.ts.map
|
package/dist/s3.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3.d.ts","sourceRoot":"","sources":["../src/s3.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,QAAQ,EACT,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AAEtD,wBAAsB,iCAAiC,CACrD,EAAE,EAAE,QAAQ,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAwD9B;AAgBD;;;GAGG;AACH,wBAAsB,aAAa,CACjC,EAAE,EAAE,QAAQ,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,iBAUf"}
|
package/dist/s3.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { DeleteObjectCommand, HeadObjectCommand, ListObjectsCommand, ListObjectsV2Command, } from '@aws-sdk/client-s3';
|
|
2
|
+
export async function listFilesAndDirectoriesWithPrefix(S3, bucket, prefix) {
|
|
3
|
+
const command = new ListObjectsV2Command({
|
|
4
|
+
Bucket: bucket,
|
|
5
|
+
Prefix: prefix ? `${prefix}${prefix.endsWith('/') ? '' : '/'}` : '',
|
|
6
|
+
Delimiter: '/',
|
|
7
|
+
});
|
|
8
|
+
const response = await S3.send(command);
|
|
9
|
+
const objects = [];
|
|
10
|
+
const removePrefix = (key) => {
|
|
11
|
+
if (!prefix) {
|
|
12
|
+
return key;
|
|
13
|
+
}
|
|
14
|
+
return key.replace(prefix ?? '', '');
|
|
15
|
+
};
|
|
16
|
+
if (response.CommonPrefixes) {
|
|
17
|
+
for (const commonPrefix of response.CommonPrefixes) {
|
|
18
|
+
if (commonPrefix.Prefix) {
|
|
19
|
+
// Extract the directory name from the CommonPrefixes
|
|
20
|
+
const directoryName = removePrefix(commonPrefix.Prefix).slice(0, -1);
|
|
21
|
+
objects.push({ key: directoryName, isDirectory: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (response.Contents) {
|
|
26
|
+
for (const content of response.Contents) {
|
|
27
|
+
const key = content.Key;
|
|
28
|
+
if (key) {
|
|
29
|
+
// Exclude subdirectories by checking if the Key ends with '/'
|
|
30
|
+
if (!key.endsWith('/')) {
|
|
31
|
+
const fileName = removePrefix(key);
|
|
32
|
+
const headCommand = new HeadObjectCommand({
|
|
33
|
+
Bucket: bucket,
|
|
34
|
+
Key: key,
|
|
35
|
+
});
|
|
36
|
+
const headResponse = await S3.send(headCommand);
|
|
37
|
+
objects.push({
|
|
38
|
+
key: fileName,
|
|
39
|
+
isDirectory: false,
|
|
40
|
+
lastModified: content.LastModified,
|
|
41
|
+
size: content.Size,
|
|
42
|
+
contentType: headResponse.ContentType,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return objects;
|
|
49
|
+
}
|
|
50
|
+
// async function listBucketContents(
|
|
51
|
+
// prefix: string,
|
|
52
|
+
// ): Promise<ListObjectsCommandOutput> {
|
|
53
|
+
// if (!prefix.length) {
|
|
54
|
+
// throw new Error('Prefix cannot be empty');
|
|
55
|
+
// }
|
|
56
|
+
// const listObjectsCommand = new ListObjectsCommand({
|
|
57
|
+
// Bucket: S3_BUCKET_NAME,
|
|
58
|
+
// Prefix: `${prefix}/`,
|
|
59
|
+
// });
|
|
60
|
+
// const response = await S3.send(listObjectsCommand);
|
|
61
|
+
// return response;
|
|
62
|
+
// }
|
|
63
|
+
/**
|
|
64
|
+
* Delete all files with the given prefix
|
|
65
|
+
* @param prefix
|
|
66
|
+
*/
|
|
67
|
+
export async function deleteS3Files(S3, bucket, prefix) {
|
|
68
|
+
const data = await S3.send(new ListObjectsCommand({ Bucket: bucket, Prefix: `${prefix}/` }));
|
|
69
|
+
if (data.Contents?.length) {
|
|
70
|
+
for (const obj of data.Contents) {
|
|
71
|
+
await S3.send(new DeleteObjectCommand({ Bucket: bucket, Key: obj.Key }));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|