Checkboxen in der ObjectTable für Mehrfachauswahl, dazu eine Aktionsleiste sobald was markiert ist. Löschen läuft über deleteObjects() in useS3Client (parallel via Promise.allSettled, S3 kann bei uns keinen echten Batch-Delete). Bestätigung läuft über das vorhandene ConfirmPopup, bei Teilfehlern gibt's eine klare Fehlermeldung statt stillem Datenverlust.
71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
import { signS3Get, signS3Put, signS3Delete, encodeCanonicalPath } from '~/utils/sigv4'
|
|
import { parseBucketsXml, parseObjectsXml, type S3Bucket, type S3Object } from '~/utils/s3xml'
|
|
|
|
export function useS3Client() {
|
|
const config = useRuntimeConfig()
|
|
|
|
async function get(path: string): Promise<string> {
|
|
const headers = await signS3Get(path, config.public.s3AccessKey, config.public.s3SecretKey)
|
|
return $fetch<string>(`/api${encodeCanonicalPath(path)}`, { headers, responseType: 'text' })
|
|
}
|
|
|
|
async function listBuckets(): Promise<S3Bucket[]> {
|
|
return parseBucketsXml(await get('/'))
|
|
}
|
|
|
|
async function listObjects(bucket: string): Promise<S3Object[]> {
|
|
return parseObjectsXml(await get(`/${bucket}`))
|
|
}
|
|
|
|
async function downloadObject(bucket: string, key: string): Promise<Blob> {
|
|
const path = `/${bucket}/${key}`
|
|
const headers = await signS3Get(path, config.public.s3AccessKey, config.public.s3SecretKey)
|
|
return $fetch<Blob>(`/api${encodeCanonicalPath(path)}`, { headers, responseType: 'blob' })
|
|
}
|
|
|
|
async function getObjectText(bucket: string, key: string): Promise<string> {
|
|
return get(`/${bucket}/${key}`)
|
|
}
|
|
|
|
async function uploadObject(bucket: string, key: string, file: File): Promise<void> {
|
|
const path = `/${bucket}/${key}`
|
|
const headers = await signS3Put(path, config.public.s3AccessKey, config.public.s3SecretKey)
|
|
await $fetch(`/api${encodeCanonicalPath(path)}`, {
|
|
method: 'PUT',
|
|
headers: { ...headers, 'Content-Type': file.type || 'application/octet-stream' },
|
|
body: file,
|
|
})
|
|
}
|
|
|
|
async function deleteObject(bucket: string, key: string): Promise<void> {
|
|
const path = `/${bucket}/${key}`
|
|
const headers = await signS3Delete(path, config.public.s3AccessKey, config.public.s3SecretKey)
|
|
await $fetch(`/api${encodeCanonicalPath(path)}`, { method: 'DELETE', headers })
|
|
}
|
|
|
|
interface BulkDeleteResult {
|
|
deleted: string[]
|
|
failed: { key: string; error: unknown }[]
|
|
}
|
|
|
|
async function deleteObjects(bucket: string, keys: string[]): Promise<BulkDeleteResult> {
|
|
const results = await Promise.allSettled(keys.map((key) => deleteObject(bucket, key)))
|
|
|
|
const deleted: string[] = []
|
|
const failed: { key: string; error: unknown }[] = []
|
|
results.forEach((result, index) => {
|
|
const key = keys[index]!
|
|
if (result.status === 'fulfilled') {
|
|
deleted.push(key)
|
|
}
|
|
else {
|
|
failed.push({ key, error: result.reason })
|
|
}
|
|
})
|
|
|
|
return { deleted, failed }
|
|
}
|
|
|
|
return { listBuckets, listObjects, downloadObject, getObjectText, uploadObject, deleteObject, deleteObjects }
|
|
}
|