Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Setup s3 credentials with asset client (#1394)

Bogdan Chadkin6c9df7c655ff
diff --git a/apps/builder/app/shared/asset-client.ts b/apps/builder/app/shared/asset-client.ts
index bee0d4af5c..b100f1d5ba 100644
--- a/apps/builder/app/shared/asset-client.ts
+++ b/apps/builder/app/shared/asset-client.ts
@@ -13,6 +13,12 @@ export const createAssetClient = () => {
} else {
const env = S3Env.parse(process.env);
return createS3Client({
+ endpoint: env.S3_ENDPOINT,
+ region: env.S3_REGION,
+ accessKeyId: env.S3_ACCESS_KEY_ID,
+ secretAccessKey: env.S3_SECRET_ACCESS_KEY,
+ bucket: env.S3_BUCKET,
+ acl: env.S3_ACL,
maxUploadSize: env.MAX_UPLOAD_SIZE,
});
}
diff --git a/packages/asset-uploader/src/clients/s3/client.ts b/packages/asset-uploader/src/clients/s3/client.ts
deleted file mode 100644
index 1d0114519d..0000000000
--- a/packages/asset-uploader/src/clients/s3/client.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { S3Client } from "@aws-sdk/client-s3";
-import { S3Env } from "../../schema";
-
-export const getS3Client = () => {
- const s3Env = S3Env.parse(process.env);
- return new S3Client({
- endpoint: s3Env.S3_ENDPOINT,
- region: s3Env.S3_REGION,
- credentials: {
- accessKeyId: s3Env.S3_ACCESS_KEY_ID,
- secretAccessKey: s3Env.S3_SECRET_ACCESS_KEY,
- },
- });
-};
diff --git a/packages/asset-uploader/src/clients/s3/delete.ts b/packages/asset-uploader/src/clients/s3/delete.ts
deleted file mode 100644
index c8a7db6ddc..0000000000
--- a/packages/asset-uploader/src/clients/s3/delete.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { DeleteObjectCommand } from "@aws-sdk/client-s3";
-import { S3Env } from "../../schema";
-import { getS3Client } from "./client";
-
-export const deleteFromS3 = async (name: string) => {
- const s3Env = S3Env.parse(process.env);
- await getS3Client().send(
- new DeleteObjectCommand({
- Bucket: s3Env.S3_BUCKET,
- Key: name,
- })
- );
-};
diff --git a/packages/asset-uploader/src/clients/s3/s3.ts b/packages/asset-uploader/src/clients/s3/s3.ts
index 6982742be2..694a302a7c 100644
--- a/packages/asset-uploader/src/clients/s3/s3.ts
+++ b/packages/asset-uploader/src/clients/s3/s3.ts
@@ -1,15 +1,49 @@
+import { DeleteObjectCommand, S3Client } from "@aws-sdk/client-s3";
import type { AssetClient } from "../../client";
-import { deleteFromS3 } from "./delete";
import { uploadToS3 } from "./upload";
type S3ClientOptions = {
+ endpoint: string;
+ region: string;
+ accessKeyId: string;
+ secretAccessKey: string;
+ bucket: string;
+ acl?: string;
maxUploadSize: number;
};
-export const createS3Client = (clientOptions: S3ClientOptions): AssetClient => {
- const { maxUploadSize } = clientOptions;
+export const createS3Client = (options: S3ClientOptions): AssetClient => {
+ // @todo find a way to destroy this client to free resources
+ const client = new S3Client({
+ endpoint: options.endpoint,
+ region: options.region,
+ credentials: {
+ accessKeyId: options.accessKeyId,
+ secretAccessKey: options.secretAccessKey,
+ },
+ });
+
+ const uploadFile: AssetClient["uploadFile"] = async (request) => {
+ return await uploadToS3({
+ client,
+ request,
+ maxSize: options.maxUploadSize,
+ bucket: options.bucket,
+ acl: options.acl,
+ });
+ };
+
+ const deleteFile: AssetClient["deleteFile"] = async (name) => {
+ await client.send(
+ new DeleteObjectCommand({
+ Bucket: options.bucket,
+ Key: name,
+ })
+ );
+ };
+
return {
- uploadFile: (request) => uploadToS3({ request, maxSize: maxUploadSize }),
- deleteFile: deleteFromS3,
+ uploadFile,
+ deleteFile,
};
};
diff --git a/packages/asset-uploader/src/clients/s3/upload.ts b/packages/asset-uploader/src/clients/s3/upload.ts
index 0e8304a7f5..155ad5e73d 100644
--- a/packages/asset-uploader/src/clients/s3/upload.ts
+++ b/packages/asset-uploader/src/clients/s3/upload.ts
@@ -5,15 +5,13 @@ import {
unstable_composeUploadHandlers as unstableComposeUploadHandlers,
MaxPartSizeExceededError,
} from "@remix-run/node";
-import type { PutObjectCommandInput } from "@aws-sdk/client-s3";
+import type { PutObjectCommandInput, S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { Location } from "@webstudio-is/prisma-client";
-import { S3Env } from "../../schema";
import { toUint8Array } from "../../utils/to-uint8-array";
import { getAssetData, AssetData } from "../../utils/get-asset-data";
import { idsFormDataFieldName } from "../../schema";
import { getUniqueFilename } from "../../utils/get-unique-filename";
-import { getS3Client } from "./client";
import { sanitizeS3Key } from "../../utils/sanitize-s3-key";
import { uuidHandler } from "../../utils/uuid-handler";
@@ -29,13 +27,19 @@ const Ids = z.array(z.string().uuid());
const MAX_FILES_PER_REQUEST = 1;
export const uploadToS3 = async ({
+ client,
request,
maxSize,
+ bucket,
+ acl,
}: {
+ client: S3Client;
request: Request;
maxSize: number;
+ bucket: string;
+ acl?: string;
}): Promise<AssetData> => {
- const uploadHandler = createUploadHandler(MAX_FILES_PER_REQUEST);
+ const uploadHandler = createUploadHandler(MAX_FILES_PER_REQUEST, client);
const formData = await unstableCreateFileUploadHandler(
request,
@@ -44,6 +48,8 @@ export const uploadToS3 = async ({
uploadHandler({
file,
maxSize,
+ bucket,
+ acl,
}),
uuidHandler
)
@@ -62,15 +68,19 @@ export const uploadToS3 = async ({
return assetsData[0];
};
-const createUploadHandler = (maxFiles: number) => {
+const createUploadHandler = (maxFiles: number, client: S3Client) => {
let count = 0;
return async ({
file,
maxSize,
+ bucket,
+ acl,
}: {
file: UploadHandlerPart;
maxSize: number;
+ bucket: string;
+ acl?: string;
}): Promise<string | undefined> => {
if (file.filename === undefined) {
// Do not parse if it's not a file
@@ -103,14 +113,12 @@ const createUploadHandler = (maxFiles: number) => {
const uniqueFilename = getUniqueFilename(fileName);
- const s3Env = S3Env.parse(process.env);
-
// if there is no ACL passed we do not default since some providers do not support it
- const ACL = s3Env.S3_ACL ? { ACL: s3Env.S3_ACL } : {};
+ const ACL = acl ? { ACL: acl } : {};
const params: PutObjectCommandInput = {
...ACL,
- Bucket: s3Env.S3_BUCKET,
+ Bucket: bucket,
Key: uniqueFilename,
Body: data,
ContentType: file.contentType,
@@ -120,7 +128,7 @@ const createUploadHandler = (maxFiles: number) => {
},
};
- const upload = new Upload({ client: getS3Client(), params });
+ const upload = new Upload({ client, params });
AssetsUploadedSuccess.parse(await upload.done());