head bucket

This commit is contained in:
fyears 2021-11-07 15:29:18 +08:00
parent 2ce34379ad
commit a65f5eb004
2 changed files with 56 additions and 1 deletions

View File

@ -24,7 +24,13 @@ import {
import type { SyncStatusType } from "./sync"; import type { SyncStatusType } from "./sync";
import { getSyncPlan, doActualSync } from "./sync"; import { getSyncPlan, doActualSync } from "./sync";
import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3"; import {
DEFAULT_S3_CONFIG,
getS3Client,
listFromRemote,
S3Config,
checkS3Connectivity,
} from "./s3";
import { exportSyncPlansToFiles } from "./debugMode"; import { exportSyncPlansToFiles } from "./debugMode";
interface SaveRemotePluginSettings { interface SaveRemotePluginSettings {
@ -247,6 +253,26 @@ class SaveRemoteSettingTab extends PluginSettingTab {
}) })
); );
new Setting(containerEl)
.setName("check connectivity")
.setDesc("check connectivity")
.addButton(async (button) => {
button.setButtonText("Check");
button.onClick(async () => {
new Notice("Checking...");
const s3Client = getS3Client(this.plugin.settings.s3);
const res = await checkS3Connectivity(
s3Client,
this.plugin.settings.s3
);
if (res) {
new Notice("Great! The bucket can be accessed.");
} else {
new Notice("The S3 bucket cannot be reached.");
}
});
});
containerEl.createEl("h2", { text: "General" }); containerEl.createEl("h2", { text: "General" });
new Setting(containerEl) new Setting(containerEl)
.setName("encryption password") .setName("encryption password")

View File

@ -10,6 +10,7 @@ import {
GetObjectCommand, GetObjectCommand,
DeleteObjectCommand, DeleteObjectCommand,
HeadObjectCommand, HeadObjectCommand,
HeadBucketCommand,
} from "@aws-sdk/client-s3"; } from "@aws-sdk/client-s3";
import type { _Object } from "@aws-sdk/client-s3"; import type { _Object } from "@aws-sdk/client-s3";
@ -266,3 +267,31 @@ export const deleteFromRemote = async (
// pass // pass
} }
}; };
/**
* Check the config of S3 by heading bucket
* https://stackoverflow.com/questions/50842835
* @param s3Client
* @param s3Config
* @returns
*/
export const checkS3Connectivity = async (
s3Client: S3Client,
s3Config: S3Config
) => {
try {
const results = await s3Client.send(
new HeadBucketCommand({ Bucket: s3Config.s3BucketName })
);
if (
results === undefined ||
results.$metadata === undefined ||
results.$metadata.httpStatusCode === undefined
) {
return false;
}
return results.$metadata.httpStatusCode === 200;
} catch (err) {
return false;
}
};