import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { S3Object } from 'windmill-client';
// Define the S3 resource type
type S3 = {
port: number,
bucket: string,
region: string,
useSSL: boolean,
endPoint: string,
accessKey: string,
pathStyle: boolean,
secretKey: string
}
/*
* * Generates a presigned URL for accessing an S3 object
* @param {S3Object} file - The uploaded file
* @param {S3} s3Resource - The S3 resource configuration
* @param {number} expiretime - The time that the link is valid. Maximum is 604800 seconds (7 days)
* @returns Promise<string> - The presigned URL
*/
export async function main(file: S3Object, s3Resource: S3, expiretime: number) {
// Use the S3 resource to get credentials and configuration
const bucketName = s3Resource.bucket; // Use the bucket from the S3 resource
const region = s3Resource.region; // Use the region from the S3 resource
// Initialize S3 client with credentials from the S3 resource
const s3Client = new S3Client({
region,
credentials: {
accessKeyId: s3Resource.accessKey, // Use access key from the S3 resource
secretAccessKey: s3Resource.secretKey, // Use secret key from the S3 resource
},
});
const getCommand = new GetObjectCommand({
Bucket: bucketName,
Key: file.s3, // Use the key from the S3Object parameter
});
const presignedUrl = await getSignedUrl(s3Client, getCommand, { expiresIn: expiretime });
return presignedUrl;
}
Submitted by jansteinark898 23 days ago