UNPKG

@llumiverse/drivers

Version:

LLM driver implementations. Currently supported are: openai, huggingface, bedrock, replicate.

107 lines 3.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.doesBucketExist = doesBucketExist; exports.createBucket = createBucket; exports.tryCreateBucket = tryCreateBucket; exports.uploadFile = uploadFile; exports.forceUploadFile = forceUploadFile; exports.parseS3UrlToUri = parseS3UrlToUri; const client_s3_1 = require("@aws-sdk/client-s3"); const lib_storage_1 = require("@aws-sdk/lib-storage"); async function doesBucketExist(s3, bucketName) { try { await s3.send(new client_s3_1.HeadBucketCommand({ Bucket: bucketName })); return true; } catch (err) { if (err.name === 'NotFound') { return false; } throw err; } } function createBucket(s3, bucketName) { return s3.send(new client_s3_1.CreateBucketCommand({ Bucket: bucketName })); } async function tryCreateBucket(s3, bucketName) { const exists = await doesBucketExist(s3, bucketName); if (!exists) { return createBucket(s3, bucketName); } } async function uploadFile(s3, source, bucketName, file, onProgress) { const upload = new lib_storage_1.Upload({ client: s3, params: { Bucket: bucketName, Key: file, Body: source, } }); onProgress && upload.on("httpUploadProgress", onProgress); const result = await upload.done(); return result; } /** * Create the bucket if not already exists and then upload the file. * @param s3 * @param source * @param bucketName * @param file * @param onProgress * @returns */ async function forceUploadFile(s3, source, bucketName, file, onProgress) { // make sure the bucket exists await tryCreateBucket(s3, bucketName); return uploadFile(s3, source, bucketName, file, onProgress); } /** * Parse an S3 HTTPS URL into an S3 URI format * s3Url - The S3 HTTPS URL (e.g., https://bucket.s3.region.amazonaws.com/key) * returns The S3 URI (e.g., s3://bucket/key) */ function parseS3UrlToUri(s3Url) { try { const url = new URL(s3Url); // Extract the hostname which contains the bucket and S3 endpoint const hostname = url.hostname; // Parse the hostname to extract the bucket name let bucketName; if (hostname.endsWith('.amazonaws.com')) { if (hostname.includes('.s3.')) { // Format: bucket-name.s3.region.amazonaws.com bucketName = hostname.split('.s3.')[0]; } else if (hostname.startsWith('s3.')) { // Format: s3.region.amazonaws.com/bucket-name // In this case, the bucket is actually in the first segment of the pathname bucketName = url.pathname.split('/')[1]; // Adjust the pathname to remove the bucket name const pathParts = url.pathname.split('/').slice(2); url.pathname = '/' + pathParts.join('/'); } else { throw new Error('Unable to determine bucket name from URL'); } } else { throw new Error('Unable to determine bucket name from URL'); } // The key is the pathname without the leading slash // If we had the bucket name in the path, it's already been removed above let key = url.pathname; if (key.startsWith('/')) { key = key.substring(1); } // Construct the S3 URI return `s3://${bucketName}/${key}`; } catch (error) { console.error('Error parsing S3 URL:', error); throw error; } } //# sourceMappingURL=s3.js.map