- Checkboxen in der Objekttabelle inkl. Select-All pro Bucket-Ansicht - BulkActionBar zeigt Anzahl Auswahl + Aktionen (Verschieben/Löschen/Aufheben) - BulkDeletePopup: löscht ausgewählte Objekte einzeln, mit Retry bei Teilfehlern - BulkMovePopup: verschiebt in beliebigen Zielbucket/-pfad via Server-Side-Copy + Delete - sigv4: signS3Copy für x-amz-copy-source PUT-Requests, Header werden jetzt sortiert statt auf Insertion-Order zu vertrauen - useS3Client: copyObject/moveObject ergänzt Move ist client-seitig Copy+Delete, da S3 kein natives Rename/Move kennt.
110 lines
4.0 KiB
TypeScript
110 lines
4.0 KiB
TypeScript
const REGION = 'us-east-1'
|
|
const SERVICE = 's3'
|
|
|
|
export interface SignedHeaders {
|
|
Authorization: string
|
|
'X-Amz-Date': string
|
|
'X-Amz-Content-Sha256': string
|
|
}
|
|
|
|
export async function signS3Get(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
|
return signS3('GET', path, await sha256Hex(''), accessKey, secretKey)
|
|
}
|
|
|
|
export async function signS3Put(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
|
return signS3('PUT', path, 'UNSIGNED-PAYLOAD', accessKey, secretKey)
|
|
}
|
|
|
|
export async function signS3Delete(path: string, accessKey: string, secretKey: string): Promise<SignedHeaders> {
|
|
return signS3('DELETE', path, await sha256Hex(''), accessKey, secretKey)
|
|
}
|
|
|
|
export async function signS3Copy(path: string, copySource: string, accessKey: string, secretKey: string): Promise<SignedHeaders & { 'x-amz-copy-source': string }> {
|
|
const headers = await signS3('PUT', path, await sha256Hex(''), accessKey, secretKey, { 'x-amz-copy-source': encodeCanonicalPath(copySource) })
|
|
return { ...headers, 'x-amz-copy-source': encodeCanonicalPath(copySource) }
|
|
}
|
|
|
|
async function signS3(
|
|
method: string,
|
|
path: string,
|
|
payloadHash: string,
|
|
accessKey: string,
|
|
secretKey: string,
|
|
extraHeaders: Record<string, string> = {},
|
|
): Promise<SignedHeaders> {
|
|
const amzDate = toAmzDate(new Date())
|
|
const dateStamp = amzDate.slice(0, 8)
|
|
|
|
const signedHeaderValues: Record<string, string> = {
|
|
...extraHeaders,
|
|
'x-amz-content-sha256': payloadHash,
|
|
'x-amz-date': amzDate,
|
|
}
|
|
const sortedHeaderNames = Object.keys(signedHeaderValues).sort()
|
|
const signedHeaders = sortedHeaderNames.join(';')
|
|
const canonicalHeaders = sortedHeaderNames
|
|
.map((name) => `${name}:${signedHeaderValues[name]}\n`)
|
|
.join('')
|
|
|
|
const canonicalRequest = [method, encodeCanonicalPath(path), '', canonicalHeaders, signedHeaders, payloadHash].join('\n')
|
|
const credentialScope = `${dateStamp}/${REGION}/${SERVICE}/aws4_request`
|
|
const stringToSign = [
|
|
'AWS4-HMAC-SHA256',
|
|
amzDate,
|
|
credentialScope,
|
|
await sha256Hex(canonicalRequest),
|
|
].join('\n')
|
|
|
|
const key = await deriveSigningKey(secretKey, dateStamp)
|
|
const signature = bufferToHex(await hmac(key, stringToSign))
|
|
|
|
return {
|
|
Authorization: `AWS4-HMAC-SHA256 Credential=${accessKey}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`,
|
|
'X-Amz-Date': amzDate,
|
|
'X-Amz-Content-Sha256': payloadHash,
|
|
}
|
|
}
|
|
|
|
function toAmzDate(date: Date): string {
|
|
return date.toISOString().replace(/[:-]|\.\d{3}/g, '')
|
|
}
|
|
|
|
function isUnreservedByte(byte: number): boolean {
|
|
return (byte >= 0x41 && byte <= 0x5a) // A-Z
|
|
|| (byte >= 0x61 && byte <= 0x7a) // a-z
|
|
|| (byte >= 0x30 && byte <= 0x39) // 0-9
|
|
|| byte === 0x2d || byte === 0x5f || byte === 0x2e || byte === 0x7e // - _ . ~
|
|
}
|
|
|
|
export function encodeCanonicalPath(path: string): string {
|
|
return path
|
|
.split('/')
|
|
.map((segment) => Array.from(new TextEncoder().encode(segment))
|
|
.map((byte) => (isUnreservedByte(byte) ? String.fromCharCode(byte) : `%${byte.toString(16).toUpperCase().padStart(2, '0')}`))
|
|
.join(''))
|
|
.join('/')
|
|
}
|
|
|
|
async function deriveSigningKey(secretKey: string, dateStamp: string): Promise<ArrayBuffer> {
|
|
const kDate = await hmac(new TextEncoder().encode(`AWS4${secretKey}`), dateStamp)
|
|
const kRegion = await hmac(kDate, REGION)
|
|
const kService = await hmac(kRegion, SERVICE)
|
|
return hmac(kService, 'aws4_request')
|
|
}
|
|
|
|
async function hmac(key: BufferSource, data: string): Promise<ArrayBuffer> {
|
|
const cryptoKey = await crypto.subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'])
|
|
return crypto.subtle.sign('HMAC', cryptoKey, new TextEncoder().encode(data))
|
|
}
|
|
|
|
async function sha256Hex(data: string): Promise<string> {
|
|
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data))
|
|
return bufferToHex(digest)
|
|
}
|
|
|
|
function bufferToHex(buffer: ArrayBuffer): string {
|
|
return Array.from(new Uint8Array(buffer))
|
|
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
.join('')
|
|
}
|