import React from 'react'; import { Link, useParams } from 'react-router-dom'; import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3'; import Alert from 'react-bootstrap/Alert'; import Breadcrumb from 'react-bootstrap/Breadcrumb'; import Button from 'react-bootstrap/Button'; import Container from 'react-bootstrap/Container'; import Card from 'react-bootstrap/Card'; import ListGroup from 'react-bootstrap/ListGroup'; import Stack from 'react-bootstrap/Stack'; import { LinkContainer } from 'react-router-bootstrap' import ObjectInfo from './ObjectInfo'; import UploadFiles from './UploadFiles'; import downloadFile from './downloadFile'; import { BsFolder, BsFileEarmarkText, BsDownload } from 'react-icons/bs'; type Props = { client: S3Client; bucket: string; prefix: string; }; type State = { loaded: boolean; folders: string[]; files: string[]; info: string | null; iInfo: number; iUpload: number; }; var cache: { [path: string]: State; } = {}; class ObjectList extends React.Component { state = { loaded: false, folders: [], files: [], info: null, iInfo: 0, iUpload: 0, }; constructor(props: Props) { super(props); } componentDidMount() { //console.log(this.props); if (cache[this.path()]) { this.setState(cache[this.path()]); } this.loadEntries(); } async loadEntries() { let command = new ListObjectsV2Command({ Bucket: this.props.bucket, Prefix: this.props.prefix, Delimiter: '/', }); try { const pxlen = this.props.prefix.length; const data = await this.props.client.send(command); //console.log("ok", data); const folders = (data.CommonPrefixes || []).map((cp) => cp.Prefix!.substring(pxlen)); const files = (data.Contents || []).map((obj) => obj.Key!.substring(pxlen)); folders.sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'})); files.sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'})); this.setState({ loaded: true, folders: folders, files: files, }); cache[this.path()] = this.state; } catch(error) { console.log("err", error); } } path() { return this.props.bucket + "/" + this.props.prefix; } renderBreadcrumbs() { let spl = this.props.prefix.split("/"); let items = []; for (var i = 0; i < spl.length - 1; i++) { let to = "/" + this.props.bucket + "/" + spl.slice(0, i+1).join("/") + "/" ; if (i < spl.length - 2) { items.push( { spl[i] } ); } else { items.push( { spl[i] } ); } } return ( my buckets { this.props.prefix == "" ? { this.props.bucket } : { this.props.bucket } } { items } ); } openInfo(f: string) { this.setState({info: f, iInfo: this.state.iInfo + 1}); } openUpload() { this.setState({iUpload: this.state.iUpload + 1}); } onUploadComplete() { this.loadEntries(); } render() { if (!this.state.loaded) { return ( <> { this.renderBreadcrumbs() } Loading... ); } return ( <> { this.state.iUpload > 0 ? this.onUploadComplete()} /> : <> } { this.state.info ? : <> }
{ this.renderBreadcrumbs() }
{ this.state.folders.map((f) => { f } )} { this.state.files.map((f) => this.openInfo(f)}>
{ f }
)}
); } } function isBlob(obj: any): obj is Blob { return !! obj; } interface IClient { client: S3Client; } export const ObjectList1 = ({ client }: IClient) => { const params = useParams(); const bucket = params["bucket"]!; return <> ; }; export const ObjectList2 = ({ client }: IClient) => { const params = useParams(); const bucket = params["bucket"]!; const prefix = params["*"] || ""; const key = bucket + "/" + prefix; return <> ; };