UNPKG

federer

Version:

Experiments in asynchronous federated learning and decentralized learning

90 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3 = void 0; const tslib_1 = require("tslib"); const assert = require("assert"); const fs = tslib_1.__importStar(require("fs")); const path = tslib_1.__importStar(require("path")); const stream = tslib_1.__importStar(require("stream")); const s3 = tslib_1.__importStar(require("@aws-sdk/client-s3")); const stream_to_buffer_1 = require("@jorgeferrero/stream-to-buffer"); const __1 = require(".."); class S3 { constructor(config) { this.s3 = new s3.S3(config); } async bucketExists(name) { const buckets = await this.listBuckets(); return buckets.some((bucket) => bucket.Name === name); } async createBucket(name) { await this.s3.createBucket({ Bucket: name, }); } async listBuckets() { const response = await this.s3.listBuckets({}); return response.Buckets ?? []; } async listObjects(bucketName) { const response = await this.s3.listObjects({ Bucket: bucketName, }); const objects = response.Contents ?? []; const truncated = response.IsTruncated ?? false; return { truncated, objects }; } async containsFile(bucketName, file) { const response = await this.s3.listObjects({ Bucket: bucketName, Prefix: this.keyFromFile(file), }); const objects = response.Contents ?? []; return objects.length > 0; } async upload(file, bucketName) { await this.s3.putObject({ Bucket: bucketName, Key: this.keyFromFile(file), Body: fs.createReadStream(file), }); } async download(file, bucketName) { const response = await this.s3.getObject({ Bucket: bucketName, Key: this.keyFromFile(file), }); const body = response.Body; assert(body !== undefined); if (body instanceof stream.Readable) { return stream_to_buffer_1.streamToBuffer(body); } else { // If we reach this branch, the body is either a Blob or a ReadableStream. // // Blobs are a browser API, so the value should never be able to be a Blob // on Node.js // // ReadableStream is the web browser equivalent to Node.js' // stream.Readable, so we don't expect this to happen either on Node.js. // See https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream throw new Error(`Unsupported response body type: ${body}`); } } async deleteFile(file, bucketName) { await this.s3.deleteObject({ Bucket: bucketName, Key: this.keyFromFile(file), }); } async deleteBucket(name) { await this.s3.deleteBucket({ Bucket: name, }); } keyFromFile(file) { return path.isAbsolute(file) ? __1.relative(file) : file; } } exports.S3 = S3; //# sourceMappingURL=s3.js.map