Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: custom s3 endpoint url for 'plugin-node' #2176

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/plugin-node/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export async function validateNodeConfig(
AWS_REGION: runtime.getSetting("AWS_REGION"),
AWS_S3_BUCKET: runtime.getSetting("AWS_S3_BUCKET"),
AWS_S3_UPLOAD_PATH: runtime.getSetting("AWS_S3_UPLOAD_PATH"),
AWS_S3_ENDPOINT: runtime.getSetting("AWS_S3_ENDPOINT"),
AWS_S3_SSL_ENABLED: runtime.getSetting("AWS_S3_SSL_ENABLED"),
AWS_S3_FORCE_PATH_STYLE: runtime.getSetting(
"AWS_S3_FORCE_PATH_STYLE"
),
}),
};

Expand Down
44 changes: 29 additions & 15 deletions packages/plugin-node/src/services/awsS3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
S3Client,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import * as fs from "fs";
import * as path from "path";
import * as fs from "node:fs";
import * as path from "node:path";

interface UploadResult {
success: boolean;
Expand Down Expand Up @@ -44,7 +44,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {

const AWS_ACCESS_KEY_ID = this.runtime.getSetting("AWS_ACCESS_KEY_ID");
const AWS_SECRET_ACCESS_KEY = this.runtime.getSetting(
"AWS_SECRET_ACCESS_KEY"
"AWS_SECRET_ACCESS_KEY",
);
const AWS_REGION = this.runtime.getSetting("AWS_REGION");
const AWS_S3_BUCKET = this.runtime.getSetting("AWS_S3_BUCKET");
Expand All @@ -58,10 +58,12 @@ export class AwsS3Service extends Service implements IAwsS3Service {
return false;
}

/** Optional fields to allow for other providers */
// Optional fields to allow for other providers
const endpoint = this.runtime.getSetting("AWS_S3_ENDPOINT");
const sslEnabled = this.runtime.getSetting("AWS_S3_SSL_ENABLED");
const forcePathStyle = this.runtime.getSetting("AWS_S3_FORCE_PATH_STYLE");
const forcePathStyle = this.runtime.getSetting(
"AWS_S3_FORCE_PATH_STYLE",
);

this.s3Client = new S3Client({
...(endpoint ? { endpoint } : {}),
Expand All @@ -83,7 +85,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
filePath: string,
subDirectory = "",
useSignedUrl = false,
expiresIn = 900
expiresIn = 900,
): Promise<UploadResult> {
try {
if (!(await this.initializeS3Client())) {
Expand All @@ -107,7 +109,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
const fileName =
`${this.fileUploadPath}${subDirectory}/${baseFileName}`.replaceAll(
"//",
"/"
"/",
);
// Set upload parameters
const uploadParams = {
Expand All @@ -125,9 +127,15 @@ export class AwsS3Service extends Service implements IAwsS3Service {
success: true,
};

// If not using signed URL, return public access URL
// If not using signed URL, return either custom endpoint or public access URL
if (!useSignedUrl) {
result.url = `https://${this.bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${fileName}`;
if (this.s3Client.config.endpoint) {
const endpoint = await this.s3Client.config.endpoint();
const port = endpoint.port ? `:${endpoint.port}` : "";
odilitime marked this conversation as resolved.
Show resolved Hide resolved
result.url = `${endpoint.protocol}//${endpoint.hostname}${port}${endpoint.path}${this.bucket}/${fileName}`;
} else {
result.url = `https://${this.bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${fileName}`;
}
} else {
const getObjectCommand = new GetObjectCommand({
Bucket: this.bucket,
Expand All @@ -138,7 +146,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
getObjectCommand,
{
expiresIn, // 15 minutes in seconds
}
},
);
}

Expand All @@ -159,7 +167,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
*/
async generateSignedUrl(
fileName: string,
expiresIn = 900
expiresIn = 900,
): Promise<string> {
if (!(await this.initializeS3Client())) {
throw new Error("AWS S3 credentials not configured");
Expand Down Expand Up @@ -198,7 +206,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
fileName?: string,
subDirectory?: string,
useSignedUrl = false,
expiresIn = 900
expiresIn = 900,
): Promise<JsonUploadResult> {
try {
if (!(await this.initializeS3Client())) {
Expand Down Expand Up @@ -247,9 +255,15 @@ export class AwsS3Service extends Service implements IAwsS3Service {
key: key,
};

// Return corresponding URL based on requirements
// If not using signed URL, return either custom endpoint or public access URL
if (!useSignedUrl) {
result.url = `https://${this.bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`;
if (this.s3Client.config.endpoint) {
const endpoint = await this.s3Client.config.endpoint();
const port = endpoint.port ? `:${endpoint.port}` : "";
result.url = `${endpoint.protocol}//${endpoint.hostname}${port}${endpoint.path}${this.bucket}/${key}`;
} else {
result.url = `https://${this.bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`;
}
} else {
const getObjectCommand = new GetObjectCommand({
Bucket: this.bucket,
Expand All @@ -258,7 +272,7 @@ export class AwsS3Service extends Service implements IAwsS3Service {
result.url = await getSignedUrl(
this.s3Client,
getObjectCommand,
{ expiresIn }
{ expiresIn },
);
}

Expand Down
Loading