Active Storage, specify a Google bucket directory ? - ruby-on-rails

I've been implementing an Active Storage Google strategy on Rails 5.2, at the moment I am able to upload files using the rails console without problems, the only thing I am missing is if there is a way to specify a directory inside a bucket. Right now I am uploading as follows
bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")
The configuration on my storage.yml
google:
service: GCS
project: my-project
credentials: <%= Rails.root.join("config/myfile.json") %>
bucket: bucketname
But in my bucket there are different directories such as bucketname/department1 and such. I been through the documentation and have not found a way to specify further directories and all my uploads end up in bucket name.

Sorry, I’m afraid Active Storage doesn’t support that. You’re intended to configure Active Storage with a bucket it can use exclusively.

Maybe you can try metaprogramming, something like this:
Create config/initializers/active_storage_service.rb to add set_bucket method to ActiveStorage::Service
module Methods
def set_bucket(bucket_name)
# update config bucket
config[:bucket] = bucket_name
# update current bucket
#bucket = client.bucket(bucket_name, skip_lookup: true)
end
end
ActiveStorage::Service.class_eval { include Methods }
Update your bucket before uploading or downloading files
ActiveStorage::Blob.service.set_bucket "my_bucket_name"
bk.file.attach(io: File.open(bk.source_dir.to_s), filename: "file.tar.gz", content_type: "application/x-tar")

Related

Rails Active Storage & AWS S3 : How to attach image to model through seeds.rb and then store it in S3 private bucket?

For a school project, I'm working on a Rails app which "sells" pics of kittens. I picked 10 pictures of cats online, they are currently on my computer. I'm using Postgresql for the DB. I have a class/model Item which represents the kitten photos.
What I'm looking for is a way to, when generating fake data through seeds.rb loops, attaching a kitten photo to each Item class/model, which will be then stored to an AWS S3 bucket that is already created (it's called catz-temple). I have my two access and secret S3 keys on a .env file, I already have modified my storage.yml file like so :
amazon:
service: S3
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
region: eu-central-1
bucket: catz-temple
I found out there was a gem called aws-sdk-ruby, but I just can't find out what approach I should have on this topic.
For now, I just put my bucket in public access and take each bucket photos' urls, but there's no API and secure approach into this...
Thank you all
Starting by follow the guides for configuring ActiveStorage and S3. Then setup the attachments on your model.
class Kitteh < ApplicationRecord
has_one_attached :photo
end
With ActiveStorage you can directly attach files to records by passing an IO object:
photos = Rails.root.join('path/to/the/images', '*.{jpg,gif,png}')
100.times do |n|
path = photos.sample
File.open(path) do |file|
Kitteh.new(name: "Kitteh #{n}") do |k|
k.photo.attach(
io: file,
filename: path.basename
)
end.save!
end
end
This example creates 100 records with a random image selected from a directory on your hard drive and will upload it to the storage you have configured.

Set ActiveStorage disk service upload paths

Does anyone know how to change the disk_service.rb to specify upload paths, for example:
my_path = Rails.root.join("public", "websites", "example.com", "users", current_user.id, "avatar")
current_user.avatar.attach(file, my_path)
This would result in having the file uploaded here:
/public/websites/example.com/users/12345/avatar/blah.png
And then I'd be able to:
rails_representation_path( current_user.avatar.variant(resize: "100x100"), disposition: 'attachment')
and get back the path:
/websites/example.com/users/12345/avatar/blah-100x100.png
This will allow getting rid of many issues around the ActiveStorage public/private URLs and related to CDN caching.
I am playing with https://github.com/rails/rails/blob/master/activestorage/lib/active_storage/service/disk_service.rb but can't figure out much of what it does and how it works really.
In your config/storage.yml you should have a configuration block for the local storage service, you can change where the attachments are stored by specifing a different path for root.
local:
service: Disk
root: <%= Rails.root.join('storage') %>
I don't think that you'll be able to address the specific images in the manner you are describing. ActiveRecord creates a unique key for everything that is attached to a model and renames the files with that key.

Is it possible to specify a custom CDN for ActiveStorage?

