Provides Bunny Storage for Shrine, built on Bunny's S3-compatible API.
It is a subclass of Shrine::Storage::S3 that sets up the S3 client for Bunny and builds file URLs from your Bunny pull zone.
gem "shrine-bunny"The storage zone must have S3 compatibility turned on. Bunny lets you turn it on only when you create the zone.
require "shrine/storage/bunny"
bunny_options = {
storage_zone: "my-zone", # storage zone name
region: "de", # storage zone region (de, ny, sg, uk, se, la, jh, syd)
password: "...", # storage zone password
host: "https://my-zone.b-cdn.net", # pull zone URL or its custom hostname
}
Shrine.storages = {
cache: Shrine::Storage::Bunny.new(prefix: "cache", **bunny_options),
store: Shrine::Storage::Bunny.new(**bunny_options),
}You find the zone name, region and password in the Access tab of the storage zone. Every other option goes to Shrine::Storage::S3, so :prefix, :upload_options, :client and the other S3 options work as usual.
Bunny serves files publicly only through a pull zone, so #url always returns the pull zone URL:
uploaded_file.url #=> "https://my-zone.b-cdn.net/photo/1/image/abc123.jpg"- The URL is never presigned, and
:publichas no effect. - You can pass
:hostto use a different hostname for one URL. - The key is escaped the same way as
Aws::S3::Object#public_url, so the URL is the same asShrine::Storage::S3#url(public: true, host:)gives. - A
:signeris called with the URL and the URL options, the same as inShrine::Storage::S3. You can use it for Bunny token authentication.
Shrine::Storage::S3#url asks the AWS SDK for the full S3 URL and then puts the host in its place. This storage builds the URL from the host and the key only. For 200 URLs, with shrine 3.10.2 and aws-sdk-s3 1.238.0 on Ruby 4.0:
| Allocations | Time | |
|---|---|---|
Shrine::Storage::S3 |
69,001 | 18.2 ms |
Shrine::Storage::Bunny |
800 | 0.1 ms |
- Endpoint:
https://<region>-s3.storage.bunnycdn.com, with the zone name as the bucket and the access key ID, and the zone password as the secret access key. - Checksums:
request_checksum_calculation: "when_required". Bunny documents only SHA256 checksums for uploads, so the SDK sends a checksum only when an operation requires one. - Copies:
copy_options: {}. Bunny has no object tagging, so a copy from cache to store does not send the tagging directive thatShrine::Storage::S3sends by default. - ACLs: Bunny has no ACLs.
Shrine::Storage::S3sends an ACL when:publicis set, so this storage raisesArgumentErroron:public.
bundle exec rake testThe unit tests use a stubbed S3 client. test/integration_test.rb runs the Shrine storage linter against a real storage zone and fetches a file through the pull zone. It runs when these variables are set:
BUNNY_STORAGE_ZONE=... BUNNY_STORAGE_REGION=... BUNNY_STORAGE_PASSWORD=... BUNNY_PULL_ZONE_HOST=... bundle exec rake testUse a test storage zone. The linter deletes every file under the random prefix it uploads to.