-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added bucket functionality but running into server errors
- Loading branch information
1 parent
e231755
commit 730abd2
Showing
6 changed files
with
7,910 additions
and
5,575 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
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,51 @@ | ||
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { Bucket } from "sst/node/bucket"; | ||
|
||
/* const s3Client = new S3Client({ region: process.env.AWS_REGION }); */ | ||
const s3Client = new S3Client({}); | ||
|
||
export const handler = async (event) => { | ||
console.log('HERERERERERERE') | ||
let body; | ||
try { | ||
body = JSON.parse(event.body); | ||
} catch (error) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message: "Invalid request body" }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
} | ||
|
||
const { name, contentType } = body; | ||
|
||
if (!name || !contentType) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ message: "Missing 'name' or 'contentType' in request body" }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
} | ||
|
||
const key = `uploads/${name}-${Date.now()}.${contentType.split('/')[1]}`; | ||
const command = new PutObjectCommand({ | ||
Bucket: Bucket.myFiles.bucketName, | ||
Key: key, | ||
ContentType: contentType, | ||
}); | ||
|
||
const url = await getSignedUrl(s3Client, command, { expiresIn: 3600 }); // 1 hour expiration | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ url, key }), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
}; |
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
Oops, something went wrong.