Skip to content

Commit

Permalink
Merge pull request #20 from vishalkondle45/feature/drive
Browse files Browse the repository at this point in the history
fix: bucket issue
  • Loading branch information
vishalkondle-dev authored Jun 30, 2024
2 parents 979fd49 + 60d6c74 commit 4bd7c0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
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

0 comments on commit 4bd7c0d

Please sign in to comment.