Take the 2-minute tour ×

I'm writing cms like application and I would like my images to be stored as attachments in couchdb. The problem is in naming the attachments because I don't want my images to be named the same (e.g. /db/doc_id/thumb.jpg)

Ideally attachments names should depend on doc.name field. To make this work I would have to rename attachment each time user changed the name (description|alt) of current photo document.

So my question is: how to change attachment name? or maybe I should go other way in solving my problem?

share|improve this question

2 Answers 2

up vote 2 down vote accepted


I'd suggest considering using UUIDs for the document names, and storing the attachment as something "static" like "original", "thumb", "300wide", etc. The name given by the user when they upload the file can be stored as a key, and you can use a MapReduce index to retrieve the image/file using that name later.

If you go that route, though, you'll have to come at the "duplicates" problem a bit differently--as you could easily upload the same image multiple times with the same user-provided name and there would be no conflict.

Depending on what you're building, though, making the user provide a unique name is generally unwise--Flickr (among many others) doesn't, for instance.

If you really do need to make the doc_id == the name given by the user, then it would still be wise to store the attachments under static names, so you don't have to update the attachment name.

Lastly, if you feel you really must change the attachment name (and there are certainly cases where you need to), the simplest way is to GET the attachment from the old location (or with the document), PUT it as the new name, and DELETE the attachment with the old name.

Hope that helps!

share|improve this answer
Doesn't the GET/PUT/DELETE sequence require the attachment's mime-type for the PUT? How would I get that? (The only way I know is to get the document and look at the properties of the attachment stub in the _attachments field, but this seems like too much overhead if there are a lot of attachments.) –  Mitchell Model Feb 27 '13 at 18:43
If you know the name of the attachment, you could use a HEAD request to get its mime-type. If you don't, you could use MapReduce to build an index of all the _attachments and their mime-types. Then use the results of that as the basis for your GET/PUT/DELETE runs. –  BigBlueHat Mar 6 '13 at 13:49

Use this command >

curl -v http://localhost:5984/database/DocumentID/OldFileName?rev=RevisionID -X MOVE -H "Destination: NewFileName"
share|improve this answer
MOVE was only briefly supported by CouchDB and removed before 0.9 was released: wiki.apache.org/couchdb/HTTP_Document_API#MOVE –  BigBlueHat Jul 2 '12 at 16:03

Your Answer


