-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from dotkom/feat/use-s3-for-file-transfer
Implement s3 integration
- Loading branch information
Showing
14 changed files
with
475 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> | ||
/// <reference types="next-images" /> | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
AWS_REGION?: string; | ||
AWS_ACCESS_KEY_ID?: string; | ||
AWS_SECRET_ACCESS_KEY?: string; | ||
AWS_S3_BUCKET_NAME?: string; | ||
LAMBDA_PRESIGN_UPLOAD_ENDPOINT?: string; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const LAMBDA_ENDPOINT = '/api/generate-receipt'; | ||
export const LAMBDA_PRESIGN_UPLOAD_ENDPOINT = '/api/presign-upload-url'; | ||
|
||
export const OW4_ADDRESS = process.env.NEXT_PUBLIC_OW4_ADDRESS || 'https://online.ntnu.no'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextApiRequest, NextApiResponse, PageConfig } from 'next'; | ||
|
||
import { SuccessBody } from 'lambda/handler'; | ||
import { sentryMiddleware } from 'lambda/sentry'; | ||
import { ErrorData } from 'lambda/errors'; | ||
import { getPresignedS3URL } from "../../utils/getPresignedS3URL"; | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse<SuccessBody | ErrorData>) => { | ||
const { filename, contentType } = req.body; | ||
|
||
try { | ||
const data = await getPresignedS3URL(filename, contentType); | ||
res.status(200).json({ message: "Presigned URL", data: JSON.stringify(data) }); | ||
} catch (error) { | ||
res.status(500).json({ message: "Failed to get presigned URL", data: error }); | ||
} | ||
}; | ||
|
||
export const config: PageConfig = { | ||
api: { | ||
bodyParser: { | ||
sizeLimit: '25mb', | ||
}, | ||
}, | ||
}; | ||
|
||
export default sentryMiddleware(handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const credentials = (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) ? | ||
{ | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
} : | ||
undefined; | ||
|
||
AWS.config.update({ | ||
region: process.env.AWS_REGION, | ||
credentials, | ||
}) | ||
|
||
export async function downloadFileFromS3Bucket(key: string): Promise<File> { | ||
if (!process.env.AWS_S3_BUCKET_NAME) { | ||
throw new Error('AWS_S3_BUCKET_NAME is not set'); | ||
} | ||
|
||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
}); | ||
|
||
const params = { | ||
Bucket: process.env.AWS_S3_BUCKET_NAME, | ||
Key: key, | ||
}; | ||
|
||
const data = await s3.getObject(params).promise(); | ||
|
||
return new File([data.Body as Blob], key, { type: data.ContentType }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const credentials = (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) ? | ||
{ | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
} : | ||
undefined; | ||
|
||
AWS.config.update({ | ||
region: process.env.AWS_REGION, | ||
credentials, | ||
}) | ||
|
||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
}); | ||
|
||
const createPresignedPost = (params: AWS.S3.PresignedPost.Params): Promise<AWS.S3.PresignedPost> => new Promise((resolve, reject) => { | ||
s3.createPresignedPost(params, function (err, data) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(data) | ||
} | ||
}); | ||
}); | ||
|
||
export const getPresignedS3URL = async (name: string, contentType: string): Promise<{ url: string, fields: {[key: string]: string}}> => { | ||
return await createPresignedPost({ | ||
Bucket: "receipt-form", | ||
Fields: { | ||
key: `uploads/${+new Date()}-${name}`, | ||
"Content-Type": contentType, | ||
}, | ||
Conditions: [ | ||
["content-length-range", 0, 1024 * 1024 * 10], | ||
], | ||
Expires: 60, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { LAMBDA_PRESIGN_UPLOAD_ENDPOINT } from "constants/backend"; | ||
|
||
export const uploadFile = async (file: File): Promise<string> => { | ||
if (!LAMBDA_PRESIGN_UPLOAD_ENDPOINT) { | ||
throw new Error('LAMBDA_PRESIGN_UPLOAD_ENDPOINT is not set'); | ||
} | ||
|
||
// Request to get the presigned POST data | ||
const response = await fetch(LAMBDA_PRESIGN_UPLOAD_ENDPOINT, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ filename: file.name, contentType: file.type }), | ||
}); | ||
|
||
const body = await response.json(); | ||
if (!response.ok) { | ||
console.error(await response.text()); | ||
throw new Error('Failed to get presigned post data'); | ||
} | ||
|
||
const { url, fields }: {url: string, fields: {[key: string]: string}} = JSON.parse(body.data); | ||
|
||
const formData = new FormData(); | ||
for (const key in fields) { | ||
formData.append(key, fields[key]); | ||
} | ||
formData.append('file', file); | ||
|
||
const uploadResponse = await fetch(url, { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
|
||
if (!uploadResponse.ok) { | ||
throw new Error('Failed to upload file'); | ||
} | ||
|
||
return fields.key | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const credentials = (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) ? | ||
{ | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, | ||
} : | ||
undefined; | ||
|
||
AWS.config.update({ | ||
region: process.env.AWS_REGION, | ||
credentials, | ||
}) | ||
|
||
// upload file to S3 bucket and make it publicly downloadable | ||
export async function uploadFileToS3Bucket(file: Uint8Array, key: string): Promise<string> { | ||
if (!process.env.AWS_S3_BUCKET_NAME) { | ||
throw new Error('AWS_S3_BUCKET_NAME is not set'); | ||
} | ||
const s3 = new AWS.S3({ | ||
apiVersion: '2006-03-01', | ||
params: { Bucket: process.env.AWS_S3_BUCKET_NAME }, | ||
}); | ||
|
||
const params = { | ||
Bucket: process.env.AWS_S3_BUCKET_NAME, | ||
Key: key, | ||
Body: file, | ||
ACL: 'public-read', | ||
}; | ||
|
||
const result = await s3.upload(params).promise(); | ||
|
||
return result.Location; | ||
} |
Oops, something went wrong.