복붙노트

[RUBY-ON-RAILS] 아마존 S3에서 파일을 다운로드 send_file 사용하십니까?

RUBY-ON-RAILS

아마존 S3에서 파일을 다운로드 send_file 사용하십니까?

나는 사용자가 S3에 저장되어있는 파일을 다운로드 할 수 있어야한다있는 내 응용 프로그램의 다운로드 링크가 있습니다. 이 파일은 같은 것을 보면 URL을에 공개적으로 액세스 할 수 있습니다

https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png

다운로드 링크는 내 컨트롤러에서 작업을 명중 :

class AttachmentsController < ApplicationController
  def show
    @attachment = Attachment.find(params[:id])
    send_file(@attachment.file.url, disposition: 'attachment')
  end
end

내가 파일을 다운로드하려고하지만 나는 다음과 같은 오류가 발생합니다 :

ActionController::MissingFile in AttachmentsController#show

Cannot read file https://s3.amazonaws.com/:bucket_name/:path/:to/:file.png
Rails.root: /Users/user/dev/rails/print

Application Trace | Framework Trace | Full Trace
app/controllers/attachments_controller.rb:9:in `show'

이 파일은 분명히 존재하고 오류 메시지의 URL에서 공개적으로 액세스 할 수 있습니다.

어떻게 사용자가 S3 파일을 다운로드 할 수 있습니까?

해결법

  1. ==============================

    1.위해 웹 서버에서 파일을 보내려면,

    위해 웹 서버에서 파일을 보내려면,

  2. ==============================

    2.또한 SEND_DATA 사용할 수 있습니다.

    또한 SEND_DATA 사용할 수 있습니다.

    당신은 더 나은 제어 할 수 있기 때문에이 옵션을 좋아한다. 당신은 일부 사용자에게 혼동 될 수도 S3 사용자를, 전송되지 않습니다.

    난 그냥 AttachmentsController에 다운로드 방법을 추가

    def download
      data = open("https://s3.amazonaws.com/PATTH TO YOUR FILE") 
      send_data data.read, filename: "NAME YOU WANT.pdf", type: "application/pdf", disposition: 'inline', stream: 'true', buffer_size: '4096' 
    end 
    

    그리고 경로를 추가

    get "attachments/download"
    
  3. ==============================

    3.나는이 처리하는 가장 좋은 방법이 만료 S3 URL을 사용하고있는 것. 다른 방법은 다음과 같은 문제가 있습니다 :

    나는이 처리하는 가장 좋은 방법이 만료 S3 URL을 사용하고있는 것. 다른 방법은 다음과 같은 문제가 있습니다 :

    내 구현은 다음과 같습니다 :

    def download_url
      S3 = AWS::S3.new.buckets[ 'bucket_name' ] # This can be done elsewhere as well,
                                                # e.g config/environments/development.rb
      url_options = { 
        expires_in:                   60.minutes, 
        use_ssl:                      true, 
        response_content_disposition: "attachment; filename=\"#{attachment_file_name}\""
      }
    
      S3.objects[ self.path ].url_for( :read, url_options ).to_s
    end
    
    <%= link_to 'Download Avicii by Avicii', attachment.download_url %>
    

    이게 다예요.

    당신은 여전히 ​​다음 몇 가지 이유로 다운로드 작업을 계속하고 싶었다면 바로 이것을 사용 :

    당신의 attachments_controller.rb에서

    def download
      redirect_to @attachment.download_url
    end
    

    덕분에 그의 인도를 guilleva합니다.

  4. ==============================

    4.난 그냥 아마존 S3에 내 공개 / 시스템 폴더를 마이그레이션 한. 도움이 위의 솔루션하지만 내 응용 프로그램은 문서의 다른 종류를 받아들입니다. 같은 동작을해야하는 경우 그래서, 이것은 나를 위해 도움이 :

    난 그냥 아마존 S3에 내 공개 / 시스템 폴더를 마이그레이션 한. 도움이 위의 솔루션하지만 내 응용 프로그램은 문서의 다른 종류를 받아들입니다. 같은 동작을해야하는 경우 그래서, 이것은 나를 위해 도움이 :

    @document = DriveDocument.where(id: params[:id])
    if @document.present?
      @document.track_downloads(current_user) if current_user
      data = open(@document.attachment.expiring_url)
      send_data data.read, filename: @document.attachment_file_name, type: @document.attachment_content_type, disposition: 'attachment'
    end
    

    이 파일은 DriveDocument 객체의 첨부 파일 필드에 저장되고있다. 이게 도움이 되길 바란다.

  5. ==============================

    5.다음은 나를 위해 잘 작동 결국 것입니다. SEND_DATA 사용하여 다음 S3 객체에서 원시 데이터를 얻기과하면 브라우저에서 해당를 전달합니다.

    다음은 나를 위해 잘 작동 결국 것입니다. SEND_DATA 사용하여 다음 S3 객체에서 원시 데이터를 얻기과하면 브라우저에서 해당를 전달합니다.

    AWS-SDK 보석 문서는 여기에서 찾을 사용 http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html

    전체 제어 방법

    def download
      AWS.config({
        access_key_id: "SECRET_KEY",
        secret_access_key: "SECRET_ACCESS_KEY"
      })
    
      send_data( 
        AWS::S3.new.buckets["S3_BUCKET"].objects["FILENAME"].read, {
          filename: "NAME_YOUR_FILE.pdf", 
          type: "application/pdf", 
          disposition: 'attachment', 
          stream: 'true', 
          buffer_size: '4096'
        }
      )
    end
    
  6. from https://stackoverflow.com/questions/12277971/using-send-file-to-download-a-file-from-amazon-s3 by cc-by-sa and MIT license