Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix: storage throttle issues when uploading large number of files (#40445)

Ali Waseemd9e585146722
diff --git a/apps/studio/data/api-keys/temp-api-keys-utils.test.ts b/apps/studio/data/api-keys/temp-api-keys-utils.test.ts
new file mode 100644
index 00000000000..b4ab3d910e4
--- /dev/null
+++ b/apps/studio/data/api-keys/temp-api-keys-utils.test.ts
@@ -0,0 +1,298 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import {
+ createTemporaryUploadKey,
+ isTemporaryUploadKeyValid,
+ type TemporaryUploadKey,
+} from './temp-api-keys-utils'
+
+describe('createTemporaryUploadKey', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('should create a temporary upload key with correct apiKey', () => {
+ const apiKey = 'test-api-key-123'
+ const expiryInSeconds = 3600
+
+ const result = createTemporaryUploadKey(apiKey, expiryInSeconds)
+
+ expect(result.apiKey).toBe(apiKey)
+ })
+
+ it('should create a temporary upload key with expiry time 1 hour in the future', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const apiKey = 'test-api-key-123'
+ const expiryInSeconds = 3600 // 1 hour
+
+ const result = createTemporaryUploadKey(apiKey, expiryInSeconds)
+
+ expect(result.expiryTime).toBe(now + 3600 * 1000)
+ })
+
+ it('should handle short expiry durations', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const apiKey = 'test-api-key-123'
+ const expiryInSeconds = 60 // 1 minute
+
+ const result = createTemporaryUploadKey(apiKey, expiryInSeconds)
+
+ expect(result.expiryTime).toBe(now + 60 * 1000)
+ })
+
+ it('should create keys with different expiry times when called at different times', () => {
+ const apiKey = 'test-api-key-123'
+ const expiryInSeconds = 3600
+
+ const now1 = 1000000
+ vi.setSystemTime(now1)
+ const result1 = createTemporaryUploadKey(apiKey, expiryInSeconds)
+
+ const now2 = 2000000
+ vi.setSystemTime(now2)
+ const result2 = createTemporaryUploadKey(apiKey, expiryInSeconds)
+
+ expect(result1.expiryTime).toBe(now1 + expiryInSeconds * 1000)
+ expect(result2.expiryTime).toBe(now2 + expiryInSeconds * 1000)
+ expect(result1.expiryTime).not.toBe(result2.expiryTime)
+ })
+})
+
+describe('isTemporaryUploadKeyValid', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('should return false for null key', () => {
+ const result = isTemporaryUploadKeyValid(null)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return false for undefined key', () => {
+ const result = isTemporaryUploadKeyValid(undefined)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return true for a key with more than 60 seconds remaining', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now + 120000, // 2 minutes from now
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(true)
+ })
+
+ it('should return false for a key with exactly 60 seconds remaining', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now + 60000, // Exactly 60 seconds
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return false for a key with less than 60 seconds remaining', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now + 30000, // 30 seconds from now
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return false for an expired key', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now - 1000, // 1 second ago
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return false for a key that expired long ago', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now - 3600000, // 1 hour ago
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(false)
+ })
+
+ it('should return true for a key with exactly 61 seconds remaining', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now + 61000, // 61 seconds from now
+ }
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ expect(result).toBe(true)
+ })
+
+ it('should handle time advancing correctly', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key: TemporaryUploadKey = {
+ apiKey: 'test-key',
+ expiryTime: now + 120000, // 2 minutes from now
+ }
+
+ // Initially valid
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+
+ // Advance time by 59 seconds (should still be valid - 61 seconds remaining)
+ vi.advanceTimersByTime(59000)
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+
+ // Advance time by 2 more seconds (should be invalid - 59 seconds remaining)
+ vi.advanceTimersByTime(2000)
+ expect(isTemporaryUploadKeyValid(key)).toBe(false)
+ })
+
+ it('should return true for a key missing apiKey property', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key = {
+ expiryTime: now + 120000,
+ } as TemporaryUploadKey
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ // While the key has expiryTime, it's missing apiKey, but since we're checking
+ // the structure, we expect it to still pass the time check if the expiryTime exists
+ // Actually, the function only checks time, not the apiKey presence
+ // Let's verify the actual behavior
+ expect(result).toBe(true)
+ })
+
+ it('should return false for a key missing expiryTime property', () => {
+ const key = {
+ apiKey: 'test-key',
+ } as TemporaryUploadKey
+
+ const result = isTemporaryUploadKeyValid(key)
+
+ // Without expiryTime, the calculation will be NaN and fail the > 60000 check
+ expect(result).toBe(false)
+ })
+})
+
+describe('integration: createTemporaryUploadKey and isTemporaryUploadKeyValid', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('should create a key that is immediately valid', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const key = createTemporaryUploadKey('test-api-key', 3600)
+
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+ })
+
+ it('should create a key that becomes invalid after expiry time minus 60 seconds', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ const expiryInSeconds = 120 // 2 minutes
+ const key = createTemporaryUploadKey('test-api-key', expiryInSeconds)
+
+ // Initially valid
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+
+ // Advance to 59 seconds before expiry (should still be valid - 61 seconds remaining)
+ vi.advanceTimersByTime((expiryInSeconds - 61) * 1000)
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+
+ // Advance to 60 seconds before expiry (should be invalid - 60 seconds remaining)
+ vi.advanceTimersByTime(1000)
+ expect(isTemporaryUploadKeyValid(key)).toBe(false)
+ })
+
+ it('should handle very short expiry durations', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ // Create a key that expires in 30 seconds (less than the 60 second buffer)
+ const key = createTemporaryUploadKey('test-api-key', 30)
+
+ // Should be invalid immediately because it will expire in less than 60 seconds
+ expect(isTemporaryUploadKeyValid(key)).toBe(false)
+ })
+
+ it('should handle expiry duration of exactly 60 seconds', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ // Create a key that expires in exactly 60 seconds
+ const key = createTemporaryUploadKey('test-api-key', 60)
+
+ // Should be invalid because it has exactly 60 seconds remaining (not more than 60)
+ expect(isTemporaryUploadKeyValid(key)).toBe(false)
+ })
+
+ it('should handle expiry duration of 61 seconds', () => {
+ const now = Date.now()
+ vi.setSystemTime(now)
+
+ // Create a key that expires in 61 seconds
+ const key = createTemporaryUploadKey('test-api-key', 61)
+
+ // Should be valid because it has 61 seconds remaining (more than 60)
+ expect(isTemporaryUploadKeyValid(key)).toBe(true)
+
+ // Advance by 1 second
+ vi.advanceTimersByTime(1000)
+
+ // Should now be invalid because it has exactly 60 seconds remaining
+ expect(isTemporaryUploadKeyValid(key)).toBe(false)
+ })
+})
diff --git a/apps/studio/data/api-keys/temp-api-keys-utils.ts b/apps/studio/data/api-keys/temp-api-keys-utils.ts
new file mode 100644
index 00000000000..f713d9a83a4
--- /dev/null
+++ b/apps/studio/data/api-keys/temp-api-keys-utils.ts
@@ -0,0 +1,24 @@
+export interface TemporaryUploadKey {
+ apiKey: string
+ expiryTime: number
+}
+
+export function createTemporaryUploadKey(
+ apiKey: string,
+ expiryInSeconds: number
+): TemporaryUploadKey {
+ return {
+ apiKey,
+ expiryTime: Date.now() + expiryInSeconds * 1000,
+ }
+}
+
+export function isTemporaryUploadKeyValid(
+ key: TemporaryUploadKey | null | undefined
+): key is TemporaryUploadKey {
+ if (!key) return false
+
+ const now = Date.now()
+ const timeRemaining = key.expiryTime - now
+ return timeRemaining > 60000 // More than 60 seconds remaining
+}
diff --git a/apps/studio/state/storage-explorer.tsx b/apps/studio/state/storage-explorer.tsx
index 751080cd58c..08004705897 100644
--- a/apps/studio/state/storage-explorer.tsx
+++ b/apps/studio/state/storage-explorer.tsx
@@ -37,6 +37,11 @@ import {
import { convertFromBytes } from 'components/interfaces/Storage/StorageSettings/StorageSettings.utils'
import { InlineLink } from 'components/ui/InlineLink'
import { getTemporaryAPIKey } from 'data/api-keys/temp-api-keys-query'
+import {
+ createTemporaryUploadKey,
+ isTemporaryUploadKeyValid,
+ type TemporaryUploadKey,
+} from 'data/api-keys/temp-api-keys-utils'
import { configKeys } from 'data/config/keys'
import { useProjectSettingsV2Query } from 'data/config/project-settings-v2-query'
import { ProjectStorageConfigResponse } from 'data/config/project-storage-config-query'
@@ -101,6 +106,9 @@ function createStorageExplorerState({
resumableUploadUrl,
uploadProgresses: [] as UploadProgress[],
+ // Temporary API key management for batch uploads
+ temporaryUploadKey: undefined as TemporaryUploadKey | undefined,
+
// abortController,
abortApiCalls: () => {
if (abortController) {
@@ -115,6 +123,29 @@ function createStorageExplorerState({
state.abortUploadCallbacks[toastId] = []
},
+ // Get or refresh the temporary upload key
+ getOrRefreshTemporaryUploadKey: async () => {
+ if (isTemporaryUploadKeyValid(state.temporaryUploadKey)) {
+ return state.temporaryUploadKey.apiKey
+ }
+
+ // Generate new key with 10 minutes expiry
+ const expiryInSeconds = 600
+ const data = await getTemporaryAPIKey({
+ projectRef: state.projectRef,
+ expiry: expiryInSeconds,
+ })
+
+ state.temporaryUploadKey = createTemporaryUploadKey(data.api_key, expiryInSeconds)
+
+ return data.api_key
+ },
+
+ // Clear the temporary upload key
+ clearTemporaryUploadKey: () => {
+ state.temporaryUploadKey = undefined
+ },
+
columns: [] as StorageColumn[],
popColumn: () => {
state.abortApiCalls()
@@ -1098,6 +1129,15 @@ function createStorageExplorerState({
.map((folder) => folder.name)
.join('/')
+ // Generate temporary API key for the batch upload
+ try {
+ await state.getOrRefreshTemporaryUploadKey()
+ } catch (error) {
+ console.error('Failed to get temporary API key:', error)
+ toast.error('Failed to initialize upload session. Please try again.')
+ return
+ }
+
const toastId = state.onUploadProgress()
// Upload files in batches
@@ -1196,10 +1236,12 @@ function createStorageExplorerState({
chunkSize,
onBeforeRequest: async (req) => {
try {
- const data = await getTemporaryAPIKey({ projectRef: state.projectRef })
- req.setHeader('apikey', data.api_key)
+ // Use the shared temporary key for batch uploads
+ // This checks if the key is still valid and refreshes if needed
+ const apiKey = await state.getOrRefreshTemporaryUploadKey()
+ req.setHeader('apikey', apiKey)
if (!IS_PLATFORM) {
- req.setHeader('Authorization', `Bearer ${data.api_key}`)
+ req.setHeader('Authorization', `Bearer ${apiKey}`)
}
} catch (error) {
throw error
@@ -1356,6 +1398,9 @@ function createStorageExplorerState({
closeButton: true,
duration: SONNER_DEFAULT_DURATION,
})
+ } finally {
+ // Clear the temporary API key after batch upload completes
+ state.clearTemporaryUploadKey()
}
const t2 = new Date()