Skip to content
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
27 changes: 18 additions & 9 deletions packages/storage/src/lib/object/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UploadPartCommand,
} from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { toError } from '@shared/utils';
import { config } from '../config';
import { createTigrisClient } from '../tigris-client';
import type { TigrisStorageConfig, TigrisStorageResponse } from '../types';
Expand Down Expand Up @@ -32,17 +33,25 @@ export async function initMultipartUpload(
Key: path,
});

const { UploadId } = await tigrisClient.send(createCommand);
try {
const { UploadId } = await tigrisClient.send(createCommand);

if (!UploadId) {
return { error: new Error('Unable to initialize multipart upload') };
}
if (!UploadId) {
return { error: new Error('Unable to initialize multipart upload') };
}

return {
data: {
uploadId: UploadId,
},
};
return {
data: {
uploadId: UploadId,
},
};
} catch (error) {
return {
error: new Error(
`Unable to initialize multipart upload: ${toError(error).message}`
),
};
}
}

export type GetPartsPresignedUrlsOptions = {
Expand Down
12 changes: 3 additions & 9 deletions packages/storage/src/lib/object/put.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { toError } from '@shared/utils';
import { config } from '../config';
import { createTigrisClient } from '../tigris-client';
import type { TigrisStorageConfig, TigrisStorageResponse } from '../types';
Expand Down Expand Up @@ -115,15 +116,8 @@ export async function put(

try {
await upload.done();
} catch (error: unknown) {
const { message } = error as {
message: string;
};
return {
error: message
? new Error(message)
: new Error(`Unexpected error while uploading to Tigris Storage`),
};
} catch (error) {
return { error: toError(error) };
}

let signedUrl: string;
Expand Down
77 changes: 41 additions & 36 deletions packages/storage/src/lib/upload/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { getPresignedUrl } from '../object/presigned-url';
import { TigrisStorageConfig, TigrisStorageResponse } from '../types';
import { UploadAction } from './shared';
import { toError } from '@shared/utils';

export interface ClientUploadRequest {
action: UploadAction;
Expand All @@ -22,43 +23,47 @@ export async function handleClientUpload(
): Promise<TigrisStorageResponse<unknown, Error>> {
const { action, name, contentType, uploadId, parts, partIds } = request;

switch (action) {
case UploadAction.SinglepartInit:
return await getPresignedUrl(name, {
contentType,
operation: 'put',
expiresIn: 3600, // 1 hour
config,
});
case UploadAction.MultipartInit:
return await initMultipartUpload(name, {
config,
});
case UploadAction.MultipartGetParts:
if (!uploadId || !parts) {
try {
switch (action) {
case UploadAction.SinglepartInit:
return await getPresignedUrl(name, {
contentType,
operation: 'put',
expiresIn: 3600, // 1 hour
config,
});
case UploadAction.MultipartInit:
return await initMultipartUpload(name, {
config,
});
case UploadAction.MultipartGetParts:
if (!uploadId || !parts) {
return {
error: new Error(
'uploadId and parts are required for multipart-parts'
),
};
}
return await getPartsPresignedUrls(name, parts, uploadId, {
config,
});
case UploadAction.MultipartComplete:
if (!uploadId || !partIds) {
return {
error: new Error(
'uploadId and partIds are required for multipart-complete'
),
};
}
return await completeMultipartUpload(name, uploadId, partIds, {
config,
});
default:
return {
error: new Error(
'uploadId and parts are required for multipart-parts'
),
error: new Error(`Invalid action: ${action}`),
};
}
return await getPartsPresignedUrls(name, parts, uploadId, {
config,
});
case UploadAction.MultipartComplete:
if (!uploadId || !partIds) {
return {
error: new Error(
'uploadId and partIds are required for multipart-complete'
),
};
}
return await completeMultipartUpload(name, uploadId, partIds, {
config,
});
default:
return {
error: new Error(`Invalid action: ${action}`),
};
}
} catch (error) {
return { error: toError(error) };
}
}
2 changes: 1 addition & 1 deletion shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { isNode, loadEnv, missingConfigError } from './config';
export type { TigrisResponse } from './types';
export { executeWithConcurrency, handleError } from './utils';
export { executeWithConcurrency, handleError, toError } from './utils';
export { TigrisHeaders } from './headers';
export {
createTigrisHttpClient,
Expand Down
6 changes: 6 additions & 0 deletions shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export async function executeWithConcurrency<T>(
return results;
}

export function toError(value: unknown): Error {
if (value instanceof Error) return value;
if (typeof value === 'string') return new Error(value);
return new Error('An unexpected error occurred');
}

export const handleError = (error: Error) => {
let errorMessage: string | undefined;

Expand Down
Loading