Skip to content

Commit

Permalink
Merge pull request elizaOS#2176 from dtbuchholz/2174--plugin-node-cus…
Browse files Browse the repository at this point in the history
…tom-s3-url

feat: custom s3 endpoint url for 'plugin-node'
  • Loading branch information
tcm390 authored Jan 23, 2025
2 parents 1c25851 + d5b4075 commit a20d512
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
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}` : "";
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

0 comments on commit a20d512

Please sign in to comment.