Created
April 25, 2021 17:51
-
-
Save jeffpohlmeyer/d9824920fc1eb522ceff6380edb9de80 to your computer and use it in GitHub Desktop.
Trying to upload image to Sanity from URL
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
require('dotenv').config() | |
const process = require('process') | |
const axios = require('axios') | |
const sanityClient = require('@sanity/client') | |
const client = sanityClient({ | |
projectId: process.env.SANITY_PROJECT_ID, | |
dataset: process.env.SANITY_DATASET, | |
token: process.env.SANITY_TOKEN, | |
useCdn: false, | |
}) | |
const handler = async (event) => { | |
if (!event.httpMethod === 'POST') { | |
return { | |
statusCode: 400, | |
body: 'unrecognized HTTP Method, only POST allowed', | |
} | |
} | |
const image = await axios.get( | |
'https://scontent-lga3-1.cdninstagram.com/v/t51.2885-15/e35/p1080x1080/175784167_504506980979184_5123965832585915301_n.jpg?tp=1&_nc_ht=scontent-lga3-1.cdninstagram.com&_nc_cat=1&_nc_ohc=_eLidWAYe0UAX9H4ZKh&edm=AABBvjUBAAAA&ccb=7-4&oh=915057b5d7f98878038f2a6f6a5e34d8&oe=60AC27C4&_nc_sid=83d603', | |
{ responseType: 'arraybuffer' } | |
) | |
const data = Buffer.from(image.data, 'binary').toString('base64') | |
client.assets | |
.upload('image', data) | |
.then(() => { | |
console.log('Done!') | |
}) | |
.catch((err) => { | |
console.log('err', err) | |
return { statusCode: 400 } | |
}) | |
return { statusCode: 204, headers: { 'Content-Type': 'application/json' } } | |
} | |
module.exports = { handler } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment