@settlemint/sdk-minio 2.3.2-prfd3a56ad → 2.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -16
- package/dist/minio.cjs +505 -258
- package/dist/minio.cjs.map +1 -1
- package/dist/minio.d.cts +72 -42
- package/dist/minio.d.ts +72 -42
- package/dist/minio.js +520 -0
- package/dist/minio.js.map +1 -0
- package/package.json +6 -6
- package/dist/minio.mjs +0 -271
- package/dist/minio.mjs.map +0 -1
package/dist/minio.cjs
CHANGED
|
@@ -1,302 +1,549 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
8
|
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
17
|
};
|
|
18
|
-
var
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
19
22
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
createServerMinioClient: () => createServerMinioClient,
|
|
26
|
-
deleteFile: () => deleteFile,
|
|
27
|
-
getFileById: () => getFileById,
|
|
28
|
-
getFilesList: () => getFilesList,
|
|
29
|
-
uploadFile: () => uploadFile
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(minio_exports);
|
|
32
|
-
var import_runtime3 = require("@settlemint/sdk-utils/runtime");
|
|
33
|
-
var import_validation3 = require("@settlemint/sdk-utils/validation");
|
|
34
|
-
var import_minio = require("minio");
|
|
23
|
+
//#endregion
|
|
24
|
+
const __settlemint_sdk_utils_runtime = __toESM(require("@settlemint/sdk-utils/runtime"));
|
|
25
|
+
const __settlemint_sdk_utils_validation = __toESM(require("@settlemint/sdk-utils/validation"));
|
|
26
|
+
const minio = __toESM(require("minio"));
|
|
27
|
+
const zod_v4 = __toESM(require("zod/v4"));
|
|
35
28
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
/** The MinIO secret key used to authenticate with the MinIO server */
|
|
45
|
-
secretKey: import_v4.z.string()
|
|
29
|
+
//#region src/helpers/client-options.schema.ts
|
|
30
|
+
/**
|
|
31
|
+
* Schema for validating server client options for the MinIO client.
|
|
32
|
+
*/
|
|
33
|
+
const ServerClientOptionsSchema = zod_v4.z.object({
|
|
34
|
+
instance: __settlemint_sdk_utils_validation.UrlSchema,
|
|
35
|
+
accessKey: zod_v4.z.string().min(1, "Access key cannot be empty"),
|
|
36
|
+
secretKey: zod_v4.z.string().min(1, "Secret key cannot be empty")
|
|
46
37
|
});
|
|
47
38
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/helpers/schema.ts
|
|
41
|
+
/**
|
|
42
|
+
* Schema for file metadata stored in MinIO.
|
|
43
|
+
* Defines the structure and validation rules for file information.
|
|
44
|
+
*/
|
|
45
|
+
const FileMetadataSchema = zod_v4.z.object({
|
|
46
|
+
id: zod_v4.z.string(),
|
|
47
|
+
name: zod_v4.z.string(),
|
|
48
|
+
contentType: zod_v4.z.string(),
|
|
49
|
+
size: zod_v4.z.number(),
|
|
50
|
+
uploadedAt: zod_v4.z.string().datetime(),
|
|
51
|
+
etag: zod_v4.z.string(),
|
|
52
|
+
url: zod_v4.z.string().url().optional()
|
|
58
53
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
var import_validation2 = require("@settlemint/sdk-utils/validation");
|
|
54
|
+
/**
|
|
55
|
+
* Default bucket name to use for file storage when none is specified.
|
|
56
|
+
*/
|
|
57
|
+
const DEFAULT_BUCKET = "uploads";
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/helpers/executor.ts
|
|
61
|
+
/**
|
|
62
|
+
* Executes a MinIO operation using the provided client
|
|
63
|
+
*
|
|
64
|
+
* @param client - MinIO client to use
|
|
65
|
+
* @param operation - The operation to execute
|
|
66
|
+
* @returns The result of the operation execution
|
|
67
|
+
* @throws Will throw an error if the operation fails
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* import { createServerMinioClient, createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
71
|
+
* const { client } = createServerMinioClient({
|
|
72
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
73
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
74
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
75
|
+
* });
|
|
76
|
+
* const listOperation = createListObjectsOperation("my-bucket", "prefix/");
|
|
77
|
+
* const result = await executeMinioOperation(client, listOperation);
|
|
78
|
+
*/
|
|
67
79
|
async function executeMinioOperation(client, operation) {
|
|
68
|
-
|
|
69
|
-
|
|
80
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
81
|
+
return operation.execute(client);
|
|
70
82
|
}
|
|
71
83
|
|
|
72
|
-
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/helpers/operations.ts
|
|
86
|
+
/**
|
|
87
|
+
* Creates an operation to list objects in a bucket
|
|
88
|
+
*
|
|
89
|
+
* @param bucket - The bucket name to list objects from
|
|
90
|
+
* @param prefix - Optional prefix to filter objects (like a folder path)
|
|
91
|
+
* @returns A MinioOperation that lists objects when executed
|
|
92
|
+
* @throws Will throw an error if the operation fails
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* import { createListObjectsOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
96
|
+
*
|
|
97
|
+
* const listOperation = createListObjectsOperation("my-bucket", "folder/");
|
|
98
|
+
* const objects = await executeMinioOperation(client, listOperation);
|
|
99
|
+
*/
|
|
73
100
|
function createListObjectsOperation(bucket, prefix = "") {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
};
|
|
101
|
+
return { execute: async (client) => {
|
|
102
|
+
const objectsStream = client.listObjects(bucket, prefix, true);
|
|
103
|
+
const objects = [];
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
objectsStream.on("data", (obj) => {
|
|
106
|
+
if (obj.name && typeof obj.size === "number" && obj.etag && obj.lastModified) {
|
|
107
|
+
objects.push({
|
|
108
|
+
name: obj.name,
|
|
109
|
+
prefix: obj.prefix,
|
|
110
|
+
size: obj.size,
|
|
111
|
+
etag: obj.etag,
|
|
112
|
+
lastModified: obj.lastModified
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
objectsStream.on("error", (err) => {
|
|
117
|
+
reject(err);
|
|
118
|
+
});
|
|
119
|
+
objectsStream.on("end", () => {
|
|
120
|
+
resolve(objects);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
} };
|
|
99
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Creates an operation to get an object's metadata
|
|
127
|
+
*
|
|
128
|
+
* @param bucket - The bucket name containing the object
|
|
129
|
+
* @param objectName - The object name/path
|
|
130
|
+
* @returns A MinioOperation that gets object stats when executed
|
|
131
|
+
* @throws Will throw an error if the operation fails
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* import { createStatObjectOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
135
|
+
*
|
|
136
|
+
* const statOperation = createStatObjectOperation("my-bucket", "folder/file.txt");
|
|
137
|
+
* const stats = await executeMinioOperation(client, statOperation);
|
|
138
|
+
*/
|
|
100
139
|
function createStatObjectOperation(bucket, objectName) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
};
|
|
140
|
+
return { execute: async (client) => {
|
|
141
|
+
return client.statObject(bucket, objectName);
|
|
142
|
+
} };
|
|
106
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Creates an operation to upload a buffer to MinIO
|
|
146
|
+
*
|
|
147
|
+
* @param bucket - The bucket name to upload to
|
|
148
|
+
* @param objectName - The object name/path to create
|
|
149
|
+
* @param buffer - The buffer containing the file data
|
|
150
|
+
* @param metadata - Optional metadata to attach to the object
|
|
151
|
+
* @returns A MinioOperation that uploads the buffer when executed
|
|
152
|
+
* @throws Will throw an error if the operation fails
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* import { createUploadOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
156
|
+
*
|
|
157
|
+
* const buffer = Buffer.from("file content");
|
|
158
|
+
* const uploadOperation = createUploadOperation("my-bucket", "folder/file.txt", buffer, { "content-type": "text/plain" });
|
|
159
|
+
* const result = await executeMinioOperation(client, uploadOperation);
|
|
160
|
+
*/
|
|
161
|
+
function createUploadOperation(bucket, objectName, buffer, metadata) {
|
|
162
|
+
return { execute: async (client) => {
|
|
163
|
+
return client.putObject(bucket, objectName, buffer, undefined, metadata);
|
|
164
|
+
} };
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Creates an operation to delete an object from MinIO
|
|
168
|
+
*
|
|
169
|
+
* @param bucket - The bucket name containing the object
|
|
170
|
+
* @param objectName - The object name/path to delete
|
|
171
|
+
* @returns A MinioOperation that deletes the object when executed
|
|
172
|
+
* @throws Will throw an error if the operation fails
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* import { createDeleteOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
176
|
+
*
|
|
177
|
+
* const deleteOperation = createDeleteOperation("my-bucket", "folder/file.txt");
|
|
178
|
+
* await executeMinioOperation(client, deleteOperation);
|
|
179
|
+
*/
|
|
107
180
|
function createDeleteOperation(bucket, objectName) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
};
|
|
181
|
+
return { execute: async (client) => {
|
|
182
|
+
return client.removeObject(bucket, objectName);
|
|
183
|
+
} };
|
|
113
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Creates an operation to generate a presigned URL for an object
|
|
187
|
+
*
|
|
188
|
+
* @param bucket - The bucket name containing the object
|
|
189
|
+
* @param objectName - The object name/path
|
|
190
|
+
* @param expirySeconds - How long the URL should be valid for in seconds
|
|
191
|
+
* @returns A MinioOperation that creates a presigned URL when executed
|
|
192
|
+
* @throws Will throw an error if the operation fails
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* import { createPresignedUrlOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
196
|
+
*
|
|
197
|
+
* const urlOperation = createPresignedUrlOperation("my-bucket", "folder/file.txt", 3600);
|
|
198
|
+
* const url = await executeMinioOperation(client, urlOperation);
|
|
199
|
+
*/
|
|
114
200
|
function createPresignedUrlOperation(bucket, objectName, expirySeconds) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
};
|
|
201
|
+
return { execute: async (client) => {
|
|
202
|
+
return client.presignedGetObject(bucket, objectName, expirySeconds);
|
|
203
|
+
} };
|
|
120
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Creates an operation to generate a presigned PUT URL for direct uploads
|
|
207
|
+
*
|
|
208
|
+
* @param bucket - The bucket name to upload to
|
|
209
|
+
* @param objectName - The object name/path to create
|
|
210
|
+
* @param expirySeconds - How long the URL should be valid for in seconds
|
|
211
|
+
* @returns A MinioOperation that creates a presigned PUT URL when executed
|
|
212
|
+
* @throws Will throw an error if the operation fails
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* import { createPresignedPutOperation, executeMinioOperation } from "@settlemint/sdk-minio";
|
|
216
|
+
*
|
|
217
|
+
* const putUrlOperation = createPresignedPutOperation("my-bucket", "folder/file.txt", 3600);
|
|
218
|
+
* const url = await executeMinioOperation(client, putUrlOperation);
|
|
219
|
+
*/
|
|
121
220
|
function createPresignedPutOperation(bucket, objectName, expirySeconds) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
};
|
|
221
|
+
return { execute: async (client) => {
|
|
222
|
+
return client.presignedPutObject(bucket, objectName, expirySeconds);
|
|
223
|
+
} };
|
|
127
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Creates a simplified upload function bound to a specific client
|
|
227
|
+
*
|
|
228
|
+
* @param client - The MinIO client to use for uploads
|
|
229
|
+
* @returns A function that uploads buffers to MinIO
|
|
230
|
+
* @throws Will throw an error if the operation fails
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* import { createSimpleUploadOperation, getMinioClient } from "@settlemint/sdk-minio";
|
|
234
|
+
*
|
|
235
|
+
* const client = await getMinioClient();
|
|
236
|
+
* const uploadFn = createSimpleUploadOperation(client);
|
|
237
|
+
* const result = await uploadFn(buffer, "my-bucket", "folder/file.txt", { "content-type": "text/plain" });
|
|
238
|
+
*/
|
|
128
239
|
function createSimpleUploadOperation(client) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
240
|
+
return async (buffer, bucket, objectName, metadata) => {
|
|
241
|
+
return client.putObject(bucket, objectName, buffer, undefined, metadata);
|
|
242
|
+
};
|
|
132
243
|
}
|
|
133
244
|
|
|
134
|
-
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/helpers/functions.ts
|
|
247
|
+
/**
|
|
248
|
+
* Helper function to normalize paths and prevent double slashes
|
|
249
|
+
*
|
|
250
|
+
* @param path - The path to normalize
|
|
251
|
+
* @param fileName - The filename to append
|
|
252
|
+
* @returns The normalized path with filename
|
|
253
|
+
* @throws Will throw an error if the path is too long (max 1000 characters)
|
|
254
|
+
*/
|
|
135
255
|
function normalizePath(path, fileName) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
256
|
+
if (path.length > 1e3) {
|
|
257
|
+
throw new Error("Path is too long");
|
|
258
|
+
}
|
|
259
|
+
const cleanPath = path.replace(/\/+$/, "");
|
|
260
|
+
if (!cleanPath) {
|
|
261
|
+
return fileName;
|
|
262
|
+
}
|
|
263
|
+
return `${cleanPath}/${fileName}`;
|
|
144
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Gets a list of files with optional prefix filter
|
|
267
|
+
*
|
|
268
|
+
* @param client - The MinIO client to use
|
|
269
|
+
* @param prefix - Optional prefix to filter files (like a folder path)
|
|
270
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
271
|
+
* @returns Array of file metadata objects
|
|
272
|
+
* @throws Will throw an error if the operation fails or client initialization fails
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* import { createServerMinioClient, getFilesList } from "@settlemint/sdk-minio";
|
|
276
|
+
*
|
|
277
|
+
* const { client } = createServerMinioClient({
|
|
278
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
279
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
280
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
281
|
+
* });
|
|
282
|
+
*
|
|
283
|
+
* const files = await getFilesList(client, "documents/");
|
|
284
|
+
*/
|
|
145
285
|
async function getFilesList(client, prefix = "", bucket = DEFAULT_BUCKET) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
286
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
287
|
+
console.log(`Listing files with prefix: "${prefix}" in bucket: "${bucket}"`);
|
|
288
|
+
try {
|
|
289
|
+
const listOperation = createListObjectsOperation(bucket, prefix);
|
|
290
|
+
const objects = await executeMinioOperation(client, listOperation);
|
|
291
|
+
console.log(`Found ${objects.length} files in MinIO`);
|
|
292
|
+
const fileObjects = await Promise.allSettled(objects.map(async (obj) => {
|
|
293
|
+
try {
|
|
294
|
+
const presignedUrlOperation = createPresignedUrlOperation(bucket, obj.name, 3600);
|
|
295
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
296
|
+
return {
|
|
297
|
+
id: obj.name,
|
|
298
|
+
name: obj.name.split("/").pop() || obj.name,
|
|
299
|
+
contentType: "application/octet-stream",
|
|
300
|
+
size: obj.size,
|
|
301
|
+
uploadedAt: obj.lastModified.toISOString(),
|
|
302
|
+
etag: obj.etag,
|
|
303
|
+
url
|
|
304
|
+
};
|
|
305
|
+
} catch (error) {
|
|
306
|
+
console.warn(`Failed to generate presigned URL for ${obj.name}:`, error);
|
|
307
|
+
return {
|
|
308
|
+
id: obj.name,
|
|
309
|
+
name: obj.name.split("/").pop() || obj.name,
|
|
310
|
+
contentType: "application/octet-stream",
|
|
311
|
+
size: obj.size,
|
|
312
|
+
uploadedAt: obj.lastModified.toISOString(),
|
|
313
|
+
etag: obj.etag
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
})).then((results) => results.filter((result) => result.status === "fulfilled").map((result) => result.value));
|
|
317
|
+
return (0, __settlemint_sdk_utils_validation.validate)(FileMetadataSchema.array(), fileObjects);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.error("Failed to list files:", error);
|
|
320
|
+
throw new Error(`Failed to list files: ${error instanceof Error ? error.message : String(error)}`);
|
|
321
|
+
}
|
|
178
322
|
}
|
|
323
|
+
/**
|
|
324
|
+
* Gets a single file by its object name
|
|
325
|
+
*
|
|
326
|
+
* @param client - The MinIO client to use
|
|
327
|
+
* @param fileId - The file identifier/path
|
|
328
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
329
|
+
* @returns File metadata with presigned URL
|
|
330
|
+
* @throws Will throw an error if the file doesn't exist or client initialization fails
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* import { createServerMinioClient, getFileByObjectName } from "@settlemint/sdk-minio";
|
|
334
|
+
*
|
|
335
|
+
* const { client } = createServerMinioClient({
|
|
336
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
337
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
338
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
339
|
+
* });
|
|
340
|
+
*
|
|
341
|
+
* const file = await getFileByObjectName(client, "documents/report.pdf");
|
|
342
|
+
*/
|
|
179
343
|
async function getFileById(client, fileId, bucket = DEFAULT_BUCKET) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return (0, import_validation2.validate)(FileMetadataSchema, fileMetadata);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
console.error(`Failed to get file ${fileId}:`, error);
|
|
213
|
-
throw new Error(`Failed to get file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
214
|
-
}
|
|
344
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
345
|
+
console.log(`Getting file details for: ${fileId} in bucket: ${bucket}`);
|
|
346
|
+
try {
|
|
347
|
+
const statOperation = createStatObjectOperation(bucket, fileId);
|
|
348
|
+
const statResult = await executeMinioOperation(client, statOperation);
|
|
349
|
+
const presignedUrlOperation = createPresignedUrlOperation(bucket, fileId, 3600);
|
|
350
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
351
|
+
let size = 0;
|
|
352
|
+
if (statResult.metaData["content-length"]) {
|
|
353
|
+
const parsedSize = Number.parseInt(statResult.metaData["content-length"], 10);
|
|
354
|
+
if (!Number.isNaN(parsedSize) && parsedSize >= 0 && Number.isFinite(parsedSize)) {
|
|
355
|
+
size = parsedSize;
|
|
356
|
+
}
|
|
357
|
+
} else if (typeof statResult.size === "number" && !Number.isNaN(statResult.size)) {
|
|
358
|
+
size = statResult.size;
|
|
359
|
+
}
|
|
360
|
+
const fileMetadata = {
|
|
361
|
+
id: fileId,
|
|
362
|
+
name: fileId.split("/").pop() || fileId,
|
|
363
|
+
contentType: statResult.metaData["content-type"] || "application/octet-stream",
|
|
364
|
+
size,
|
|
365
|
+
uploadedAt: statResult.lastModified.toISOString(),
|
|
366
|
+
etag: statResult.etag,
|
|
367
|
+
url
|
|
368
|
+
};
|
|
369
|
+
return (0, __settlemint_sdk_utils_validation.validate)(FileMetadataSchema, fileMetadata);
|
|
370
|
+
} catch (error) {
|
|
371
|
+
console.error(`Failed to get file ${fileId}:`, error);
|
|
372
|
+
throw new Error(`Failed to get file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
373
|
+
}
|
|
215
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* Deletes a file from storage
|
|
377
|
+
*
|
|
378
|
+
* @param client - The MinIO client to use
|
|
379
|
+
* @param fileId - The file identifier/path
|
|
380
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
381
|
+
* @returns Success status
|
|
382
|
+
* @throws Will throw an error if deletion fails or client initialization fails
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* import { createServerMinioClient, deleteFile } from "@settlemint/sdk-minio";
|
|
386
|
+
*
|
|
387
|
+
* const { client } = createServerMinioClient({
|
|
388
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
389
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
390
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
391
|
+
* });
|
|
392
|
+
*
|
|
393
|
+
* await deleteFile(client, "documents/report.pdf");
|
|
394
|
+
*/
|
|
216
395
|
async function deleteFile(client, fileId, bucket = DEFAULT_BUCKET) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
396
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
397
|
+
try {
|
|
398
|
+
const deleteOperation = createDeleteOperation(bucket, fileId);
|
|
399
|
+
await executeMinioOperation(client, deleteOperation);
|
|
400
|
+
return true;
|
|
401
|
+
} catch (error) {
|
|
402
|
+
console.error(`Failed to delete file ${fileId}:`, error);
|
|
403
|
+
throw new Error(`Failed to delete file ${fileId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
404
|
+
}
|
|
226
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* Creates a presigned upload URL for direct browser uploads
|
|
408
|
+
*
|
|
409
|
+
* @param client - The MinIO client to use
|
|
410
|
+
* @param fileName - The file name to use
|
|
411
|
+
* @param path - Optional path/folder
|
|
412
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
413
|
+
* @param expirySeconds - How long the URL should be valid for
|
|
414
|
+
* @returns Presigned URL for PUT operation
|
|
415
|
+
* @throws Will throw an error if URL creation fails or client initialization fails
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* import { createServerMinioClient, createPresignedUploadUrl } from "@settlemint/sdk-minio";
|
|
419
|
+
*
|
|
420
|
+
* const { client } = createServerMinioClient({
|
|
421
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
422
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
423
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
424
|
+
* });
|
|
425
|
+
*
|
|
426
|
+
* // Generate the presigned URL on the server
|
|
427
|
+
* const url = await createPresignedUploadUrl(client, "report.pdf", "documents/");
|
|
428
|
+
*
|
|
429
|
+
* // Send the URL to the client/browser via HTTP response
|
|
430
|
+
* return Response.json({ uploadUrl: url });
|
|
431
|
+
*
|
|
432
|
+
* // Then in the browser:
|
|
433
|
+
* const response = await fetch('/api/get-upload-url');
|
|
434
|
+
* const { uploadUrl } = await response.json();
|
|
435
|
+
* await fetch(uploadUrl, {
|
|
436
|
+
* method: 'PUT',
|
|
437
|
+
* headers: { 'Content-Type': 'application/pdf' },
|
|
438
|
+
* body: pdfFile
|
|
439
|
+
* });
|
|
440
|
+
*/
|
|
227
441
|
async function createPresignedUploadUrl(client, fileName, path = "", bucket = DEFAULT_BUCKET, expirySeconds = 3600) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
442
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
443
|
+
try {
|
|
444
|
+
const safeFileName = fileName.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
445
|
+
const objectName = normalizePath(path, safeFileName);
|
|
446
|
+
const presignedPutOperation = createPresignedPutOperation(bucket, objectName, expirySeconds);
|
|
447
|
+
const url = await executeMinioOperation(client, presignedPutOperation);
|
|
448
|
+
if (!url) {
|
|
449
|
+
throw new Error("Failed to generate presigned upload URL");
|
|
450
|
+
}
|
|
451
|
+
return url;
|
|
452
|
+
} catch (error) {
|
|
453
|
+
console.error("Failed to create presigned upload URL:", error);
|
|
454
|
+
throw new Error(`Failed to create presigned upload URL: ${error instanceof Error ? error.message : String(error)}`);
|
|
455
|
+
}
|
|
242
456
|
}
|
|
457
|
+
/**
|
|
458
|
+
* Uploads a buffer directly to storage
|
|
459
|
+
*
|
|
460
|
+
* @param client - The MinIO client to use
|
|
461
|
+
* @param buffer - The buffer to upload
|
|
462
|
+
* @param objectName - The full object name/path
|
|
463
|
+
* @param contentType - The content type of the file
|
|
464
|
+
* @param bucket - Optional bucket name (defaults to DEFAULT_BUCKET)
|
|
465
|
+
* @returns The uploaded file metadata
|
|
466
|
+
* @throws Will throw an error if upload fails or client initialization fails
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* import { createServerMinioClient, uploadBuffer } from "@settlemint/sdk-minio";
|
|
470
|
+
*
|
|
471
|
+
* const { client } = createServerMinioClient({
|
|
472
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
473
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
474
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
475
|
+
* });
|
|
476
|
+
*
|
|
477
|
+
* const buffer = Buffer.from("Hello, world!");
|
|
478
|
+
* const uploadedFile = await uploadFile(client, buffer, "documents/hello.txt", "text/plain");
|
|
479
|
+
*/
|
|
243
480
|
async function uploadFile(client, buffer, objectName, contentType, bucket = DEFAULT_BUCKET) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
return (0, import_validation2.validate)(FileMetadataSchema, fileMetadata);
|
|
270
|
-
} catch (error) {
|
|
271
|
-
console.error("Failed to upload file:", error);
|
|
272
|
-
throw new Error(`Failed to upload file: ${error instanceof Error ? error.message : String(error)}`);
|
|
273
|
-
}
|
|
481
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
482
|
+
try {
|
|
483
|
+
const metadata = {
|
|
484
|
+
"content-type": contentType,
|
|
485
|
+
"upload-time": new Date().toISOString()
|
|
486
|
+
};
|
|
487
|
+
const simpleUploadFn = createSimpleUploadOperation(client);
|
|
488
|
+
const result = await simpleUploadFn(buffer, bucket, objectName, metadata);
|
|
489
|
+
const presignedUrlOperation = createPresignedUrlOperation(bucket, objectName, 3600);
|
|
490
|
+
const url = await executeMinioOperation(client, presignedUrlOperation);
|
|
491
|
+
const fileName = objectName.split("/").pop() || objectName;
|
|
492
|
+
const fileMetadata = {
|
|
493
|
+
id: objectName,
|
|
494
|
+
name: fileName,
|
|
495
|
+
contentType,
|
|
496
|
+
size: buffer.length,
|
|
497
|
+
uploadedAt: new Date().toISOString(),
|
|
498
|
+
etag: result.etag,
|
|
499
|
+
url
|
|
500
|
+
};
|
|
501
|
+
return (0, __settlemint_sdk_utils_validation.validate)(FileMetadataSchema, fileMetadata);
|
|
502
|
+
} catch (error) {
|
|
503
|
+
console.error("Failed to upload file:", error);
|
|
504
|
+
throw new Error(`Failed to upload file: ${error instanceof Error ? error.message : String(error)}`);
|
|
505
|
+
}
|
|
274
506
|
}
|
|
275
507
|
|
|
276
|
-
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/minio.ts
|
|
510
|
+
/**
|
|
511
|
+
* Creates a MinIO client for server-side use with authentication.
|
|
512
|
+
*
|
|
513
|
+
* @param options - The server client options for configuring the MinIO client
|
|
514
|
+
* @returns An object containing the initialized MinIO client
|
|
515
|
+
* @throws Will throw an error if not called on the server or if the options fail validation
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* import { createServerMinioClient } from "@settlemint/sdk-minio";
|
|
519
|
+
*
|
|
520
|
+
* const { client } = createServerMinioClient({
|
|
521
|
+
* instance: process.env.SETTLEMINT_MINIO_ENDPOINT!,
|
|
522
|
+
* accessKey: process.env.SETTLEMINT_MINIO_ACCESS_KEY!,
|
|
523
|
+
* secretKey: process.env.SETTLEMINT_MINIO_SECRET_KEY!
|
|
524
|
+
* });
|
|
525
|
+
* client.listBuckets();
|
|
526
|
+
*/
|
|
277
527
|
function createServerMinioClient(options) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
})
|
|
290
|
-
};
|
|
528
|
+
(0, __settlemint_sdk_utils_runtime.ensureServer)();
|
|
529
|
+
const validatedOptions = (0, __settlemint_sdk_utils_validation.validate)(ServerClientOptionsSchema, options);
|
|
530
|
+
const url = new URL(validatedOptions.instance);
|
|
531
|
+
return { client: new minio.Client({
|
|
532
|
+
endPoint: url.hostname,
|
|
533
|
+
accessKey: validatedOptions.accessKey,
|
|
534
|
+
secretKey: validatedOptions.secretKey,
|
|
535
|
+
useSSL: url.protocol !== "http:",
|
|
536
|
+
port: url.port ? Number(url.port) : undefined,
|
|
537
|
+
region: "eu-central-1"
|
|
538
|
+
}) };
|
|
291
539
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
});
|
|
540
|
+
|
|
541
|
+
//#endregion
|
|
542
|
+
exports.DEFAULT_BUCKET = DEFAULT_BUCKET;
|
|
543
|
+
exports.createPresignedUploadUrl = createPresignedUploadUrl;
|
|
544
|
+
exports.createServerMinioClient = createServerMinioClient;
|
|
545
|
+
exports.deleteFile = deleteFile;
|
|
546
|
+
exports.getFileById = getFileById;
|
|
547
|
+
exports.getFilesList = getFilesList;
|
|
548
|
+
exports.uploadFile = uploadFile;
|
|
302
549
|
//# sourceMappingURL=minio.cjs.map
|