Refactor client/server s3 dependencies
This commit is contained in:
parent
bc21de9e4d
commit
a1c6ef9b3f
@ -9,8 +9,8 @@
|
|||||||
"analyze": "ANALYZE=true next build"
|
"analyze": "ANALYZE=true next build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.461.0",
|
"@aws-sdk/client-s3": "3.462.0",
|
||||||
"@aws-sdk/s3-request-presigner": "^3.461.0",
|
"@aws-sdk/s3-request-presigner": "3.462.0",
|
||||||
"@next/bundle-analyzer": "14.0.3",
|
"@next/bundle-analyzer": "14.0.3",
|
||||||
"@tailwindcss/forms": "^0.5.7",
|
"@tailwindcss/forms": "^0.5.7",
|
||||||
"@testing-library/jest-dom": "^6.1.4",
|
"@testing-library/jest-dom": "^6.1.4",
|
||||||
|
|||||||
465
pnpm-lock.yaml
generated
465
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,9 @@
|
|||||||
import { auth } from '@/auth';
|
import { auth } from '@/auth';
|
||||||
import { awsS3GetSignedUploadUrl } from '@/services/blob/aws-s3';
|
import {
|
||||||
|
awsS3Client,
|
||||||
|
awsS3PutObjectCommandForKey,
|
||||||
|
} from '@/services/blob/aws-s3';
|
||||||
|
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||||
|
|
||||||
export const runtime = 'edge';
|
export const runtime = 'edge';
|
||||||
|
|
||||||
@ -9,7 +13,11 @@ export async function GET(
|
|||||||
) {
|
) {
|
||||||
const session = await auth();
|
const session = await auth();
|
||||||
if (session?.user && key) {
|
if (session?.user && key) {
|
||||||
const url = await awsS3GetSignedUploadUrl(key);
|
const url = await getSignedUrl(
|
||||||
|
awsS3Client(),
|
||||||
|
awsS3PutObjectCommandForKey(key),
|
||||||
|
{ expiresIn: 3600 }
|
||||||
|
);
|
||||||
return new Response(
|
return new Response(
|
||||||
url,
|
url,
|
||||||
{ headers: { 'content-type': 'text/plain' } },
|
{ headers: { 'content-type': 'text/plain' } },
|
||||||
|
|||||||
@ -6,25 +6,24 @@ import {
|
|||||||
ListObjectsCommand,
|
ListObjectsCommand,
|
||||||
PutObjectCommand,
|
PutObjectCommand,
|
||||||
} from '@aws-sdk/client-s3';
|
} from '@aws-sdk/client-s3';
|
||||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
||||||
|
|
||||||
const S3_BUCKET = process.env.NEXT_PUBLIC_AWS_S3_BUCKET ?? '';
|
const AWS_S3_BUCKET = process.env.NEXT_PUBLIC_AWS_S3_BUCKET ?? '';
|
||||||
const S3_REGION = process.env.NEXT_PUBLIC_AWS_S3_REGION ?? '';
|
const AWS_S3_REGION = process.env.NEXT_PUBLIC_AWS_S3_REGION ?? '';
|
||||||
const S3_ACCESS_KEY = process.env.AWS_S3_ACCESS_KEY ?? '';
|
const AWS_S3_ACCESS_KEY = process.env.AWS_S3_ACCESS_KEY ?? '';
|
||||||
const S3_SECRET_ACCESS_KEY = process.env.AWS_S3_SECRET_ACCESS_KEY ?? '';
|
const AWS_S3_SECRET_ACCESS_KEY = process.env.AWS_S3_SECRET_ACCESS_KEY ?? '';
|
||||||
|
|
||||||
const API_PATH_PRESIGNED_URL = '/api/aws-s3/presigned-url';
|
const API_PATH_PRESIGNED_URL = '/api/aws-s3/presigned-url';
|
||||||
|
|
||||||
const client = () => new S3Client({
|
export const awsS3Client = () => new S3Client({
|
||||||
region: S3_REGION,
|
region: AWS_S3_REGION,
|
||||||
credentials: {
|
credentials: {
|
||||||
accessKeyId: S3_ACCESS_KEY,
|
accessKeyId: AWS_S3_ACCESS_KEY,
|
||||||
secretAccessKey: S3_SECRET_ACCESS_KEY,
|
secretAccessKey: AWS_S3_SECRET_ACCESS_KEY,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const AWS_S3_BASE_URL =
|
export const AWS_S3_BASE_URL =
|
||||||
`https://${S3_BUCKET}.s3.${S3_REGION}.amazonaws.com`;
|
`https://${AWS_S3_BUCKET}.s3.${AWS_S3_REGION}.amazonaws.com`;
|
||||||
|
|
||||||
export const isUrlFromAwsS3 = (url: string) =>
|
export const isUrlFromAwsS3 = (url: string) =>
|
||||||
url.startsWith(AWS_S3_BASE_URL);
|
url.startsWith(AWS_S3_BASE_URL);
|
||||||
@ -33,15 +32,9 @@ const urlForKey = (key?: string) => `${AWS_S3_BASE_URL}/${key}`;
|
|||||||
|
|
||||||
const generateBlobId = () => generateNanoid(16);
|
const generateBlobId = () => generateNanoid(16);
|
||||||
|
|
||||||
// Runs on server
|
export const awsS3PutObjectCommandForKey = (Key: string) =>
|
||||||
export const awsS3GetSignedUploadUrl = async (Key: string) =>
|
new PutObjectCommand({ Bucket: AWS_S3_BUCKET, Key, ACL: 'public-read' });
|
||||||
getSignedUrl(
|
|
||||||
client(),
|
|
||||||
new PutObjectCommand({ Bucket: S3_BUCKET, Key, ACL: 'public-read' }),
|
|
||||||
{ expiresIn: 3600 }
|
|
||||||
);
|
|
||||||
|
|
||||||
// Runs on client
|
|
||||||
export const awsS3UploadFromClient = async (
|
export const awsS3UploadFromClient = async (
|
||||||
file: File | Blob,
|
file: File | Blob,
|
||||||
fileName: string,
|
fileName: string,
|
||||||
@ -69,8 +62,8 @@ export const awsS3Copy = async (
|
|||||||
const Key = addRandomSuffix
|
const Key = addRandomSuffix
|
||||||
? `${name}-${generateBlobId()}.${extension}`
|
? `${name}-${generateBlobId()}.${extension}`
|
||||||
: fileNameDestination;
|
: fileNameDestination;
|
||||||
return client().send(new CopyObjectCommand({
|
return awsS3Client().send(new CopyObjectCommand({
|
||||||
Bucket: S3_BUCKET,
|
Bucket: AWS_S3_BUCKET,
|
||||||
CopySource: fileNameSource,
|
CopySource: fileNameSource,
|
||||||
Key,
|
Key,
|
||||||
ACL: 'public-read',
|
ACL: 'public-read',
|
||||||
@ -79,15 +72,15 @@ export const awsS3Copy = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const awsS3Delete = async (Key: string) => {
|
export const awsS3Delete = async (Key: string) => {
|
||||||
client().send(new DeleteObjectCommand({
|
awsS3Client().send(new DeleteObjectCommand({
|
||||||
Bucket: S3_BUCKET,
|
Bucket: AWS_S3_BUCKET,
|
||||||
Key,
|
Key,
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const awsS3List = async (Prefix: string) =>
|
export const awsS3List = async (Prefix: string) =>
|
||||||
client().send(new ListObjectsCommand({
|
awsS3Client().send(new ListObjectsCommand({
|
||||||
Bucket: S3_BUCKET,
|
Bucket: AWS_S3_BUCKET,
|
||||||
Prefix,
|
Prefix,
|
||||||
}))
|
}))
|
||||||
.then((data) => data.Contents?.map(({ Key }) => urlForKey(Key)) ?? []);
|
.then((data) => data.Contents?.map(({ Key }) => urlForKey(Key)) ?? []);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user