Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bucket issue #20

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions app/api/drive/files/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import File from '@/models/File';
import { authOptions } from '../../auth/[...nextauth]/authOptions';
import { UserDataTypes } from '../../auth/[...nextauth]/next-auth.interfaces';

const Bucket = process.env.AWS_BUCKET_NAME as string;

const s3Client = new S3Client({
region: process.env.AWS_REGION as string,
credentials: {
Expand Down Expand Up @@ -71,12 +69,14 @@ export async function PUT(req: NextRequest) {
let link = null;
if (fl?.link) {
const input = {
Bucket,
CopySource: `${Bucket}/${fl.link}`,
Bucket: 'dream-by-vishal',
CopySource: `dream-by-vishal/${fl.link}`,
Key: encodeURI(`${body?.parent}/${fl?.name}`),
};
await s3Client.send(new CopyObjectCommand(input));
await s3Client.send(new DeleteObjectCommand({ Bucket, Key: encodeURI(fl.link) }));
await s3Client.send(
new DeleteObjectCommand({ Bucket: 'dream-by-vishal', Key: encodeURI(fl.link) })
);
link = encodeURI(`${body?.parent}/${fl?.name}`);
}
const input = {
Expand All @@ -96,7 +96,10 @@ export async function PUT(req: NextRequest) {
const deleteAllChilds = async (fl: any) => {
if (fl?.link) {
await s3Client.send(
new DeleteObjectCommand({ Bucket, Key: encodeURI(`${fl?.parent}/${fl?.name}`) })
new DeleteObjectCommand({
Bucket: 'dream-by-vishal',
Key: encodeURI(`${fl?.parent}/${fl?.name}`),
})
);
}
await fl?.deleteOne();
Expand Down
6 changes: 2 additions & 4 deletions app/api/drive/files/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import File from '@/models/File';
export const maxDuration = 60;
export const dynamic = 'force-dynamic';

const Bucket = process.env.AWS_BUCKET_NAME as string;

const s3Client = new S3Client({
region: process.env.AWS_REGION as string,
credentials: {
Expand All @@ -36,7 +34,7 @@ export async function POST(req: Request) {
files.map(async (file) => {
const Body = (await file.arrayBuffer()) as Buffer;
const Key = encodeURI(parent ? `${parent}/${file.name}` : file.name);
await s3Client.send(new PutObjectCommand({ Bucket, Key, Body }));
await s3Client.send(new PutObjectCommand({ Bucket: 'dream-by-vishal', Key, Body }));
await File.create({
user: session?.user._id,
parent,
Expand All @@ -61,7 +59,7 @@ export async function GET(req: NextRequest) {
}
await startDb();
const Key = req.nextUrl.searchParams.get('Key') || '';
const command = new GetObjectCommand({ Bucket, Key });
const command = new GetObjectCommand({ Bucket: 'dream-by-vishal', Key });
const src = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
return NextResponse.json(src, { status: 200 });
} catch (error: any) {
Expand Down
2 changes: 1 addition & 1 deletion app/api/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function uploadImageToS3(file: Buffer, name: string) {
Key: name,
Body: fileBuffer,
ACL: ObjectCannedACL.public_read,
Bucket: process.env.AWS_BUCKET_NAME,
Bucket: 'dream-by-vishal',
ContentType: 'image/*',
};
const command = new PutObjectCommand(params);
Expand Down
Loading