Skip to content

Instantly share code, notes, and snippets.

@statico
Created April 17, 2023 19:33
Show Gist options
  • Save statico/9a746d404c7379b42e59daf034925523 to your computer and use it in GitHub Desktop.
Save statico/9a746d404c7379b42e59daf034925523 to your computer and use it in GitHub Desktop.
sanity blog backup to lambda (not working because of --raw)
/*global fetch*/
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
const client = new S3Client({});
export const handler = async (event) => {
const projectId = process.env.SANITY_PROJECT_ID
const dataset = process.env.SANITY_DATASET
const token = process.env.SANITY_TOKEN
const bucket = process.env.S3_BUCKET
const date = new Date().toISOString()
// https://www.sanity.io/docs/export
const url = `https://${projectId}.api.sanity.io/v2021-06-07/data/export/${dataset}`
const headers = {
Authorization: `Bearer ${token}`
}
const res = await fetch(url, { headers })
const data = await res.text()
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_PutObject_section.html
const command = new PutObjectCommand({
Bucket: bucket,
Key: `sanity-${projectId}-${dataset}-${date}.ndjson`,
Body: data
})
await client.send(command)
const response = { statusCode: 200, body: 'ok' }
}
@statico
Copy link
Author

statico commented Apr 17, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment