Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow overwriting objects when copying #530

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
rateLimiterRedisCommandTimeout: number
uploadSignedUrlExpirationTime: number
tusUrlExpiryMs: number
tusMaxConcurrentUploads: number
tusPath: string
tusPartSize: number
tusUseFileVersionSeparator: boolean
Expand Down Expand Up @@ -231,6 +232,10 @@
getOptionalConfigFromEnv('TUS_URL_EXPIRY_MS') || (1000 * 60 * 60).toString(),
10
),
tusMaxConcurrentUploads: parseInt(
getOptionalConfigFromEnv('TUS_MAX_CONCURRENT_UPLOADS') || '500',
10
),
tusUseFileVersionSeparator:
getOptionalConfigFromEnv('TUS_USE_FILE_VERSION_SEPARATOR') === 'true',

Expand Down Expand Up @@ -420,7 +425,7 @@
try {
const parsed = JSON.parse(jwtJWKS)
config.jwtJWKS = parsed
} catch (e: any) {

Check warning on line 428 in src/config.ts

View workflow job for this annotation

GitHub Actions / Test / OS ubuntu-20.04 / Node 20

Unexpected any. Specify a different type
throw new Error('Unable to parse JWT_JWKS value to JSON')
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/routes/tus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function createTusStore() {
partSize: tusPartSize * 1024 * 1024, // Each uploaded part will have ${tusPartSize}MB,
expirationPeriodInMilliseconds: tusUrlExpiryMs,
cache: new AlsMemoryKV(),
maxConcurrentPartUploads: 100,
maxConcurrentPartUploads: 500,
s3ClientConfig: {
requestHandler: new NodeHttpHandler({
...agent,
Expand Down
50 changes: 40 additions & 10 deletions src/storage/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface CopyObjectParams {
destinationKey: string
owner?: string
copyMetadata?: boolean
upsert?: boolean
conditions?: {
ifMatch?: string
ifNoneMatch?: string
Expand Down Expand Up @@ -280,6 +281,7 @@ export class ObjectStorage {
owner,
conditions,
copyMetadata,
upsert,
}: CopyObjectParams) {
mustBeValidKey(destinationKey)

Expand Down Expand Up @@ -310,7 +312,7 @@ export class ObjectStorage {
bucketId: destinationBucket,
objectName: destinationKey,
owner,
isUpsert: false,
isUpsert: upsert,
})

try {
Expand All @@ -325,14 +327,42 @@ export class ObjectStorage {

const metadata = await this.backend.headObject(storageS3Bucket, s3DestinationKey, newVersion)

const destObject = await this.db.createObject({
...originObject,
bucket_id: destinationBucket,
name: destinationKey,
owner,
metadata,
user_metadata: copyMetadata ? originObject.user_metadata : undefined,
version: newVersion,
const destinationObject = await this.db.asSuperUser().withTransaction(async (db) => {
await db.waitObjectLock(destinationBucket, destinationKey, undefined, {
timeout: 3000,
})

const existingDestObject = await db.findObject(
this.bucketId,
destinationKey,
'id,name,metadata,version,bucket_id',
{
dontErrorOnEmpty: true,
forUpdate: true,
}
)

const destinationObject = await db.upsertObject({
...originObject,
bucket_id: destinationBucket,
name: destinationKey,
owner,
metadata,
user_metadata: copyMetadata ? originObject.user_metadata : undefined,
version: newVersion,
})

if (existingDestObject) {
await ObjectAdminDelete.send({
name: existingDestObject.name,
bucketId: existingDestObject.bucket_id,
tenant: this.db.tenant(),
version: existingDestObject.version,
reqId: this.db.reqId,
})
}

return destinationObject
})

await ObjectCreatedCopyEvent.sendWebhook({
Expand All @@ -345,7 +375,7 @@ export class ObjectStorage {
})

return {
destObject,
destObject: destinationObject,
httpStatusCode: copyResult.httpStatusCode,
eTag: copyResult.eTag,
lastModified: copyResult.lastModified,
Expand Down
1 change: 1 addition & 0 deletions src/storage/protocols/s3/s3-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ export class S3ProtocolHandler {
destinationBucket: Bucket,
destinationKey: Key,
owner: this.owner,
upsert: true,
conditions: {
ifMatch: command.CopySourceIfMatch,
ifNoneMatch: command.CopySourceIfNoneMatch,
Expand Down
29 changes: 16 additions & 13 deletions src/storage/protocols/tus/postgres-locker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ export class PgLock implements Lock {
this.db
.withTransaction(async (db) => {
const abortController = new AbortController()
const acquired = await Promise.race([
this.waitTimeout(15000, abortController.signal),
this.acquireLock(db, this.id, abortController.signal),
])

abortController.abort()

if (!acquired) {
throw ERRORS.LockTimeout()
try {
const acquired = await Promise.race([
this.waitTimeout(5000, abortController.signal),
this.acquireLock(db, this.id, abortController.signal),
])

if (!acquired) {
throw ERRORS.LockTimeout()
}

await new Promise<void>((innerResolve) => {
this.tnxResolver = innerResolve
resolve()
})
} finally {
abortController.abort()
}

await new Promise<void>((innerResolve) => {
this.tnxResolver = innerResolve
resolve()
})
})
.catch(reject)
})
Expand Down
Loading