🌟Add an option to not delete files from S3 storage (#321)

This commit is contained in:
Anton Shepilov
2024-01-14 02:40:02 +03:00
committed by GitHub
parent 70c033aabe
commit 873ef8c08c
5 changed files with 65 additions and 5 deletions
@@ -94,7 +94,8 @@
"port": "STORAGE_S3_PORT", "port": "STORAGE_S3_PORT",
"useSSL": "STORAGE_S3_USE_SSL", "useSSL": "STORAGE_S3_USE_SSL",
"accessKey": "STORAGE_S3_ACCESS_KEY", "accessKey": "STORAGE_S3_ACCESS_KEY",
"secretKey": "STORAGE_S3_SECRET_KEY" "secretKey": "STORAGE_S3_SECRET_KEY",
"disableRemove": "STORAGE_S3_DISABLE_REMOVE"
} }
}, },
"email-pusher": { "email-pusher": {
+2 -1
View File
@@ -121,7 +121,8 @@
"port": 9000, "port": 9000,
"useSSL": false, "useSSL": false,
"accessKey": "ABCD", "accessKey": "ABCD",
"secretKey": "x1yz" "secretKey": "x1yz",
"disableRemove": false
}, },
"local": { "local": {
"path": "/tdrive" "path": "/tdrive"
@@ -11,6 +11,7 @@ export type S3Configuration = {
useSSL: boolean; useSSL: boolean;
accessKey: string; accessKey: string;
secretKey: string; secretKey: string;
disableRemove: boolean;
}; };
export default class S3ConnectorService implements StorageConnectorAPI { export default class S3ConnectorService implements StorageConnectorAPI {
@@ -66,8 +67,13 @@ export default class S3ConnectorService implements StorageConnectorAPI {
async remove(path: string): Promise<boolean> { async remove(path: string): Promise<boolean> {
try { try {
await this.client.removeObject(this.minioConfiguration.bucket, path); if (this.minioConfiguration.disableRemove) {
return true; logger.info(`File ${path} wasn't removed, file removal is disabled in configuration`);
return true;
} else {
await this.client.removeObject(this.minioConfiguration.bucket, path);
return true;
}
} catch (err) {} } catch (err) {}
return false; return false;
} }
@@ -3,7 +3,7 @@ import { Stream, Readable } from "stream";
import Multistream from "multistream"; import Multistream from "multistream";
import { Consumes, logger, TdriveService } from "../../framework"; import { Consumes, logger, TdriveService } from "../../framework";
import LocalConnectorService, { LocalConfiguration } from "./connectors/local/service"; import LocalConnectorService, { LocalConfiguration } from "./connectors/local/service";
import S3ConnectorService from "./connectors/S3/service"; import S3ConnectorService from "./connectors/S3/s3-service";
import StorageAPI, { import StorageAPI, {
DeleteOptions, DeleteOptions,
ReadOptions, ReadOptions,
@@ -47,6 +47,7 @@ export default class StorageService extends TdriveService<StorageAPI> implements
useSSL: Boolean(this.configuration.get<boolean>("S3.useSSL")), useSSL: Boolean(this.configuration.get<boolean>("S3.useSSL")),
accessKey: this.configuration.get<string>("S3.accessKey"), accessKey: this.configuration.get<string>("S3.accessKey"),
secretKey: this.configuration.get<string>("S3.secretKey"), secretKey: this.configuration.get<string>("S3.secretKey"),
disableRemove: this.configuration.get<boolean>("S3.disableRemove"),
}); });
} else { } else {
logger.info("Using 'local' connector for storage."); logger.info("Using 'local' connector for storage.");
@@ -0,0 +1,51 @@
import S3ConnectorService, {
S3Configuration
} from "../../../../../../../src/core/platform/services/storage/connectors/S3/s3-service";
import { jest } from "@jest/globals";
describe("The S3ConnectorService", () => {
beforeEach(async () => {
});
afterEach(() => {
jest.clearAllMocks();
});
test("remove shouldnt remove file if the option is disabled", async () => {
//given
const subj: S3ConnectorService = new S3ConnectorService({
disableRemove: true,
endPoint: "play.min.io",
port: 9000
} as S3Configuration);
const removeSpy = jest.spyOn(subj.client, "removeObject");
//when
const result = await subj.remove("path");
//then
expect(result).toBeTruthy();
expect(removeSpy).not.toHaveBeenCalled();
});
test("remove should call remove if the option is not disabled", async () => {
//given
const subj: S3ConnectorService = new S3ConnectorService({
disableRemove: false,
endPoint: "play.min.io",
port: 9000,
bucket: "testbucket"
} as S3Configuration);
const removeSpy = jest.spyOn(subj.client, "removeObject");
removeSpy.mockReturnThis();
//when
const result = await subj.remove("path");
//then
expect(removeSpy).toHaveBeenCalled();
expect(removeSpy).toBeCalledWith("testbucket", "path");
});
});