now files do presigned url setup
This commit is contained in:
@@ -11,6 +11,7 @@ import {
|
||||
import type {
|
||||
FileMetadata,
|
||||
FileUploadConfig,
|
||||
PresignedDownloadResult,
|
||||
PresignedUrlResult,
|
||||
UploadOptions,
|
||||
UploadResult,
|
||||
@@ -266,6 +267,46 @@ export class R2StorageClient {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate presigned URL for private object download/view
|
||||
*/
|
||||
async generatePresignedDownloadUrl(
|
||||
objectKey: string,
|
||||
expiresIn: number = 3600,
|
||||
): Promise<Result<PresignedDownloadResult>> {
|
||||
try {
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: this.config.bucketName,
|
||||
Key: objectKey,
|
||||
});
|
||||
|
||||
const downloadUrl = await getSignedUrl(this.s3Client, command, {
|
||||
expiresIn,
|
||||
});
|
||||
|
||||
logger.info(`Generated presigned download URL for ${objectKey}`);
|
||||
return {
|
||||
data: {
|
||||
downloadUrl,
|
||||
expiresIn,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error("Failed to generate presigned download URL:", error);
|
||||
return {
|
||||
error: getError(
|
||||
{
|
||||
code: ERROR_CODES.STORAGE_ERROR,
|
||||
message: "Failed to generate presigned download URL",
|
||||
description: "Could not create download URL",
|
||||
detail: "S3 presigned URL generation failed",
|
||||
},
|
||||
error,
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file from R2
|
||||
*/
|
||||
|
||||
@@ -60,6 +60,14 @@ export const presignedUrlResultSchema = v.object({
|
||||
});
|
||||
export type PresignedUrlResult = v.InferOutput<typeof presignedUrlResultSchema>;
|
||||
|
||||
export const presignedDownloadResultSchema = v.object({
|
||||
downloadUrl: v.string(),
|
||||
expiresIn: v.pipe(v.number(), v.integer()),
|
||||
});
|
||||
export type PresignedDownloadResult = v.InferOutput<
|
||||
typeof presignedDownloadResultSchema
|
||||
>;
|
||||
|
||||
// File Validation Result Schema
|
||||
export const fileValidationResultSchema = v.object({
|
||||
isValid: v.boolean(),
|
||||
|
||||
Reference in New Issue
Block a user