要么得到的ID,要么箱,从after_post_process或after_save的 [英] Either get the id, either the bin, from after_post_process or after_save

查看:280
本文介绍了要么得到的ID,要么箱,从after_post_process或after_save的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在争取这个有一段时间了,和你的帮助将肯定大大AP preciation。

我已经建立了一个方法来签署PDF文档,你可以在这里找到 ,现在从我的签名档是只有一步之遥。

我想以异步方式做到这一点,但首先我需要了解如何同步做。

所以我尝试2种不同的方法,在post_process后:

  after_post_process办|回执|
    如果receipt.receipt_file_changed?

      要求AWS-SDK
          logger.debug(回执编号:#{self.inspect})
          文件= receipt.receipt_file.queued_for_write [:原创]
          S3 = AWS :: S3.new(
          access_key_id:S3_CONFIG [access_key_id],
          secret_access_key:S3_CONFIG [secret_access_key])
          bucket_name = S3_CONFIG [斗]

          B = s3.buckets [bucket_name]
          filen = File.basename(file.path)
          outputF =原创/ teste.pdf

          O = b.objects [outputF]
          o.write(文件:file.path)

    结束
  结束
 

在这里我希望能够将文件发送到路径类似 /original/1/myfilename.pdf ,其中1人是我的receipt_id(并且是空的在后post_process的时间)。

然后我尝试了不同的方法与after_save的,但receipt_file.to_file(:原创)不是一个有效的电话...

  after_save的办|回执|
    如果receipt.receipt_file_changed?

      要求AWS-SDK
          logger.debug(回执编号:#{receipt.inspect})

          S3 = AWS :: S3.new(
          access_key_id:S3_CONFIG [access_key_id],
          secret_access_key:S3_CONFIG [secret_access_key])
          bucket_name = S3_CONFIG [斗]

          B = s3.buckets [bucket_name]
          filen = File.basename(receipt_file_file_name)
          outputF =原创/ teste.pdf

          O = b.objects [outputF]
          o.write(文件:receipt.receipt_file.to_file(:原创))

    结束
  结束
 

我怎样才能获得该文件并重新上传回S3?

修改

经过一番研究,我看我们如何可以加载亚马逊的文件,现在的问题是,我的文件内容是空的......我究竟做错了什么?

  after_save的办|回执|
    如果receipt.receipt_file_changed?

      要求AWS-SDK

          logger.debug(我是这里的内部after_save的)
          S3 = AWS :: S3.new(
          access_key_id:S3_CONFIG [access_key_id],
          secret_access_key:S3_CONFIG [secret_access_key])
          bucket_name = S3_CONFIG [斗]

          B = s3.buckets [bucket_name]
          filen = File.basename(receipt_file_file_name)
          logger.debug(文件名是#{filen})
          路径=原始/#{receipt.id} / {#} filen
          O = b.objects [路径]

          需要临时文件

          EXT = File.extname(filen)

          文件= Tempfile.new([File.basename(filen,分机),分机]:编码=>ASCII-8位)
          #流从S3下载到磁盘上的文件

          开始
            o.read做|块|
                file.write(块)
            结束
          结束
          file.close

          logger.debug(文件是#{file.inspect})

          o.write(文件:signPdf(文件)。路径)
          file.unlink

    结束
  结束
 

解决方案

在片分裂这个问题后,我发现真正的问题是下载的过程中,并写入到本地磁盘。所以我问这个<一href="http://stackoverflow.com/questions/12352230/using-aws-sdk-to-download-files-from-s3-encoding-not-right">question,并达成了解决办法。 最终得到了这个code为以后保存:

  after_save的办|回执|
    如果receipt.receipt_file_changed? &功放;&安培; !@receipt.receipt_file.url [PDF]

      要求AWS-SDK

          S3 = AWS :: S3.new(
          access_key_id:S3_CONFIG [access_key_id],
          secret_access_key:S3_CONFIG [secret_access_key])
          bucket_name = S3_CONFIG [斗]

          B = s3.buckets [bucket_name]
          filen = File.basename(receipt_file_file_name)

          路径=原始/#{receipt.id} / {#} filen
          O = b.objects [路径]

          需要开放-URI
          需要临时文件

          EXT = File.extname(filen)
          tfile = Tempfile.new([File.basename(filen,分机),分机])
          开始
            开放(tfile.path,世行)做|文件|
                文件&LT;&LT;开放(receipt.receipt_file.url,:ssl_verify_mode =&GT;的OpenSSL :: SSL :: VERIFY_NONE).read
                o.write(文件:signPdf(文件)。路径,命令acl =&GT;:public_read)
            结束
          确保
            tfile.close!
          结束

    结束
  结束
 

I've been fighting this for some time now, and your help will certainly be of much appreciation.

I've built a method to sign pdf documents which you can find here, and now am only one step away from signing my file.

I would like to do this asynchronously, but first i would need to understand how to do it synchronously.

So I try 2 different approaches, the after post_process :

after_post_process do |receipt|
    if receipt.receipt_file_changed?

      require 'aws-sdk'
          logger.debug("RECEIPT ID: #{self.inspect}")
          file = receipt.receipt_file.queued_for_write[:original]
          s3=AWS::S3.new(
          access_key_id: S3_CONFIG["access_key_id"],
          secret_access_key: S3_CONFIG["secret_access_key"])
          bucket_name = S3_CONFIG["bucket"]

          b = s3.buckets[bucket_name]
          filen = File.basename(file.path)
          outputF = "original/teste.pdf"

          o = b.objects[outputF]
          o.write(file: file.path)

    end
  end

where i wanted to be able to send the file to a path something like /original/1/myfilename.pdf where 1 would be my receipt_id (and is null at the time of the after post_process).

I then tried a different approach with after_save, but receipt_file.to_file(:original) is not a valid call...

after_save do |receipt|
    if receipt.receipt_file_changed?

      require 'aws-sdk'
          logger.debug("RECEIPT ID: #{receipt.inspect}")

          s3=AWS::S3.new(
          access_key_id: S3_CONFIG["access_key_id"],
          secret_access_key: S3_CONFIG["secret_access_key"])
          bucket_name = S3_CONFIG["bucket"]

          b = s3.buckets[bucket_name]
          filen = File.basename(receipt_file_file_name)
          outputF = "original/teste.pdf"

          o = b.objects[outputF]
          o.write(file: receipt.receipt_file.to_file(:original))

    end
  end

How can i get the file and upload it back again to S3 ?

Edit

After some research i read how we can load a file from Amazon, and now the problem is that my file content is empty...What am i doing wrong?

after_save do |receipt|
    if receipt.receipt_file_changed?

      require 'aws-sdk'

          logger.debug("I was here inside after_save")
          s3=AWS::S3.new(
          access_key_id: S3_CONFIG["access_key_id"],
          secret_access_key: S3_CONFIG["secret_access_key"])
          bucket_name = S3_CONFIG["bucket"]

          b = s3.buckets[bucket_name]
          filen = File.basename(receipt_file_file_name)
          logger.debug("Filename is #{filen}")
          path = "original/#{receipt.id}/#{filen}"
          o = b.objects[path]

          require 'tempfile'

          ext= File.extname(filen)

          file = Tempfile.new([File.basename(filen,ext),ext], :encoding => 'ascii-8bit')
          # streaming download from S3 to a file on disk

          begin
            o.read do |chunk|
                file.write(chunk)
            end
          end
          file.close

          logger.debug("File is #{file.inspect}")

          o.write(file: signPdf(file).path)
          file.unlink

    end
  end

解决方案

After splitting this problem in pieces, i noticed the real problem was within the process of download and writing to local disk. So i asked this question, and reached for a workaround. In the end got this code for the after save :

  after_save do |receipt|
    if receipt.receipt_file_changed? && !@receipt.receipt_file.url[".pdf"] 

      require 'aws-sdk'

          s3=AWS::S3.new(
          access_key_id: S3_CONFIG["access_key_id"],
          secret_access_key: S3_CONFIG["secret_access_key"])
          bucket_name = S3_CONFIG["bucket"]

          b = s3.buckets[bucket_name]
          filen = File.basename(receipt_file_file_name)

          path = "original/#{receipt.id}/#{filen}"
          o = b.objects[path]

          require 'open-uri'
          require 'tempfile'

          ext= File.extname(filen)
          tfile = Tempfile.new([File.basename(filen,ext),ext])
          begin
            open(tfile.path,"wb") do |file|
                file << open(receipt.receipt_file.url,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
                o.write(file: signPdf(file).path, :acl => :public_read)
            end
          ensure
            tfile.close!
          end

    end
  end

这篇关于要么得到的ID,要么箱,从after_post_process或after_save的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