简体   繁体   中英

How to make Paperclip read-only on development? (prevent upload/delete/overwrite)

My DBs of production and development are somewhat in sync, so development can read images from production paths (S3).

The problem is when I delete, update or create records on development, it affects the S3 image.

I don't want this behavior to happen on development but it should happen on production.

Is there an option to turn paperclip into readonly mode? I still want to see the images from S3 (and not 404 images).

I saw the :preserve_files option which is good to protect delete. Is there an option to protect overwrite / disable upload?

Well, patchy, ugly and unsafe for future versions, but does the job for the meantime.

config/initializers/paperclip.rb

if Rails.env.development?
  module Paperclip
    class Attachment
      def assign uploaded_file
      end

      def save
      end

      def clear(*)
      end

      def destroy
      end

      private
      def post_process(*)
      end

      def post_process_styles(*)
      end

      def post_process_style(*)
      end

      def queue_some_for_delete(*)
      end

      def queue_all_for_delete
      end

      def after_flush_writes
      end
    end
  end
end

Assuming you need to use production data in development, I think it would make a lot more sense to create a "User Policy" where a user can only read certain S3 resources. Then change your environment variables accordingly https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-policies-s3.html

Then, you can handle errors in development (S3 client should fail if you try to update with read only privileges). This ensures you can't touch anything in production

For example (pseudocode),

if Rails.env.development?
  // do not update
else
  Model.attachment.assign()
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM