chore(server): Use helpers to get S3 environment variables (#3118)
- use helper method when converting environment variable to boolean
This commit is contained in:
@@ -9,26 +9,29 @@ const {
|
||||
S3ServiceException
|
||||
} = require('@aws-sdk/client-s3')
|
||||
const { Upload } = require('@aws-sdk/lib-storage')
|
||||
const {
|
||||
getS3AccessKey,
|
||||
getS3SecretKey,
|
||||
getS3Endpoint,
|
||||
getS3Region,
|
||||
getS3BucketName,
|
||||
createS3Bucket
|
||||
} = require('@/modules/shared/helpers/envHelper')
|
||||
|
||||
let s3Config = null
|
||||
|
||||
const getS3Config = () => {
|
||||
if (!s3Config) {
|
||||
if (!process.env.S3_ACCESS_KEY)
|
||||
throw new Error('Config value S3_ACCESS_KEY is missing')
|
||||
if (!process.env.S3_SECRET_KEY)
|
||||
throw new Error('Config value S3_SECRET_KEY is missing')
|
||||
if (!process.env.S3_ENDPOINT) throw new Error('Config value S3_ENDPOINT is missing')
|
||||
s3Config = {
|
||||
credentials: {
|
||||
accessKeyId: process.env.S3_ACCESS_KEY,
|
||||
secretAccessKey: process.env.S3_SECRET_KEY
|
||||
accessKeyId: getS3AccessKey(),
|
||||
secretAccessKey: getS3SecretKey()
|
||||
},
|
||||
endpoint: process.env.S3_ENDPOINT,
|
||||
endpoint: getS3Endpoint(),
|
||||
forcePathStyle: true,
|
||||
// s3ForcePathStyle: true,
|
||||
// signatureVersion: 'v4',
|
||||
region: process.env.S3_REGION || 'us-east-1'
|
||||
region: getS3Region()
|
||||
}
|
||||
}
|
||||
return s3Config
|
||||
@@ -38,8 +41,7 @@ let storageBucket = null
|
||||
|
||||
const getStorageBucket = () => {
|
||||
if (!storageBucket) {
|
||||
if (!process.env.S3_BUCKET) throw new Error('Config value S3_BUCKET is missing')
|
||||
storageBucket = process.env.S3_BUCKET
|
||||
storageBucket = getS3BucketName()
|
||||
}
|
||||
return storageBucket
|
||||
}
|
||||
@@ -47,7 +49,7 @@ const getStorageBucket = () => {
|
||||
const getObjectStorage = () => ({
|
||||
client: new S3Client(getS3Config()),
|
||||
Bucket: getStorageBucket(),
|
||||
createBucket: process.env.S3_CREATE_BUCKET || false
|
||||
createBucket: createS3Bucket()
|
||||
})
|
||||
|
||||
const sendCommand = async (command) => {
|
||||
|
||||
@@ -290,7 +290,7 @@ export function getServerMovedTo() {
|
||||
}
|
||||
|
||||
export function adminOverrideEnabled() {
|
||||
return process.env.ADMIN_OVERRIDE_ENABLED === 'true'
|
||||
return getBooleanFromEnv('ADMIN_OVERRIDE_ENABLED')
|
||||
}
|
||||
|
||||
export function enableMixpanel() {
|
||||
@@ -309,7 +309,7 @@ export function speckleAutomateUrl() {
|
||||
}
|
||||
|
||||
export function weeklyEmailDigestEnabled() {
|
||||
return process.env.WEEKLY_DIGEST_ENABLED === 'true'
|
||||
return getBooleanFromEnv('WEEKLY_DIGEST_ENABLED')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,7 +399,7 @@ export function getLicenseToken(): string | undefined {
|
||||
}
|
||||
|
||||
export function isEmailEnabled() {
|
||||
return process.env.EMAIL === 'true'
|
||||
return getBooleanFromEnv('EMAIL')
|
||||
}
|
||||
|
||||
export function postgresMaxConnections() {
|
||||
@@ -413,3 +413,41 @@ export function highFrequencyMetricsCollectionPeriodMs() {
|
||||
export function maximumObjectUploadFileSizeMb() {
|
||||
return getIntFromEnv('MAX_OBJECT_UPLOAD_FILE_SIZE_MB', '100')
|
||||
}
|
||||
|
||||
export function getS3AccessKey() {
|
||||
if (!process.env.S3_ACCESS_KEY)
|
||||
throw new MisconfiguredEnvironmentError(
|
||||
'Environment variable S3_ACCESS_KEY is missing'
|
||||
)
|
||||
return process.env.S3_ACCESS_KEY
|
||||
}
|
||||
|
||||
export function getS3SecretKey() {
|
||||
if (!process.env.S3_SECRET_KEY)
|
||||
throw new MisconfiguredEnvironmentError(
|
||||
'Environment variable S3_SECRET_KEY is missing'
|
||||
)
|
||||
return process.env.S3_SECRET_KEY
|
||||
}
|
||||
|
||||
export function getS3Endpoint() {
|
||||
if (!process.env.S3_ENDPOINT)
|
||||
throw new MisconfiguredEnvironmentError(
|
||||
'Environment variable S3_ENDPOINT is missing'
|
||||
)
|
||||
return process.env.S3_ENDPOINT
|
||||
}
|
||||
|
||||
export function getS3Region(aDefault: string = 'us-east-1') {
|
||||
return process.env.S3_REGION || aDefault
|
||||
}
|
||||
|
||||
export function getS3BucketName() {
|
||||
if (!process.env.S3_BUCKET)
|
||||
throw new MisconfiguredEnvironmentError('Environment variable S3_BUCKET is missing')
|
||||
return process.env.S3_BUCKET
|
||||
}
|
||||
|
||||
export function createS3Bucket() {
|
||||
return getBooleanFromEnv('S3_CREATE_BUCKET')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user