"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3 = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const credential_providers_1 = require("@aws-sdk/credential-providers"); // ES6 import
const promise_error_handler_1 = __importDefault(require("../error/promise-error-handler"));
class S3 {
    constructor(config) {
        this.put = (bucketName, key, data) => __awaiter(this, void 0, void 0, function* () {
            const params = {
                Bucket: bucketName,
                Key: key,
                Body: data
            };
            const command = new client_s3_1.PutObjectCommand(params);
            const { error } = yield (0, promise_error_handler_1.default)(this.client.send(command));
            if (error) {
                return Promise.reject(error);
            }
            // Return true if upload succeded
            return true;
        });
        this.get = (bucketName, key) => __awaiter(this, void 0, void 0, function* () {
            const bucketParams = {
                Bucket: bucketName,
                Key: key
            };
            const { result: data, error } = yield (0, promise_error_handler_1.default)(this.client.send(new client_s3_1.GetObjectCommand(bucketParams)));
            if (error) {
                return Promise.reject(error);
            }
            return data === null || data === void 0 ? void 0 : data.Body;
        });
        const settings = {
            credentials: (0, credential_providers_1.fromNodeProviderChain)(),
            region: config.region,
        };
        this.client = new client_s3_1.S3Client(settings);
    }
}
exports.S3 = S3;
//# sourceMappingURL=s3.js.map