<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gorka</title>
    <description>The latest articles on DEV Community by Gorka (@gorka).</description>
    <link>https://dev.to/gorka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917101%2Fa0df5533-9dba-403e-a7d5-c15ffe05c272.jpeg</url>
      <title>DEV Community: Gorka</title>
      <link>https://dev.to/gorka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gorka"/>
    <language>en</language>
    <item>
      <title>Configure Vultr CORS to accept direct uploads from Active Storage</title>
      <dc:creator>Gorka</dc:creator>
      <pubDate>Wed, 21 Sep 2022 23:20:25 +0000</pubDate>
      <link>https://dev.to/gorka/configure-vultr-cors-to-accept-direct-uploads-from-active-storage-23cc</link>
      <guid>https://dev.to/gorka/configure-vultr-cors-to-accept-direct-uploads-from-active-storage-23cc</guid>
      <description>&lt;p&gt;When I configured Rails' Active Storage to use Vultr Object Storage everything looked fine but when I tried to upload some images from Action Text I got red messages in the console. Something was not working ok.&lt;/p&gt;

&lt;p&gt;I went to Rails Guides, to Active Storage Overview and then to Direct Uploads. In section &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html#cross-origin-resource-sharing-cors-configuration"&gt;9.2 Cross-Origin Resource Sharing (CORS) configuration&lt;/a&gt; they offer some information on how to configure CORS for the main services but... where's Vultr?&lt;/p&gt;

&lt;p&gt;Turns out Vultr has an API that's compatible with a subset of the S3 API. Luckily they share CORS configuration. And they even have a &lt;a href="https://www.vultr.com/docs/how-to-apply-cors-policies-to-vultr-object-storage-buckets/"&gt;wiki page explaining how to configure it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm just writing this here in case the official information disappears from the internet.&lt;/p&gt;

&lt;p&gt;In order to configure this thing you can use a tool named &lt;a href="https://s3tools.org/s3cmd"&gt;s3cmd&lt;/a&gt;. It's available from &lt;a href="https://formulae.brew.sh/formula/s3cmd#default"&gt;brew.sh&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Before uploading the configuration to Vultr you have to configure the tool. Vultr has all the steps on &lt;a href="https://www.vultr.com/docs/how-to-use-s3cmd-with-vultr-object-storage"&gt;it's wiki&lt;/a&gt; but I'm going to copy here the basics.&lt;/p&gt;

&lt;p&gt;Run the configuration script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ s3cmd --configure
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ignore the region, and set &lt;code&gt;ewr1.vultrobjects.com&lt;/code&gt; as the S3 endpoint. The DNS-style template for accessing a bucket is &lt;code&gt;%(bucket)s.ewr1.vultrobjects.com&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now you have to create an xml file with the content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;CORSConfiguration&amp;gt;
&amp;lt;CORSRule&amp;gt;
    &amp;lt;ID&amp;gt;Get and put from/to Vultr Object Storage&amp;lt;/ID&amp;gt;
    &amp;lt;AllowedOrigin&amp;gt;http://example.com&amp;lt;/AllowedOrigin&amp;gt;
    &amp;lt;AllowedMethod&amp;gt;GET&amp;lt;/AllowedMethod&amp;gt;
    &amp;lt;AllowedMethod&amp;gt;PUT&amp;lt;/AllowedMethod&amp;gt;
    &amp;lt;AllowedMethod&amp;gt;HEAD&amp;lt;/AllowedMethod&amp;gt;
    &amp;lt;AllowedHeader&amp;gt;*&amp;lt;/AllowedHeader&amp;gt;
    &amp;lt;ExposeHeader&amp;gt;Origin&amp;lt;/ExposeHeader&amp;gt;
    &amp;lt;ExposeHeader&amp;gt;Content-Type&amp;lt;/ExposeHeader&amp;gt;
    &amp;lt;ExposeHeader&amp;gt;Content-MD5&amp;lt;/ExposeHeader&amp;gt;
    &amp;lt;ExposeHeader&amp;gt;Content-Disposition&amp;lt;/ExposeHeader&amp;gt;
    &amp;lt;MaxAgeSeconds&amp;gt;3600&amp;lt;/MaxAgeSeconds&amp;gt;
&amp;lt;/CORSRule&amp;gt;
&amp;lt;/CORSConfiguration&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add as many &lt;code&gt;AllowedOrigin&lt;/code&gt; as you need. Maybe with &lt;code&gt;https&lt;/code&gt;, &lt;code&gt;www&lt;/code&gt; subdomain, ...&lt;/p&gt;

&lt;p&gt;And then run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ s3cmd setcors RULES_FILE.xml s3://BUCKET_NAME
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should be enough to both upload and get images from the buckets.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>cors</category>
      <category>vultr</category>
      <category>s3</category>
    </item>
  </channel>
</rss>
