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