Skip to content

Commit

Permalink
FS-4598: updating aws sdk into v3 for s3 service (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwan-samarasinghe authored Aug 21, 2024
1 parent 6f0e75b commit 4c3093d
Show file tree
Hide file tree
Showing 3 changed files with 1,424 additions and 40 deletions.
5 changes: 4 additions & 1 deletion runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
"license": "SEE LICENSE IN LICENSE",
"description": "Digital forms runner adapter",
"dependencies": {
"@aws-sdk/client-s3": "3.633.0",
"@aws-sdk/lib-storage": "3.633.0",
"@aws-sdk/s3-request-presigner": "3.633.0",
"dropzone": "5.9.3",
"joi": "17.13.3",
"tinymce": "^7.3.0"
"tinymce": "7.3.0"
},
"devDependencies": {
"@babel/cli": "^7.23.3",
Expand Down
78 changes: 46 additions & 32 deletions runner/src/server/services/S3UploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import {config} from "../plugins/utils/AdapterConfigurationSchema";
// @ts-ignore
import http from "http";
import {get} from "../../../../digital-form-builder/runner/src/server/services/httpService";

import S3 = require("aws-sdk/clients/s3");
import {
DeleteObjectCommand,
GetObjectCommand,
ListObjectsCommand,
PutObjectCommand,
S3Client
} from "@aws-sdk/client-s3";
import {Upload} from "@aws-sdk/lib-storage";
import {getSignedUrl} from "@aws-sdk/s3-request-presigner";

type Payload = HapiRequest["payload"];

Expand All @@ -25,7 +32,7 @@ if (endpointUrl) {
//@ts-ignore
awsConfig.signatureVersion = process.env.AWS_SIGNATURE_VERSION || "v4";
}
const s3 = new S3(awsConfig);
const s3 = new S3Client(awsConfig);

const parsedError = (key: string, error?: string) => {
return {
Expand Down Expand Up @@ -105,7 +112,7 @@ export class S3UploadService {
await this.uploadFilesS3(locations, prefix, metadata).then((result) => {
result.forEach((doc) => {
if (typeof doc.error !== "undefined") {
error = "Failed to upload file to server: " + doc.error;
error = "Failed to upload file to server:" + doc.error;
} else {
location = `${prefix}/${locations[0].hapi.filename}`;
}
Expand Down Expand Up @@ -357,10 +364,11 @@ export class S3UploadService {
ContentType: file.hapi.headers["content-type"],
Metadata: metadata,
};

await s3
.upload(uploadParams)
.promise()
const command = new Upload({
client: s3,
params: uploadParams
})
await command.done()
.then(function (data) {
response.push({location: data.Location, error: undefined});
})
Expand All @@ -383,55 +391,61 @@ export class S3UploadService {
folderPath: string,
formSessionId: string
): Promise<S3Object[]> {
let response = new Array();
const params = {
Bucket: bucketName,
Prefix: `${folderPath}/`,
};

const response = await s3.listObjectsV2(params).promise();

if (!response.Contents || response.Contents.length === 0) {
return [];
}
//@ts-ignore
const files = response.Contents.filter((obj) => !obj.Key.endsWith("/")).map(
(obj) => ({
FormSessionId: formSessionId,
Key: obj.Key!.replace(`${folderPath}/`, ""),
Size: obj.Size!,
})
);

return files;
const command = new ListObjectsCommand(params);
await s3.send(command).then(function (result) {
if (!result.Contents || result.Contents.length === 0) {
response = [];
} else {
// @ts-ignore
response = result.Contents.filter((obj) => !obj.Key.endsWith("/")).map(
(obj) => ({
FormSessionId: formSessionId,
Key: obj.Key!.replace(`${folderPath}/`, ""),
Size: obj.Size!,
})
);
}
}).catch((err) => {
response.push({
location: undefined,
error: `${err.code}: ${err.message}`,
});
this.logger.error(`Cannot get the list of files`, err);
});
return response;
}

async getFileDownloadUrlS3(key: string) {
const params = {
Bucket: bucketName,
Key: key,
};
const url = s3.getSignedUrl("getObject", params);
return url;
const command = new GetObjectCommand(params);
return getSignedUrl(s3, command);
}

async getPreSignedUrlS3(key: string) {
const params = {
Bucket: bucketName,
Key: key,
Expires: 60 * 60,
Key: key
};

return await s3.getSignedUrlPromise("putObject", params);
const command = new PutObjectCommand(params);
return getSignedUrl(s3, command, {expiresIn: 60 * 60});
}

async deleteFileS3(key: string) {
const params = {
Bucket: bucketName,
Key: key,
};

try {
await s3.deleteObject(params).promise();
const command = new DeleteObjectCommand(params);
await s3.send(command);
return true;
} catch (err) {
console.error(`Issue when deleting file with key: ${key}`);
Expand Down
Loading

0 comments on commit 4c3093d

Please sign in to comment.