@uploadx/s3
Version:
Uploadx S3 module
64 lines (63 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3MetaStorage = void 0;
const client_s3_1 = require("@aws-sdk/client-s3");
const credential_providers_1 = require("@aws-sdk/credential-providers");
const core_1 = require("@uploadx/core");
const BUCKET_NAME = 'node-uploadx';
class S3MetaStorage extends core_1.MetaStorage {
constructor(config) {
super(config);
this.config = config;
this.bucket = config.bucket || process.env.S3_BUCKET || BUCKET_NAME;
const keyFile = config.keyFile || process.env.S3_KEYFILE;
keyFile && (config.credentials = (0, credential_providers_1.fromIni)({ configFilepath: keyFile }));
const clientConfig = { ...config };
clientConfig.logger = (0, core_1.toBoolean)(process.env.S3_DEBUG) ? this.logger : undefined;
this.client = new client_s3_1.S3Client(clientConfig);
}
async get(id) {
const params = { Bucket: this.bucket, Key: this.getMetaName(id) };
const { Metadata } = await this.client.send(new client_s3_1.HeadObjectCommand(params));
if (Metadata) {
return JSON.parse(decodeURIComponent(Metadata.metadata));
}
return Promise.reject();
}
async delete(id) {
const params = { Bucket: this.bucket, Key: this.getMetaName(id) };
await this.client.send(new client_s3_1.DeleteObjectCommand(params));
}
async save(id, file) {
const metadata = encodeURIComponent(JSON.stringify(file));
const params = {
Bucket: this.bucket,
Key: this.getMetaName(id),
Metadata: { metadata },
ContentLength: 0
};
await this.client.send(new client_s3_1.PutObjectCommand(params));
return file;
}
async list(prefix) {
const params = {
Bucket: this.bucket,
Prefix: this.prefix + prefix
};
const items = [];
const response = await this.client.send(new client_s3_1.ListObjectsV2Command(params));
if (response.Contents?.length) {
for (const { Key, LastModified } of response.Contents) {
Key &&
LastModified &&
Key.endsWith(this.suffix) &&
items.push({
id: this.getIdFromMetaName(Key),
createdAt: LastModified
});
}
}
return { items, prefix };
}
}
exports.S3MetaStorage = S3MetaStorage;