Skip to content

Commit

Permalink
wip [skip-pizza]
Browse files Browse the repository at this point in the history
  • Loading branch information
jamdelion committed Nov 20, 2024
1 parent d37679c commit 270eaaf
Show file tree
Hide file tree
Showing 6 changed files with 1,152 additions and 14 deletions.
5 changes: 2 additions & 3 deletions api.planx.uk/modules/file/service/deleteFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ export const deleteFilesByURL = async (
fileURLs: string[],
): Promise<string[]> => {
const keys = fileURLs.map(getS3KeyFromURL);
const result = await deleteFilesByKey(keys);
return result;
return await deleteFilesByKey(keys);
};

export const deleteFilesByKey = async (keys: string[]): Promise<string[]> => {
const s3 = s3Factory();
const params = getDeleteFilesParams(keys);
try {
await s3.deleteObjects(params).promise();
s3.deleteObjects(params);
return keys;
} catch (error) {
throw Error(`Failed to delete S3 files: ${error}`);
Expand Down
6 changes: 3 additions & 3 deletions api.planx.uk/modules/file/service/getFile.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type S3 from "aws-sdk/clients/s3.js";
import type { PutObjectCommandInput } from "@aws-sdk/client-s3";
import { s3Factory } from "./utils.js";

export const getFileFromS3 = async (fileId: string) => {
const s3 = s3Factory();

const params = {
Key: fileId,
} as S3.PutObjectRequest;
} as PutObjectCommandInput;

const file = await s3.getObject(params).promise();
const file = await s3.getObject(params);

return {
body: file.Body as Buffer,
Expand Down
10 changes: 5 additions & 5 deletions api.planx.uk/modules/file/service/uploadFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type S3 from "aws-sdk/clients/s3.js";
import type { PutObjectCommandInput } from "@aws-sdk/client-s3";
import { customAlphabet } from "nanoid";
import mime from "mime";
import { s3Factory } from "./utils.js";
Expand All @@ -14,7 +14,7 @@ export const uploadPublicFile = async (

const { params, key, fileType } = generateFileParams(file, filename, filekey);

await s3.putObject(params).promise();
await s3.putObject(params);
const fileUrl = buildFileUrl(key, "public");

return {
Expand All @@ -36,7 +36,7 @@ export const uploadPrivateFile = async (
is_private: "true",
};

await s3.putObject(params).promise();
await s3.putObject(params);
const fileUrl = buildFileUrl(key, "private");

return {
Expand All @@ -63,7 +63,7 @@ export function generateFileParams(
filename: string,
filekey?: string,
): {
params: S3.PutObjectRequest;
params: PutObjectCommandInput;
fileType: string | null;
key: string;
} {
Expand All @@ -76,7 +76,7 @@ export function generateFileParams(
Body: file?.buffer || JSON.stringify(file),
ContentDisposition: `inline;filename="${filename}"`,
ContentType: file?.mimetype || "application/json",
} as S3.PutObjectRequest;
} as PutObjectCommandInput;

return {
fileType,
Expand Down
13 changes: 10 additions & 3 deletions api.planx.uk/modules/file/service/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import S3 from "aws-sdk/clients/s3.js";
import { S3 } from "@aws-sdk/client-s3";
import { isLiveEnv } from "../../../helpers.js";

export function s3Factory() {
return new S3({
// The key params is no longer supported in v3, and can be removed.
// @deprecated The object needs to be passed to individual operations where it's intended.
params: { Bucket: process.env.AWS_S3_BUCKET },

region: process.env.AWS_S3_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,

credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
},

...useMinio(),
});
}
Expand Down
1 change: 1 addition & 0 deletions api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@airbrake/node": "^2.1.8",
"@aws-sdk/client-s3": "^3.696.0",
"@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#ccf9ac3",
"@types/isomorphic-fetch": "^0.0.36",
"adm-zip": "^0.5.10",
Expand Down
Loading

0 comments on commit 270eaaf

Please sign in to comment.