如何通过Trix将上传的对象命名为Amazon S3?

如何解决如何通过Trix将上传的对象命名为Amazon S3?

因此,我在Rails应用程序上设置了活动存储和操作文本。我在我的WYSISYG编辑器中使用了trix,仅将图像放入编辑器中,它就被升格为S3。 这是我的设置:

class Story < ApplicationRecord
  has_rich_text :content
  belongs_to :industry
  ...ommited for brevity 

在我看来(我正在使用ruby bootstrap_form:

= bootstrap_form_for (@story) do |f|
                %fieldset.actions.control-group  
                  = f.text_field :title,hide_label: true,placeholder: "Title of your story",wrapper: { class: 'mb-3 story-title' },control_class: 'w-100 p-3'
                  = f.text_field :job_title,placeholder: "Job title",wrapper: { class: 'mb-3 job-title' },control_class: 'w-100 p-3'
                  = f.text_field :video_link,placeholder: "Attach video",wrapper: { class: 'mb-3 video-content hide' },control_class: 'w-100 p-3'
                  = f.rich_text_area :content,placeholder: "Write your story",wrapper: { class: 'rich-content' }
                  = f.text_field :tag_list,placeholder: "Insert job specific tags separated by comma",wrapper: { class: 'mb-3' },control_class: 'w-100 p-3' 
                  = f.select :industry_id,options_for_select(Industry.all.map {|industry| [industry.name,industry.id]}),{},control_class: 'col-md-6 col-xs-12 p-3 input-Box'  
                  = f.select :country_id,options_for_select(Country.all.map {|country| [country.name,country.id]}),control_class: 'col-md-6 col-xs-12 p-3 input-Box'                 
                  = f.submit "Submit",class: 'btn btn-action p3 mt-3'

这是我的控制器动作:

def create         
    @story = Story.new(story_params)     
    @story.user = current_user               
    if @story.save
      @story.tag_list = tag_params      
      flash[:notice] = "You've successfully created a story titled #{@story.title}"
      redirect_to root_path
    else 
      flash[:error] = "Please fix the following error(s)"
    end
  end

当我将文件放到trix编辑器中时,据我了解,rails已将annex.js直接将图像上传到S3。当我从编辑器中删除文件时,就会出现问题。它不会在S3中删除关联的对象。仅当我删除整个故事时,该对象才会被删除

我有两个选择和相关问题:

  1. 如何防止trix自动上载图像,而是使用trix的重写方法自己上载图像(因此,我可以命名该对象以供以后删除以进行删除)? 这是我想出的:
window.addEventListener("trix-file-accept",function(event) {
 const acceptedTypes = ['image/jpeg','image/png']
 if (!acceptedTypes.includes(event.file.type)) {
   event.preventDefault()
   alert("Only support attachment of jpeg or png files")
 }

 event.preventDefault()

 var AWS = require('aws-sdk');
 
 AWS.config.credentials = { 
   accessKeyId: gon.accessKey,secretAccessKey: gon.secretKey,region: 'us-west-1'
 }

 var s3 = new AWS.S3();
 var params = {  Bucket: gon.bucketName,Key: '#object-name#' };

 var file = event.file
   
 var upload = new AWS.S3.ManagedUpload({
   params: {
     Bucket: gon.bucketName,Key: file.name,Body: file,ACL: "public-read"
   }
 });

 var promise = upload.promise();

 promise.then(
   function(data) {
     alert("Successfully uploaded photo.");      
   },function(err) {
     console.log(err)
     return alert("There was an error uploading your photo: ",err.message);
   }
 );
})
  1. 代码基本上绕过了Trix自动将图像上传到S3的操作。图像已成功上传到S3,但是问题在于富文本格式无法捕获它,这意味着故事。内容中将没有图像。 如果我走这条路线,如何将上传的图像与模型关联?

  2. 我在考虑的另一个选项是,我不确定该文件是否可能甚至绕过Trix在文件丢失时上传图像。而是在创建故事后仅将图像上传到S3。当我向用户添加个人资料照片时,就像我为用户控制器所做的事情:

def update  
  if current_user == @user
    if @user.update(user_params)           
      if params[:avatar] != nil && params[:avatar] != "deleteAvatar"        
        @user.avatar.purge   
        @user.avatar.attach(params[:avatar])
      elsif params[:avatar] == "deleteAvatar"      
        @user.avatar.purge         
      end

            flash['notice'] = "Your profile was updated!"
      render json: { status: true }
      # redirect_to login_path
        else 
            render json: { error: @user.errors.messages }
    end 
  else
    flash[:error] = "You are not authorized to do that"
    redirect_to root_path 
  end   
end

感谢社区的任何帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?