{"id":1921,"date":"2011-08-26T15:06:00","date_gmt":"2011-08-26T14:06:00","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1921"},"modified":"2021-08-24T13:27:48","modified_gmt":"2021-08-24T12:27:48","slug":"ravendb-attachments-or-blobs","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2011\/08\/ravendb-attachments-or-blobs\/","title":{"rendered":"RavenDB: Attachments (or Blobs)"},"content":{"rendered":"<p>Today we\u2019re looking at attachments in RavenDB. Attachments allow you to store binary data along your documents. So attachments are basically BLOBs in RavenDB.<\/p>\n<h2>Storing Binary Data in Documents<\/h2>\n<p>For simply storing some smaller binary data we can put it directly into a document. We just declare a byte array field and put stuff in it. The data will be stored and loaded with the document. RavenDB will store your binary data base64-encoded in the document.<br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=BlogPost.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">public class BlogPost\n{\n\tpublic BlogPost(string title)\n\t{\n\t\tTitle = title;\n\t}\n\n\tpublic string Id { get; set; }\n\tpublic string Title { get; set; }\n\tpublic byte[] Picture { get; set; }\n}<\/code><\/pre><\/noscript><br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=StoringBinary.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var post = new BlogPost(&quot;test&quot;)\n    {\n        Picture = File.ReadAllBytes(@&quot;C:\\Users\\Gamlor\\Desktop\\thumb1.png&quot;)        \n    };\nsession.Store(post);<\/code><\/pre><\/noscript><br \/>\nHowever we need to keep in mind that we always load that binary data together with the document. For larger data that can take its time. Therefore we might want to consider to put the larger data chunks in a separate document, so that we can load that in on demand.<\/p>\n<div id=\"attachment_1923\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/blob-in-document.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1923\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/blob-in-document-300x135.png\" alt=\"Blob in Document\" title=\"blob-in-document\" class=\"size-medium wp-image-1923\" width=\"300\" height=\"135\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/blob-in-document-300x135.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/blob-in-document.png 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1923\" class=\"wp-caption-text\">Blob in Document<\/p><\/div>\n<h2>Attachments<\/h2>\n<p>As alternative we can store binary data in attachments. Attachments are completely decoupled from the regular documents. They don\u2019t participate in transactions and can be updated and changed independently from documents. Like this:<br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=StoreAttachement.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var dbCommands = session.Advanced.DatabaseCommands;\n\/\/ We can freely choose an id.\n\/\/ I've decided to use the document id to which the attachement belongs to\nvar optionalMetaData = new RavenJObject();\noptionalMetaData[&quot;Format&quot;] = &quot;PNG&quot;;\ndbCommands.PutAttachment(post.Id, Guid.NewGuid(),\n\tFile.ReadAllBytes(@&quot;C:\\Users\\Gamlor\\Desktop\\thumb1.png&quot;), optionalMetaData);<\/code><\/pre><\/noscript><\/p>\n<p>The GUID we pass is the Etag used for caching. As long as this Etag is the same the client can get the attachment from its cache. When we replace an attachment but use the same Etag the client can use the cached value. Otherwise it needs to get the new attachment. However caching is a topic for another time.<\/p>\n<p>We can get the attachment via id:<br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=RetrieveAttachement.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var dbCommands = session.Advanced.DatabaseCommands;\n\/\/ Now let's get the picture by its id\nvar attachement = dbCommands.GetAttachment(post.Id);\nvar thePicture = attachement.Data;\nvar format = attachement.Metadata[&quot;Format&quot;];\n\nFile.WriteAllBytes(@&quot;C:\\Users\\Gamlor\\Desktop\\tmp.png&quot;, thePicture);<\/code><\/pre><\/noscript><\/p>\n<p>To update an attachment we simple use the PutAttachment command again:<br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=UpdateAttachement.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">\/\/ To update a document just use the put command again\ndbCommands.PutAttachment(&quot;blogposts\/3073&quot;, Guid.NewGuid(),\n    File.ReadAllBytes(@&quot;C:\\Users\\Gamlor\\Desktop\\newerImage.png&quot;),\n    new RavenJObject());<\/code><\/pre><\/noscript><\/p>\n<p>And to delete it we can use the delete command.<br \/>\n<script src=\"https:\/\/gist.github.com\/1173399.js?file=DeleteAttachement.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">dbCommands.DeleteAttachment(post.Id, null);<\/code><\/pre><\/noscript><\/p>\n<div id=\"attachment_1924\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/seperate-attachments.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1924\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/seperate-attachments-300x172.png\" alt=\"RavenDB Attachments\" title=\"seperate-attachments\" class=\"size-medium wp-image-1924\" width=\"300\" height=\"172\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/seperate-attachments-300x172.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/08\/seperate-attachments.png 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1924\" class=\"wp-caption-text\">RavenDB Attachments<\/p><\/div>\n<h3>Update: Use of Attachements for Storing Blobs<\/h3>\n<p><a href=\"http:\/\/twitter.com\/#!\/synhershko\">@synhershko<\/a> on Twitter pointed out that out that <a href=\"https:\/\/github.com\/ravendb\/docs\/blob\/master\/docs\/consumer\/attachments.markdown\">the use of attachments is discouraged<\/a> when blobs can be put somewhere else. He&#8217;s certainly right. However in my opinion it is still handy to store blobs in attachments, when RavenDB is already up and running and does the job decent enough.<\/p>\n<h2>Conclusion<\/h2>\n<p>To store binary data we either can store it directly in document or use RavenDB\u2019s attachments. That\u2019s if for today. Next time we look at cascading operations support in RavenDB.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we\u2019re looking at attachments in RavenDB. Attachments allow you to store binary data along your documents. So attachments are basically BLOBs in RavenDB. Storing Binary Data in Documents For&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[187],"tags":[21,296],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1921"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1921"}],"version-history":[{"count":12,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1921\/revisions"}],"predecessor-version":[{"id":3906,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1921\/revisions\/3906"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}