payload-plugin-cloud-storage
Version:
Remote storage plugin for Payload CMS.
54 lines (53 loc) • 1.95 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const AWS = __importStar(require("@aws-sdk/client-s3"));
class S3Adapter {
constructor(s3Configuration, fileOptions, getEndpoint) {
this.instance = new AWS.S3(s3Configuration);
this.options = fileOptions;
if (getEndpoint) {
this.getEndpointUrlRef = getEndpoint;
}
}
getEndpointUrl(filename) {
if (this.getEndpointUrlRef) {
return this.getEndpointUrlRef(this.options.endpointUrl, filename);
}
return `${this.options.endpointUrl}/${filename}`;
}
async upload(file) {
return await this.instance.putObject({
Bucket: this.options.bucket,
Key: file.name,
Body: file.data,
ACL: this.options.acl,
ContentType: file.mimetype
});
}
async delete(filename) {
return await this.instance.deleteObject({
Bucket: process.env.SPACES_NAME,
Key: String(filename)
});
}
}
exports.default = S3Adapter;