Provides Fog storage for Shrine.
Fog is an abstraction over a variety of cloud storages (e.g. Google Cloud and Dropbox).
gem "shrine-fog"
gem "fog-xyz" # Fog gem for the storage you want to use
Require the appropriate Fog gem, and assign the parameters for initializing the storage:
require "shrine/storage/fog"
require "fog/google"
Shrine.storages[:store] = Shrine::Storage::Fog.new(
provider: "Google", #
google_storage_access_key_id: "ACCESS_KEY_ID", # Fog credentials
google_storage_secret_access_key: "SECRET_ACCESS_KEY", #
directory: "uploads",
)
You can also assign a Fog storage object as the :connection
:
require "shrine/storage/fog"
require "fog/google"
google = Fog::Storage.new(
provider: "Google",
google_storage_access_key_id: "ACCESS_KEY_ID",
google_storage_secret_access_key: "SECRET_ACCESS_KEY",
)
Shrine.storages[:store] = Shrine::Storage::Fog.new(
connection: google,
directory: "uploads",
)
If both cache and store are a Fog storage, the uploaded file is copied to store instead of reuploaded.
By default the shrine-fog will generate public unsigned URLs, but if you want
to change that tell Fog not to store files publicly, you can set :public
to
false:
fog = Shrine::Storage::Fog.new(**fog_options)
fog.url("image.jpg") #=> "https://my-bucket.s3-eu-west-1.amazonaws.com/image.jpg"
fog = Shrine::Storage::Fog.new(public: false, **fog_options)
fog.url("image.jpg") #=> "https://my-bucket.s3-eu-west-1.amazonaws.com/foo?X-Amz-Expires=3600&X-Amz-Date=20151217T102105Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIJF55TMZZY45UT6Q/20151217/eu-west-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=6908d8cd85ce4469f141a36955611f26d29ae7919eb8a6cba28f9194a92d96c3"
The signed URLs by default expire in 1 hour, you set :expires
to number of
seconds you want the URL to expire:
Shrine::Storage::Fog.new(expires: 24*60*60, **fog_options) # expires in 1 day
If you want to store your files to Amazon S3 or the filesystem, you should use the storages that ship with Shrine (instead of fog-aws or fog-local) as they are much more advanced.
Tests use fog-aws, so you'll have to create an .env
file with appropriate
credentials:
# .env
S3_ACCESS_KEY_ID="..."
S3_SECRET_ACCESS_KEY="..."
S3_REGION="..."
S3_BUCKET="..."
Afterwards you can run the tests:
$ bundle exec rake test
This gem was inspired by refile-fog.