Last active
July 16, 2022 01:28
-
-
Save unasuke/81c3a0e7f06809e77d3bd83e25a383a0 to your computer and use it in GitHub Desktop.
mini_paperclip with S3 compatible object storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3' | |
services: | |
mini_paperclip: | |
build: . | |
command: bundle exec ruby script.rb | |
depends_on: | |
- minio | |
minio: | |
image: quay.io/minio/minio | |
environment: | |
MINIO_ROOT_USER: minioadmin | |
MINIO_ROOT_PASSWORD: minioadmin | |
command: server --console-address ":9001" /data | |
ports: | |
- "9000:9000" | |
- "9001:9001" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ruby:2.7 | |
WORKDIR /app | |
COPY Gemfile Gemfile.lock /app/ | |
RUN bundle install | |
COPY . . | |
CMD bundle exec ruby script.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
source "https://rubygems.org" | |
gem "mini_paperclip", git: "https://github.com/unasuke/mini_paperclip.git", branch: "s3_option" | |
gem "aws-sdk-s3" | |
gem "activerecord" | |
gem "actionpack" | |
gem "sqlite3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GIT | |
remote: https://github.com/unasuke/mini_paperclip.git | |
revision: 63f4360a5f9a48ca9254790870428da0d2e8f725 | |
branch: s3_option | |
specs: | |
mini_paperclip (0.3.1) | |
activemodel | |
activesupport | |
aws-sdk-s3 | |
image_size | |
mimemagic | |
mini_magick | |
GEM | |
remote: https://rubygems.org/ | |
specs: | |
actionpack (7.0.3.1) | |
actionview (= 7.0.3.1) | |
activesupport (= 7.0.3.1) | |
rack (~> 2.0, >= 2.2.0) | |
rack-test (>= 0.6.3) | |
rails-dom-testing (~> 2.0) | |
rails-html-sanitizer (~> 1.0, >= 1.2.0) | |
actionview (7.0.3.1) | |
activesupport (= 7.0.3.1) | |
builder (~> 3.1) | |
erubi (~> 1.4) | |
rails-dom-testing (~> 2.0) | |
rails-html-sanitizer (~> 1.1, >= 1.2.0) | |
activemodel (7.0.3.1) | |
activesupport (= 7.0.3.1) | |
activerecord (7.0.3.1) | |
activemodel (= 7.0.3.1) | |
activesupport (= 7.0.3.1) | |
activesupport (7.0.3.1) | |
concurrent-ruby (~> 1.0, >= 1.0.2) | |
i18n (>= 1.6, < 2) | |
minitest (>= 5.1) | |
tzinfo (~> 2.0) | |
aws-eventstream (1.2.0) | |
aws-partitions (1.607.0) | |
aws-sdk-core (3.131.2) | |
aws-eventstream (~> 1, >= 1.0.2) | |
aws-partitions (~> 1, >= 1.525.0) | |
aws-sigv4 (~> 1.1) | |
jmespath (~> 1, >= 1.6.1) | |
aws-sdk-kms (1.57.0) | |
aws-sdk-core (~> 3, >= 3.127.0) | |
aws-sigv4 (~> 1.1) | |
aws-sdk-s3 (1.114.0) | |
aws-sdk-core (~> 3, >= 3.127.0) | |
aws-sdk-kms (~> 1) | |
aws-sigv4 (~> 1.4) | |
aws-sigv4 (1.5.0) | |
aws-eventstream (~> 1, >= 1.0.2) | |
builder (3.2.4) | |
concurrent-ruby (1.1.10) | |
crass (1.0.6) | |
erubi (1.10.0) | |
i18n (1.12.0) | |
concurrent-ruby (~> 1.0) | |
image_size (3.0.2) | |
jmespath (1.6.1) | |
loofah (2.18.0) | |
crass (~> 1.0.2) | |
nokogiri (>= 1.5.9) | |
mimemagic (0.4.3) | |
nokogiri (~> 1) | |
rake | |
mini_magick (4.11.0) | |
minitest (5.16.2) | |
nokogiri (1.13.7-x86_64-linux) | |
racc (~> 1.4) | |
racc (1.6.0) | |
rack (2.2.4) | |
rack-test (2.0.2) | |
rack (>= 1.3) | |
rails-dom-testing (2.0.3) | |
activesupport (>= 4.2.0) | |
nokogiri (>= 1.6) | |
rails-html-sanitizer (1.4.3) | |
loofah (~> 2.3) | |
rake (13.0.6) | |
sqlite3 (1.4.4) | |
tzinfo (2.0.4) | |
concurrent-ruby (~> 1.0) | |
PLATFORMS | |
x86_64-linux | |
DEPENDENCIES | |
actionpack | |
activerecord | |
aws-sdk-s3 | |
mini_paperclip! | |
sqlite3 | |
BUNDLED WITH | |
2.3.17 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'logger' | |
require 'aws-sdk-s3' | |
require 'active_record' | |
require 'action_dispatch' | |
require 'mini_paperclip' | |
logger = Logger.new(STDOUT) | |
logger.info "Prepare minio bucket" | |
aws_client = Aws::S3::Client.new( | |
access_key_id: "minioadmin", | |
secret_access_key: "minioadmin", | |
endpoint: "http://minio:9000", | |
region: "ap-northeast-1", | |
force_path_style: true, | |
) | |
aws_client.create_bucket(bucket: "minio-bucket") unless aws_client.list_buckets.buckets.map(&:name).include?("minio-bucket") | |
logger.info "bucket created" | |
MiniPaperclip.config.tap do |config| | |
config.storage = :s3 | |
config.url_scheme = 'http' | |
config.s3_bucket_name = 'minio-bucket' | |
config.url_path = ':class/:attachment/:hash.:extension' | |
config.hash_secret = 'secret' | |
config.s3_client_options = { | |
access_key_id: "minioadmin", | |
secret_access_key: "minioadmin", | |
endpoint: "http://minio:9000", | |
region: "ap-northeast-1", | |
force_path_style: true, | |
} | |
end | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
t.string :image_file_name | |
t.string :image_content_type | |
t.string :image_file_size | |
t.datetime :image_updated_at | |
end | |
end | |
class Post < ActiveRecord::Base | |
extend MiniPaperclip::ClassMethods | |
has_attached_file :image | |
end | |
file = ActionDispatch::Http::UploadedFile.new( | |
filename: File.basename("clip.png"), | |
type: "image/png", | |
tempfile: File.open("clip.png") | |
) | |
logger.info file | |
post = Post.new | |
post.image = file | |
post.save |
Author
unasuke
commented
Jul 16, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment