-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.js
More file actions
188 lines (173 loc) · 5.22 KB
/
Copy pathaws.js
File metadata and controls
188 lines (173 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2025 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U.
// PROJECT: fiware-data-access
//
// This software and / or computer program has been developed by Telefónica Soluciones
// de Informática y Comunicaciones de España, S.A.U (hereinafter TSOL) and is protected
// as copyright by the applicable legislation on intellectual property.
//
// It belongs to TSOL, and / or its licensors, the exclusive rights of reproduction,
// distribution, public communication and transformation, and any economic right on it,
// all without prejudice of the moral rights of the authors mentioned above. It is expressly
// forbidden to decompile, disassemble, reverse engineer, sublicense or otherwise transmit
// by any means, translate or create derivative works of the software and / or computer
// programs, and perform with respect to all or part of such programs, any type of exploitation.
//
// Any use of all or part of the software and / or computer program will require the
// express written consent of TSOL. In all cases, it will be necessary to make
// an express reference to TSOL ownership in the software and / or computer
// program.
//
// Non-fulfillment of the provisions set forth herein and, in general, any violation of
// the peaceful possession and ownership of these rights will be prosecuted by the means
// provided in both Spanish and international law. TSOL reserves any civil or
// criminal actions it may exercise to protect its rights.
import {
S3Client,
CreateBucketCommand,
DeleteObjectCommand,
HeadBucketCommand,
CopyObjectCommand,
ListObjectsV2Command,
DeleteObjectsCommand,
} from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import { FDAError } from '../fdaError.js';
import { getBasicLogger } from './logger.js';
let s3ClientInstance = null;
const logger = getBasicLogger();
export function getS3Client(endpoint, user, password) {
if (!s3ClientInstance) {
s3ClientInstance = new S3Client({
endpoint,
region: 'REGION',
credentials: {
accessKeyId: user,
secretAccessKey: password,
},
forcePathStyle: true,
});
}
return s3ClientInstance;
}
export async function destroyS3Client() {
if (s3ClientInstance) {
await s3ClientInstance.destroy();
s3ClientInstance = null;
}
}
export function newUpload(client, bucket, path, body, partSize, queueSize) {
logger.debug(
{ bucket, path, body, partSize, queueSize },
'[DEBUG]: newUpload',
);
return new Upload({
client,
params: {
Bucket: bucket,
Key: path,
Body: body,
},
partSize: partSize * 1024 * 1024,
queueSize,
});
}
export async function dropFile(s3Client, bucket, path) {
logger.debug({ bucket, path }, '[DEBUG]: dropFile');
try {
await s3Client.send(
new DeleteObjectCommand({
Bucket: bucket,
Key: path,
}),
);
} catch (e) {
if (
e?.$metadata?.httpStatusCode === 404 ||
e?.name === 'NotFound' ||
e?.name === 'NoSuchKey' ||
e?.Code === 'NoSuchKey' ||
e?.code === 'NoSuchKey'
) {
return;
}
throw new FDAError(
500,
'S3ServerError',
`Error deleting file ${path} in bucket ${bucket}: ${e}`,
);
}
}
export async function dropFiles(s3Client, bucket, objsToRemove) {
logger.debug({ bucket, objsToRemove }, '[DEBUG]: dropFiles');
if (!objsToRemove?.length) {
return;
}
try {
await s3Client.send(
new DeleteObjectsCommand({
Bucket: bucket,
Delete: {
Objects: objsToRemove.map((k) => ({ Key: k })),
},
}),
);
} catch (e) {
throw new FDAError(
500,
'S3ServerError',
`Error deleting multiple objects ${objsToRemove} in bucket ${bucket}: ${e}`,
);
}
}
export async function moveObject(s3Client, bucket, sourceKey, destKey) {
logger.debug({ bucket, sourceKey, destKey }, '[DEBUG]: moveObject');
try {
await s3Client.send(
new CopyObjectCommand({
Bucket: bucket,
CopySource: sourceKey,
Key: destKey,
}),
);
} catch (e) {
throw new FDAError(
500,
'S3ServerError',
`Error moving ${sourceKey} into ${destKey}: ${e}`,
);
}
}
export async function listObjects(s3Client, bucket, prefix) {
logger.debug({ bucket, prefix }, '[DEBUG]: listObjects');
let response;
try {
response = await s3Client.send(
new ListObjectsV2Command({
Bucket: bucket,
Prefix: prefix,
}),
);
} catch (e) {
throw new FDAError(
500,
'S3ServerError',
`Error listing objects in bucket ${bucket} and path ${prefix}: ${e}`,
);
}
return response.Contents?.map((obj) => obj.Key) || [];
}
export async function createBucket(s3Client, bucket) {
try {
await s3Client.send(new HeadBucketCommand({ Bucket: bucket }));
logger.info(`Bucket "${bucket}" already exists.`);
} catch (err) {
if (err.$metadata && err.$metadata.httpStatusCode === 404) {
logger.info(`Bucket "${bucket}" not found. Creating...`);
await s3Client.send(new CreateBucketCommand({ Bucket: bucket }));
logger.info(`Bucket "${bucket}" created.`);
} else {
logger.error('Unexpected error checking bucket:', err);
throw err;
}
}
}