2022-08-12 00:50:05 +12:00
import { Integration , QueryType , IntegrationBase } from "@budibase/types"
2022-08-13 04:03:06 +12:00
const AWS = require ( "aws-sdk" )
2021-06-25 05:16:48 +12:00
2022-08-13 04:03:06 +12:00
interface S3Config {
region : string
accessKeyId : string
secretAccessKey : string
s3ForcePathStyle : boolean
endpoint? : string
}
2021-06-25 05:16:48 +12:00
2022-08-13 04:03:06 +12:00
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" ,
type : "Object store" ,
datasource : {
region : {
type : "string" ,
required : false ,
default : "us-east-1" ,
} ,
accessKeyId : {
type : "password" ,
required : true ,
2021-06-25 05:16:48 +12:00
} ,
2022-08-13 04:03:06 +12:00
secretAccessKey : {
type : "password" ,
required : true ,
} ,
endpoint : {
type : "string" ,
required : false ,
} ,
signatureVersion : {
type : "string" ,
required : false ,
default : "v4" ,
} ,
} ,
query : {
read : {
type : QueryType . FIELDS ,
fields : {
bucket : {
type : "string" ,
required : true ,
2021-06-25 05:16:48 +12:00
} ,
} ,
} ,
2022-08-13 04:03:06 +12:00
} ,
}
2021-06-25 05:16:48 +12:00
2022-08-13 04:03:06 +12:00
class S3Integration implements IntegrationBase {
private readonly config : S3Config
private client : any
2021-06-25 05:16:48 +12:00
2022-08-13 04:03:06 +12:00
constructor ( config : S3Config ) {
this . config = config
if ( this . config . endpoint ) {
this . config . s3ForcePathStyle = true
} else {
delete this . config . endpoint
2021-06-25 05:16:48 +12:00
}
2022-08-13 04:03:06 +12:00
this . client = new AWS . S3 ( this . config )
2021-06-25 05:16:48 +12:00
}
2022-08-13 04:03:06 +12:00
async read ( query : { bucket : string } ) {
const response = await this . client
. listObjects ( {
Bucket : query.bucket ,
} )
. promise ( )
return response . Contents
2021-06-25 05:16:48 +12:00
}
}
2022-08-13 04:03:06 +12:00
export default {
schema : SCHEMA ,
integration : S3Integration ,
}