RD Pennell
Community Engineer at Sanity.io
RD is located at Richmond, CA
Fetch an image from a URL and upload to Sanity it via the JS Client
This schema is for an older version of Sanity Studio (v2), which is deprecated.
Learn how to migrate to the new Studio v3 →import client from 'part:@sanity/base/client';
import axios from 'axios';
const studioClient = client.withConfig({ apiVersion: '2021-10-21' });
const uploadImageFromUrl = async url => {
const imageBuffer = await axios
.get(url, { responseType: 'arraybuffer' })
.then(res => Buffer.from(res.data, 'utf-8'));
studioClient.assets
.upload('image', imageBuffer)
.then(img => console.log(`Image successfully uploaded: ${img.url}`))
.catch(err => console.log(err));
};
The client.assets.upload
method only accepts a File, Blob, Buffer, or Nodestream. Here's a method for fetching an image from a url, converting it to a Buffer, then uploading to your dataset.
Community Engineer at Sanity.io
Use the renderDefault method to show or hide fields.
Go to Conditionally show a field based off of a value inside of a referenceThis will allow you to display an array of references as a checklist from which you can multi-select.
Go to V3 version of Display an array of references as a checklistPopulate your list options in a string schema using an external API
Go to Asynchronous List Options ComponentUse the renderDefault function to easily control your available array options.
Go to Filter Array Options Based on the Current User Role