UNPKG

angular-aws-s3

Version:

Open Source Module to Upload your Media and files into AWS S3 Bucket directly from Front-end

94 lines (93 loc) 3.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Date_1 = require("./Date"); const ErrorThrower_1 = require("./ErrorThrower"); const Url_1 = __importDefault(require("./Url")); const Policy_1 = __importDefault(require("./Policy")); const Signature_1 = __importDefault(require("./Signature")); class AWSS3UploadClient { constructor(config) { this.config = {}; if (config) { this.config = config; } } async uploadFile(file, contentType, presignedURL, newFileName, acl) { if (!file) { throw new Error(`File cannot be empty`); } if (!presignedURL) { ErrorThrower_1.throwError(this.config, file); } if (presignedURL) { try { const payload = { method: "PUT", headers: { 'Content-Type': contentType }, body: file }; await fetch(presignedURL, payload); return Promise.resolve({ status: 200, body: "Upload complete" }); } catch (err) { return Promise.resolve({ status: 400, body: "Upload failed" }); } } else { const fd = new FormData(); const url = Url_1.default(this.config); const directory = `${this.config.dirName ? this.config.dirName + "/" : ""}`; const key = `${directory}${newFileName}`; const location = `${url}${key}`; const aclFinal = acl === undefined || acl === "" ? "public-read" : acl; fd.append("key", key); fd.append("acl", aclFinal); fd.append("Content-Type", contentType); fd.append("x-amz-meta-uuid", "14365123651274"); fd.append("x-amz-server-side-encryption", "AES256"); fd.append("X-Amz-Date", Date_1.xAmzDate); fd.append("X-Amz-Credential", `${this.config.accessKeyId}/${Date_1.dateYMD}/${this.config.region}/s3/aws4_request`); fd.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256"); fd.append("x-amz-meta-tag", ""); fd.append("Policy", Policy_1.default.getPolicy(this.config, aclFinal)); fd.append("X-Amz-Signature", Signature_1.default.getSignature(this.config, Date_1.dateYMD, Policy_1.default.getPolicy(this.config, aclFinal))); fd.append("file", file); const data = await fetch(url, { method: "post", body: fd }); if (!data.ok) return Promise.reject(data); return Promise.resolve({ bucket: this.config.bucketName, key: key, location: location, status: data.status }); } } async deleteFile(fileName) { const region = `${this.config.region ? "-" + this.config.region : ""}`; const directory = `${this.config.dirName ? this.config.dirName + "/" : ""}`; const pathFile = `${directory}${fileName}`; const url = `https://${this.config.bucketName}.s3${region}.amazonaws.com/${pathFile}`; const deleteResult = await fetch(url, { method: "delete" }); if (!deleteResult.ok) return Promise.reject(deleteResult); return Promise.resolve({ ok: deleteResult.ok, status: deleteResult.status, message: "File Deleted", fileName: pathFile }); } } exports.default = AWSS3UploadClient;