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