budibase/packages/server/src/integrations/s3.ts

111 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-08-30 12:17:11 +02:00
import { Integration, QueryType, IntegrationBase, DatasourceFieldType } from "@budibase/types"
module S3Module {
const AWS = require("aws-sdk")
interface S3Config {
2021-06-24 19:17:26 +02:00
region: string
accessKeyId: string
secretAccessKey: string
s3ForcePathStyle: boolean
endpoint?: string
}
const SCHEMA: Integration = {
docs: "https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html",
description:
"Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance.",
friendlyName: "Amazon S3",
2022-06-23 12:35:57 +02:00
type: "Object store",
datasource: {
region: {
type: "string",
required: false,
default: "us-east-1",
},
accessKeyId: {
type: "password",
required: true,
},
secretAccessKey: {
type: "password",
required: true,
},
endpoint: {
type: "string",
required: false,
},
signatureVersion: {
type: "string",
required: false,
2022-01-13 18:24:52 +01:00
default: "v4",
},
},
query: {
read: {
type: QueryType.FIELDS,
fields: {
bucket: {
2022-08-30 12:17:11 +02:00
type: DatasourceFieldType.STRING,
required: true,
},
2022-08-30 12:17:11 +02:00
delimiter: {
type: DatasourceFieldType.STRING,
},
marker: {
type: DatasourceFieldType.STRING,
},
maxKeys: {
type: DatasourceFieldType.NUMBER,
display: "Max Keys",
},
prefix: {
type: DatasourceFieldType.STRING,
},
},
2022-08-30 12:17:11 +02:00
}
}
}
class S3Integration implements IntegrationBase {
private readonly config: S3Config
private client: any
constructor(config: S3Config) {
this.config = config
if (this.config.endpoint) {
this.config.s3ForcePathStyle = true
} else {
delete this.config.endpoint
}
this.client = new AWS.S3(this.config)
}
2022-08-30 12:17:11 +02:00
async read(query: {
bucket: string,
delimiter: string,
expectedBucketOwner: string,
marker: string,
maxKeys: number,
prefix: string,
}) {
const response = await this.client
.listObjects({
Bucket: query.bucket,
2022-08-30 12:17:11 +02:00
Delimiter: query.delimiter,
Marker: query.marker,
MaxKeys: query.maxKeys,
Prefix: query.prefix,
})
.promise()
return response.Contents
}
}
module.exports = {
schema: SCHEMA,
integration: S3Integration,
}
}