paint-brush
Active storage with Azure [A How-To Guide]by@Yosept
2,067 reads
2,067 reads

Active storage with Azure [A How-To Guide]

by JosephApril 5th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The actual step by step is under the title THE ACTUAL TUTORIAL (in this same article, don't go D:), you can skip there if you're in a rush, but I think there's some value in the process of finding the answer itself, and I don't know, this is my way to communicate things. I'm not gonna be real with you guys, but this is the data you need to share with you. The journey begins with a tutorial on how to use AWS with active storage.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Active storage with Azure [A How-To Guide]
Joseph HackerNoon profile picture

Before I start rambling about my adventures with Rails, I want to do something I'd appreciate more people doing, and hey, be the change you want in the world, so the actual step by step is gonna be under the title THE ACTUAL TUTORIAL (in this same article, don't go D:), you can skip there if you're in a rush, but I think there's some value in the process of finding the answer, other than the answer itself, and I don't know, this is my way to communicate things. We all good? Ok, here we go.

The challenge

So, recently I was working on a rails app (shameless plug) where I needed to add an image for every article in a database, and while checking the requirements I read this: 

You can store the image as a string, since uploading images to Heroku is tricky and not required for this project.

I immediately choose the responsible approach and took it as a challenge, I mean, How tricky could it possibly be?. Well, my friends, it was indeed, really tricky.

The journey begins

In my first try, I just uploaded my project to Heroku to see it fail, I wanted to see exactly what happened since most of the time you can learn a lot from the logs of an error, Does it break entirely? Does it reject the project on Heroku?. But it didn't, the app loads correctly, Heroku doesn't complain, and you can upload images, so What was going on? Did they just lie to me? That wasn't tricky at all. So I googled a little and got this article from Heroku themselves where they explain: 

... [files] will appear to work at first, [but] the attachments will exhibit seemingly strange behavior and eventually disappear.

And back at the app... oh... oh no...

Luckily, the lovely people at Heroku left a tutorial on how to use AWS with active storage, unluckily for me, I'm currently banned from AWS. So I followed a link to get other services with active storage and got 3 options, Google Cloud Storage(GCS), Microsoft Azure and.. AWS, so only 2 options.

The battle of the credit cards

GCS and Azure share a lot of things, they both have free tiers/trial periods, they are both own by giant tech companies, and they both require my credit card to get into the free tier because they are owned by giant tech companies, see the circle here?

Now that wouldn't be that much of a problem if at least they accepted my debit card, which they didn't, so It was time to google again, and then... when everything seemed grey, I found the Github Students Pack. Turns out, GitHub hooks you up with A LOT of freebies if you are studying, one of them being, you guessed it, a Microsoft Azure Students account.

The final struggle

Ok, now I have the keys to the kingdom, but What do I do with them?, I went back to the rails guides, and made my last mistake (of the day at least). I entered 

https://edgeguides.something
 instead of 
https://guides.something
.

Now, in my defense, they both look exactly the same if not for a little badge on the top right saying 'edge',  which in retrospective I should've seen waaaaay before, which resulted in hours of trials and errors, and me asking myself if I'm a real programmer, and thinking of maybe just uploading strings with the URL of the file, because, I mean, it was not required, and frankly, uploading images to Heroku can be tricky.

But in the end, it was our old friend, the old issue on GitHub with the same problem as you, which came to my rescue. Looks like in the Edge Guides, they recommend using "azure-storage-blob", but in the normal guides they say you should use "azure-storage", and when someone asked in the "-blob" repo How to use it in the active storage, the answer was pretty much, "Yeah, I don't know what you're talking about".

The actual tutorial

So here we are, we have all the pieces, and we are ready to add some actual code. Amazing.

The first thing you wanna do is enter you Azure account and go to Storage accounts, and create a new one

Now, I'm gonna be real with you guys, I don't know how much information I can share from inside my Azure account without being hacked or something like that, and since I'm literally in a site called Hackernoon... I'm not gonna risk it, but this is the data you need to change in this page, you can name both whatever you want, I choose greeks and herojourney, all of the other blued-out fields, I didn't change anything, and hit Review + create at the bottom left, then create again.

Then you're gonna have to wait for a little bit, until the page show you Your deployment is complete. Then click on go to the resource

Now you need to go first to the containers, add a new container

And finally, to you access keys, you want these one (the one covered in orange):

Finally, you want to add this configuration on your config/storage

azure:
service: AzureStorage
storage_account_name: <%= ENV['STORAGE_ACCOUNT_NAME'] %>
storage_access_key: <%= ENV['STORAGE_ACCESS_KEY'] %>
container: <%= ENV['STORAGE_CONTAINER'] %>

This in your .env (assuming you are using dotenv and don't want to share your keys online)

STORAGE_ACCOUNT_NAME=your_account_name
STORAGE_ACCESS_KEY=your_access_key
STORAGE_CONTAINER=your_storage_container

Add this gem to your Gemfile

gem 'azure-storage'

The results

In the end, and after a lot of struggle, I pushed the changes into my repo, and there they where in all their glory

A couple of things to note, as you may have guessed, Azure is a paid service so keep notice of your containers, and use the local active storage in development. If you have any question you can check the repo where the code for this app is hosted, or contact me on my socials, thank you for taking the time to read my article, and happy coding :D.

Last minute edit: I'm obviously not being paid by Azure, but hacker asked me before submitting, so I thought of making in completely clear.