Newer
Older
import { defineStore } from "pinia";
import type { _Object as S3Object, HeadObjectOutput } from "@aws-sdk/client-s3";
import {
CopyObjectCommand,
GetObjectCommand,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3";
import {
paginateListObjectsV2,
DeleteObjectCommand,
DeleteObjectsCommand,
} from "@aws-sdk/client-s3";
import { environment } from "@/environment";
import type { S3Key } from "@/client/s3proxy";
import { useBucketStore } from "@/stores/buckets";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import dayjs from "dayjs";
import { Upload } from "@aws-sdk/lib-storage";
import type { Progress } from "@aws-sdk/lib-storage";
export const useS3ObjectStore = defineStore({
id: "s3objects",
state: () =>
({
objectMapping: {},
client: new S3Client({
region: "us-east-1",
endpoint: environment.S3_URL,
forcePathStyle: true,
credentials: {
accessKeyId: "",
secretAccessKey: "",
},
}),
}) as {
objectMapping: Record<string, S3Object[]>;
objectMetaMapping: Record<string, HeadObjectOutput>;
client: S3Client;
},
getters: {
getPresignedUrl(): (bucketName: string, key: string) => Promise<string> {
return (bucketName, key) => {
const keySplit = key.split("/");
const command = new GetObjectCommand({
Bucket: bucketName,
Key: key,
ResponseContentDisposition: `attachment; filename=${
keySplit[keySplit.length - 1]
}`,
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
});
return getSignedUrl(this.client, command, {
expiresIn: 30,
});
};
},
},
actions: {
_pushObject(bucketName: string, newObj: S3Object) {
if (this.objectMapping[bucketName] == undefined) {
this.fetchS3Objects(bucketName);
} else {
const objIndex = this.objectMapping[bucketName].findIndex(
(obj) => obj.Key === newObj.Key,
);
if (objIndex > -1) {
this.objectMapping[bucketName][objIndex] = newObj;
} else {
this.objectMapping[bucketName].push(newObj);
}
}
},
updateS3Client(s3Key: S3Key) {
this.client = new S3Client({
region: "us-east-1",
endpoint: environment.S3_URL,
forcePathStyle: true,
credentials: {
accessKeyId: s3Key.access_key,
secretAccessKey: s3Key.secret_key,
},
});
},
async fetchS3Objects(
bucketName: string,
prefix?: string,
onFinally?: () => void,
): Promise<S3Object[]> {
if (this.objectMapping[bucketName] != undefined) {
onFinally?.();
}
const pag = paginateListObjectsV2(
{ client: this.client },
{ Bucket: bucketName, Prefix: prefix },
);
const objs: S3Object[] = [];
try {
for await (const page of pag) {
objs.push(...(page.Contents ?? []));
}
this.objectMapping[bucketName] = objs;
} finally {
onFinally?.();
}
return objs;
},
fetchS3ObjectMeta(
bucketName: string,
key: string,
onFinally?: () => void,
): Promise<HeadObjectOutput> {
const identifier = bucketName + "/" + key;
if (this.objectMetaMapping[identifier]) {
onFinally?.();
}
const command = new HeadObjectCommand({
Bucket: bucketName,
Key: key,
});
return this.client
.send(command)
.then((resp) => {
this.objectMetaMapping[identifier] = resp;
return resp;
})
.finally(onFinally);
},
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
deleteObject(bucketName: string, key: string): Promise<void> {
const command = new DeleteObjectCommand({
Bucket: bucketName,
Key: key,
});
return this.client
.send(command)
.then(() => {
const bucketRepository = useBucketStore();
bucketRepository.fetchBucket(bucketName);
if (this.objectMapping[bucketName] == undefined) {
this.fetchS3Objects(bucketName);
} else {
this.objectMapping[bucketName] = this.objectMapping[
bucketName
].filter((obj) => obj.Key !== key);
}
})
.catch((err) => {
console.error(err);
});
},
deleteObjectsWithPrefix(bucketName: string, prefix: string): Promise<void> {
if (this.objectMapping[bucketName] == undefined) {
return Promise.resolve();
}
const command = new DeleteObjectsCommand({
Bucket: bucketName,
Delete: {
Objects: this.objectMapping[bucketName]
.filter((obj) => obj.Key?.startsWith(prefix))
.map((obj) => {
return { Key: obj.Key };
}),
},
});
return this.client.send(command).then(() => {
const bucketRepository = useBucketStore();
bucketRepository.fetchBucket(bucketName);
if (this.objectMapping[bucketName] == undefined) {
this.fetchS3Objects(bucketName);
} else {
this.objectMapping[bucketName] = this.objectMapping[
bucketName
].filter((obj) => !obj.Key?.startsWith(prefix));
}
});
},
copyObject(
srcBucket: string,
srcObject: S3Object,
destBucket: string,
destKey: string,
): Promise<S3Object> {
if (srcObject.Key == undefined) {
return Promise.resolve({});
}
const command = new CopyObjectCommand({
Bucket: destBucket,
CopySource: encodeURI(`/${srcBucket}/${srcObject.Key}`),
Key: destKey,
});
return this.client.send(command).then(() => {
const newObj = {
Key: destKey,
Size: srcObject.Size,
LastModified: dayjs().toDate(),
};
this._pushObject(destBucket, newObj);
return newObj;
});
},
async uploadObjectFile(
bucketName: string,
key: string,
file: File,
onProgress?: (progress: Progress) => void,
): Promise<S3Object> {
const parallelUploads3 = new Upload({
client: this.client,
params: {
Bucket: bucketName,
Body: file,
ContentType: file.type,
Key: key,
},
queueSize: 4, // optional concurrency configuration
leavePartsOnError: false, // optional manually handle dropped parts
});
if (onProgress != undefined) {
parallelUploads3.on("httpUploadProgress", onProgress);
}
await parallelUploads3.done();
const newObj = {
Key: key,
Size: file.size ?? 0,
LastModified: dayjs().toDate(),
};
this._pushObject(bucketName, newObj);
return newObj;
},
createFolder(bucketName: string, key: string): Promise<S3Object> {
const command = new PutObjectCommand({
Bucket: bucketName,
Body: "",
ContentType: "text/plain",
Key: key,
});
return this.client.send(command).then(() => {
const newObj = {
Key: key,
Size: 0,
LastModified: dayjs().toDate(),
};
this._pushObject(bucketName, newObj);
return newObj;
});
},
},
});