I am running a Ruby on Rails website and am currently using Rails' ActiveStorage to store my images and videos.
I am using a AWS based space for storage (DigitalOcean) and they recently rolled out support for custom CDN support. Meaning, instead of referencing my-space.nyc3.digitalocean.com, I would reference assets.akinyele.ca.
Everything has been setup on my DigicalOcean dashboard. But I was wondering if I could use assets.akinyele.ca on ActiveStorage instead.
I have tried not specifying a bucket that failed automatically because it looks like the ActiveStorage API requires that field, and uses it to build the space storage service's URL. I also tried specifying the endpoint to assets.akinyele.ca, but that gave me my-space.assets.akinyele.ca.
This is what a part of config looks like:
# config/storage.yml
local: #
development: #
# This is what I need to replace, and this is was I am using right now.
amazon:
service: S3
access_key_id: <%= ENV["TANOSHIMU_SPACE_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["TANOSHIMU_SPACE_SECRET_ACCESS_KEY"] %>
region: nyc3
bucket: my space
endpoint: 'https://nyc3.digitaloceanspaces.com'
Thank you.
You can try to override url method for ActiveStorage::Service::S3Service
P.S. Use bucket: '' in your config/storage.yml

Rails Active Storage set folder to store files

I'm using Active Storage to store files in a Rails 5.2 project. I've got files saving to S3, but they save with random string filenames and directly to the root of the bucket. I don't mind the random filenames (I actually prefer it for my use case) but would like to keep different attachments organized into folders in the bucket.
My model uses has_one_attached :file. I would like to specify to store all these files within a /downloads folder within S3 for example. I can't find any documentation regarding how to set these paths.
Something like has_one_attached :file, folder: '/downloads' would be great if that's possible...
The ultimate solution is to add an initializer. You can add a prefix based on an environment variable or your Rails.env :
# config/initializer/active_storage.rb
Rails.configuration.to_prepare do
ActiveStorage::Blob.class_eval do
before_create :generate_key_with_prefix
def generate_key_with_prefix
self.key = if prefix
File.join prefix, self.class.generate_unique_secure_token
else
self.class.generate_unique_secure_token
end
end
def prefix
ENV["SPACES_ROOT_FOLDER"]
end
end
end
It works perfectly with this. Other people suggest using Shrine.
Credit to for this great workaround : https://dev.to/drnic/how-to-isolate-your-rails-blobs-in-subfolders-1n0c
As of now ActiveStorage doesn't support that kind of functionality. Refer to this link. has_one_attached just accepts name and dependent.
Also in one of the GitHub issues, the maintainer clearly mentioned that they have clearly no idea of implementing something like this.
The workaround that I can imagine is, uploading the file from the front-end and then write a service that updates key field in active_storage_blob_statement
There is no official way to change the path which is determined by ActiveStorage::Blob#key and the source code is:
def key
self[:key] ||= self.class.generate_unique_secure_token
end
And ActieStorage::Blog.generate_unique_secure_token is
def generate_unique_secure_token
SecureRandom.base36(28)
end
So a workaround is to override the key method like the following:
# config/initializers/active_storage.rb
ActiveSupport.on_load(:active_storage_blob) do
def key
self[:key] ||= "my_folder/#{self.class.generate_unique_secure_token}"
end
end
Don't worry, this will not affect existing files. But you must be careful ActiveStorage is very new stuff, its source code is variant. When upgrading Rails version, remind yourself to take look whether this patch causes something wrong.
You can read ActiveStorage source code from here: https://github.com/rails/rails/tree/master/activestorage
Solution using Cloudinary service
If you're using Cloudinary you can set the folder on storage.yml:
cloudinary:
service: Cloudinary
folder: <%= Rails.env %>
With that, Cloudinary will automatically create folders based on your Rails env:
This is a long due issue with Active Storage that seems to have been worked around by the Cloudinary team. Thanks for the amazing work ❤️
# config/initializers/active_storage.rb
ActiveSupport.on_load(:active_storage_blob) do
def key
sql_find_order_id = "select * from active_storage_attachments where blob_id = #{self.id}"
active_storage_attachment = ActiveRecord::Base.connection.select_one(sql_find_order_id)
# this variable record_id contains the id of object association in has_one_attached
record_id = active_storage_attachment['record_id']
self[:key] = "my_folder/#{self.class.generate_unique_secure_token}"
self.save
self[:key]
end
end
Active Storage by default doesn't contain a path/folder feature but you can override the function by
model.file.attach(key: "downloads/filename", io: File.open(file), content_type: file.content_type, filename: "#{file.original_filename}")
Doing this will store the key with the path where you want to store the file in the s3 subdirectory and upload it at the exact place where you want.

Upload files to multiple buckets

I create a rails application for uploading files through carrierwave to S3 bucket,
I uploaded them to one bucket and I want to upload them to two buckets and regions at the same time .
How can I do that?
You can create an upload method and send your bucket name as an argument. A quick and dirty version would look something like:
def upload_file(specific_bucket = nil)
unless specific_bucket
BUCKET_LIST.each do |bucket|
# send file to bucket
end
else
# upload to specific_bucket
end
end
Store your bucket list in an appropriate location
BUCKET_LIST = [bucket_name_one, bucket_name_two]

Resources