👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Uploading same image multiple times in Sanity and getting different asset IDs.

13 replies
Last updated: Jun 7, 2022
Hello,I’m trying to upload assets via a node server. The problem is, if I upload same exact image, it doesn’t get uploaded, instead the old one gets replaced. How can I upload same image multiple times and have different id’s for each?


const imageAsset = await sanity*.*assets*.*upload('image', req*.*file*.*buffer, {

filename: 'avatar_' + res*.*locals*.*user*.*_id + '.jpg',

});
Jun 7, 2022, 9:10 AM
Append a timestamp or random number at the end
Jun 7, 2022, 9:12 AM
To filename? Filename changes every time. But if image buffer is exactly the same, it gets replaced.
Jun 7, 2022, 9:17 AM
if you add something like a timestamp to the file path, would it not be treated as a new image every time?
Jun 7, 2022, 9:20 AM
There is only the
filename
option which I update each time. I only attach the file buffer, which is exactly the same each time
Jun 7, 2022, 9:23 AM
If I do it from Sanity Studio, it works correctly
Jun 7, 2022, 9:23 AM
filename: 'avatar_'+res.locals.user._id+'_'+Date.now()+'.jpg
for example would that create a new file on each upload ?
Jun 7, 2022, 9:23 AM
Yes, just tried, it replaces old image with new name
Jun 7, 2022, 9:24 AM
I would of thought that would of made a new image entirely
Jun 7, 2022, 9:25 AM
I tried via Sanity Studio and I get this error:
An error occured: Asset already exists
Jun 7, 2022, 9:25 AM
Yea, me too, I tried adding multiple attributes to it too, like title, description, etc. But it’s same
Jun 7, 2022, 9:26 AM
I managed to upload two of the same images, one from server, one using Sanity Studio. But I cannot upload same image twice from same source. Either gets replaced, even if it has different name, or I get error:
An error occured: Asset already exists
Jun 7, 2022, 9:28 AM
The Studio actively prevents you from uploading the same image twice. From a structured content standpoint, it's better practice to upload a single high quality image and then use the Image URL API to request any variations on size and quality you may need.
What's your motivation for having multiple copies of the same image?
Jun 7, 2022, 6:21 PM
I agree, it doesn’t make sense to have same image twice.
Here’s my flow:
• I’m storing each user’s avatar at 300x300px on Sanity
• After upload, I’m storing a reference id to user’s document
• If user deletes his avatar, i’m getting their avatar id and delete it from Sanity
• If user B uploads same exact image as their avatar, they get same asset id as user A
• Now if they want to delete their avatar, I get an error, the image cannot be deleted because it’s referenced in another document.
This is a super rare case where two users have exact same image as their avatar and I wanted to just have different asset id’s for their own avatar.

I managed to make the backend not delete the image if it’s still referenced somewhere, so it’s all good.

Btw, studio lets you upload same image but with different resolutions, it has to have different buffer.

Thanks for the prompt support btw
🙌
Jun 7, 2022, 6:44 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?