digitaltwin-core
Version:
Minimalist framework to collect and handle data in a Digital Twin project
70 lines • 2.42 kB
JavaScript
/**
* OVH Object Storage implementation of StorageService
* via S3-compatible API using @aws-sdk/client-s3
*/
import { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
import { StorageService } from '../storage_service.js';
export class OvhS3StorageService extends StorageService {
#s3;
#bucket;
constructor(config) {
super();
this.#bucket = config.bucket;
this.#s3 = new S3Client({
endpoint: config.endpoint,
region: config.region ?? 'gra',
credentials: {
accessKeyId: config.accessKey,
secretAccessKey: config.secretKey
},
forcePathStyle: false
});
}
/**
* Uploads a file to the OVH S3-compatible object storage.
* @param buffer - File contents to upload
* @param collectorName - Folder/prefix to store under
* @param extension - Optional file extension (e.g. 'json')
* @returns The relative path (key) of the stored object
*/
async save(buffer, collectorName, extension) {
const now = new Date();
const timestamp = now.toISOString().replace(/[:.]/g, '-');
const key = `${collectorName || 'default'}/${timestamp}${extension ? '.' + extension : ''}`;
await this.#s3.send(new PutObjectCommand({
Bucket: this.#bucket,
Key: key,
Body: buffer,
ACL: 'private'
}));
return key;
}
/**
* Downloads and returns a stored object as a Buffer.
* @param relativePath - The key/path of the object to retrieve
* @returns The object contents as a Buffer
*/
async retrieve(relativePath) {
const res = await this.#s3.send(new GetObjectCommand({
Bucket: this.#bucket,
Key: relativePath
}));
const chunks = [];
const stream = res.Body;
for await (const chunk of stream) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks);
}
/**
* Deletes an object from the storage bucket.
* @param relativePath - The key/path of the object to delete
*/
async delete(relativePath) {
await this.#s3.send(new DeleteObjectCommand({
Bucket: this.#bucket,
Key: relativePath
}));
}
}
//# sourceMappingURL=ovh_storage_service.js.map