🌟Add an option to not delete files from S3 storage (#321)
This commit is contained in:
@@ -94,7 +94,8 @@
|
||||
"port": "STORAGE_S3_PORT",
|
||||
"useSSL": "STORAGE_S3_USE_SSL",
|
||||
"accessKey": "STORAGE_S3_ACCESS_KEY",
|
||||
"secretKey": "STORAGE_S3_SECRET_KEY"
|
||||
"secretKey": "STORAGE_S3_SECRET_KEY",
|
||||
"disableRemove": "STORAGE_S3_DISABLE_REMOVE"
|
||||
}
|
||||
},
|
||||
"email-pusher": {
|
||||
|
||||
@@ -121,7 +121,8 @@
|
||||
"port": 9000,
|
||||
"useSSL": false,
|
||||
"accessKey": "ABCD",
|
||||
"secretKey": "x1yz"
|
||||
"secretKey": "x1yz",
|
||||
"disableRemove": false
|
||||
},
|
||||
"local": {
|
||||
"path": "/tdrive"
|
||||
|
||||
+8
-2
@@ -11,6 +11,7 @@ export type S3Configuration = {
|
||||
useSSL: boolean;
|
||||
accessKey: string;
|
||||
secretKey: string;
|
||||
disableRemove: boolean;
|
||||
};
|
||||
|
||||
export default class S3ConnectorService implements StorageConnectorAPI {
|
||||
@@ -66,8 +67,13 @@ export default class S3ConnectorService implements StorageConnectorAPI {
|
||||
|
||||
async remove(path: string): Promise<boolean> {
|
||||
try {
|
||||
await this.client.removeObject(this.minioConfiguration.bucket, path);
|
||||
return true;
|
||||
if (this.minioConfiguration.disableRemove) {
|
||||
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) {}
|
||||
return false;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { Stream, Readable } from "stream";
|
||||
import Multistream from "multistream";
|
||||
import { Consumes, logger, TdriveService } from "../../framework";
|
||||
import LocalConnectorService, { LocalConfiguration } from "./connectors/local/service";
|
||||
import S3ConnectorService from "./connectors/S3/service";
|
||||
import S3ConnectorService from "./connectors/S3/s3-service";
|
||||
import StorageAPI, {
|
||||
DeleteOptions,
|
||||
ReadOptions,
|
||||
@@ -47,6 +47,7 @@ export default class StorageService extends TdriveService<StorageAPI> implements
|
||||
useSSL: Boolean(this.configuration.get<boolean>("S3.useSSL")),
|
||||
accessKey: this.configuration.get<string>("S3.accessKey"),
|
||||
secretKey: this.configuration.get<string>("S3.secretKey"),
|
||||
disableRemove: this.configuration.get<boolean>("S3.disableRemove"),
|
||||
});
|
||||
} else {
|
||||
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");
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user