initial-commit
This commit is contained in:
104
src/S3Adapter.js
Normal file
104
src/S3Adapter.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import { BaseNotesAdapter } from '@takerofnotes/plugin-sdk'
|
||||
import {
|
||||
S3Client,
|
||||
ListObjectsV2Command,
|
||||
GetObjectCommand,
|
||||
PutObjectCommand,
|
||||
DeleteObjectCommand,
|
||||
HeadBucketCommand,
|
||||
} from '@aws-sdk/client-s3'
|
||||
|
||||
export default class S3Adapter extends BaseNotesAdapter {
|
||||
constructor(config) {
|
||||
super()
|
||||
|
||||
for (const field in config) {
|
||||
this[field] = config[field]
|
||||
}
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.s3Client = new S3Client({
|
||||
endpoint: this.endpoint,
|
||||
region: this.region || 'us-east-1',
|
||||
credentials: {
|
||||
accessKeyId: this.accessKeyId,
|
||||
secretAccessKey: this.secretAccessKey,
|
||||
},
|
||||
})
|
||||
|
||||
this.bucket = this.bucket || 'notes'
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
const command = new ListObjectsV2Command({
|
||||
Bucket: this.bucket,
|
||||
})
|
||||
|
||||
const response = await this.s3Client.send(command)
|
||||
const notes = []
|
||||
|
||||
if (!response.Contents) return notes
|
||||
|
||||
for (const object of response.Contents) {
|
||||
if (!object.Key.endsWith('.json')) continue
|
||||
|
||||
const getCommand = new GetObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: object.Key,
|
||||
})
|
||||
|
||||
try {
|
||||
const objectResponse = await this.s3Client.send(getCommand)
|
||||
const bodyString = await objectResponse.Body.transformToString()
|
||||
const note = JSON.parse(bodyString)
|
||||
notes.push(note)
|
||||
} catch (error) {
|
||||
console.error(`Failed to download note ${object.Key}:`, error)
|
||||
}
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
async create(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async update(note) {
|
||||
await this._write(note)
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
const command = new DeleteObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: `${id}.json`,
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
}
|
||||
|
||||
async testConnection() {
|
||||
try {
|
||||
const command = new HeadBucketCommand({
|
||||
Bucket: this.bucket,
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async _write(note) {
|
||||
const command = new PutObjectCommand({
|
||||
Bucket: this.bucket,
|
||||
Key: `${note.id}.json`,
|
||||
Body: JSON.stringify(note, null, 2),
|
||||
ContentType: 'application/json',
|
||||
})
|
||||
|
||||
await this.s3Client.send(command)
|
||||
}
|
||||
}
|
||||
51
src/index.js
Normal file
51
src/index.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { definePlugin } from '@takerofnotes/plugin-sdk'
|
||||
import S3Adapter from './S3Adapter'
|
||||
|
||||
export default definePlugin({
|
||||
id: 's3',
|
||||
name: 'S3',
|
||||
description:
|
||||
'Taker of Notes storage plugin for S3 compatible object storage',
|
||||
version: '0.1.0',
|
||||
apiVersion: '0.3.1',
|
||||
configSchema: [
|
||||
{
|
||||
key: 'endpoint',
|
||||
label: 'Endpoint URL',
|
||||
type: 'text',
|
||||
default: 'https://play.min.io',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'region',
|
||||
label: 'Region',
|
||||
type: 'text',
|
||||
default: 'us-east-1',
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
key: 'bucket',
|
||||
label: 'Bucket Name',
|
||||
type: 'text',
|
||||
default: 'notes',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'accessKeyId',
|
||||
label: 'Access Key ID',
|
||||
type: 'text',
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'secretAccessKey',
|
||||
label: 'Secret Access Key',
|
||||
type: 'password',
|
||||
default: '',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
createAdapter(config) {
|
||||
return new S3Adapter(config)
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user