// // SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // "use client" import { useState, useEffect } from "react" import { Database } from "lucide-react" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { listFilesInS3 } from "@/utils/s3-storage" import { Skeleton } from "@/components/ui/skeleton" import { toast } from "@/hooks/use-toast" export function S3Connection() { const [isConnected, setIsConnected] = useState(false) const [isLoading, setIsLoading] = useState(false) const [endpoint, setEndpoint] = useState("") const [bucket, setBucket] = useState("") const [accessKey, setAccessKey] = useState("") const [secretKey, setSecretKey] = useState("") const [fileCount, setFileCount] = useState(0) useEffect(() => { // Check if we have env variables set in localStorage const savedEndpoint = localStorage.getItem("S3_ENDPOINT") const savedBucket = localStorage.getItem("S3_BUCKET") const savedAccessKey = localStorage.getItem("S3_ACCESS_KEY") const savedSecretKey = localStorage.getItem("S3_SECRET_KEY") if (savedEndpoint) setEndpoint(savedEndpoint) if (savedBucket) setBucket(savedBucket) if (savedAccessKey) setAccessKey(savedAccessKey) if (savedSecretKey) setSecretKey(savedSecretKey) // If all values are set, check connection if (savedEndpoint && savedBucket && savedAccessKey && savedSecretKey) { checkConnection() } }, []) const saveSettings = () => { localStorage.setItem("S3_ENDPOINT", endpoint) localStorage.setItem("S3_BUCKET", bucket) localStorage.setItem("S3_ACCESS_KEY", accessKey) localStorage.setItem("S3_SECRET_KEY", secretKey) // Set these in window for runtime access window.process = window.process || {} window.process.env = window.process.env || {} window.process.env.S3_ENDPOINT = endpoint window.process.env.S3_BUCKET = bucket window.process.env.S3_ACCESS_KEY = accessKey window.process.env.S3_SECRET_KEY = secretKey } const checkConnection = async () => { setIsLoading(true) try { saveSettings() // Try to list files to verify connection const files = await listFilesInS3() setFileCount(files.length) setIsConnected(true) // Save connection status to localStorage localStorage.setItem("S3_CONNECTED", "true") // Dispatch event to notify other components window.dispatchEvent(new CustomEvent('s3ConnectionChanged', { detail: { isConnected: true } })) toast({ title: "Connected to S3", description: `Successfully connected to ${bucket} bucket. Found ${files.length} files.`, variant: "default", }) } catch (error) { console.error("Failed to connect to S3:", error) setIsConnected(false) // Save connection status to localStorage localStorage.setItem("S3_CONNECTED", "false") // Dispatch event to notify other components window.dispatchEvent(new CustomEvent('s3ConnectionChanged', { detail: { isConnected: false } })) toast({ title: "Connection Failed", description: error instanceof Error ? error.message : "Could not connect to S3 storage", variant: "destructive", }) } finally { setIsLoading(false) } } const handleConnect = (e: React.FormEvent) => { e.preventDefault() checkConnection() } return ( S3 Storage Connection Connect to an S3-compatible storage service
setEndpoint(e.target.value)} required />
setBucket(e.target.value)} required />
setAccessKey(e.target.value)} required />
setSecretKey(e.target.value)} required />
{isConnected && (
Connected
{fileCount} files in bucket
)}
) }