Blog

Download private file trên S3 về browser trong rails

Nếu file trên S3 được public thì chỉ cần trả về url của file cho user, khi user click vào url đó trên browser thì họ sẽ download được.

>> Đọc thêm:

KHOÁ HỌC TRUY VẤN VÀ THAO TÁC DỮ LIỆU SQL TỪ CƠ BẢN ĐẾN NÂNG CAO

KHÓA HỌC DATA WAREHOUSE : TỔNG HỢP, CHUẨN HÓA VÀ XÂY DỰNG KHO DỮ LIỆU TRONG DOANH NGHIỆP

KHÓA HỌC DATA MODEL – THIẾT KẾ MÔ HÌNH DỮ LIỆU TRONG DOANH NGHIỆP

LỘ TRÌNH TRỞ THÀNH DATA ENGINEER CHO NGƯỜI MỚI BẮT ĐẦU

DATA ENGINEER LÀ GÌ? CÔNG VIỆC CHÍNH CỦA DATA ENGINEER? CÁC KỸ NĂNG CẦN THIẾT

Còn nếu file trên S3 là private và ta muốn download file thông qua API thì làm như sau:

  • Download S3 file rồi lưu về file tạm(temporary file) trên server.
  • Gửi nội dung file tạm về cho người dùng
  • Xóa file tạm trên server(có thể xóa hoặc không nhưng nên xóa đi)

Khi không cần xóa file tạm thì bạn chỉ cần làm như sau (ở đây mình dùng gem aws-sdk-resources, nếu bạn dùng gem khác thì gọi với API tương ứng):

def download
  s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
  obj = s3.bucket(ENV['BUCKET_NAME']).object("file_path_in_S3")
  tempFile = Tempfile.new('test')
  obj.get(response_target: tempFile.path)
  send_file(tempFile.read, filename: "file_name")
end

Khi gọi send_file Rails server sẽ gửi request tới HTTP server(Apache hoặc Nginx), HTTP server khi nhận được request send_file sẽ gửi file cho client. Vì thế, nếu ta xóa temp file ngay sau send_file sẽ phát sinh lỗi do Rails không biết được là HTTP server có đang sử dụng file tạm đó hay không.

Đoạn code sau sẽ trả về file không đúng cho client:

def download
  s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
  obj = s3.bucket(ENV['BUCKET_NAME']).object("file_path_in_S3")
  tempFile = Tempfile.new('test')
  obj.get(response_target: tempFile.path)
  send_file(tempFile.read, filename: "file_name")
  tempFile.unlink  # wrong here
end

Để xóa được file tạm thì thay vì dùng send_file ta dùng send_data như sau:

def download
  s3 = Aws::S3::Resource.new(region: ENV['AWS_REGION'])
  obj = s3.bucket(ENV['BUCKET_NAME']).object("file_path_in_S3")
  tempFile = Tempfile.new('test')
  obj.get(response_target: tempFile.path)
  File.open(tempFile.path, 'r') do |f|
    send_data(f.read, filename: "file_name")
  end
ensure
  if tempFile.present?
    tempFile.close
    tempFile.unlink
  end
end

Link tham khảo: https://stackoverflow.com/questions/5535981/what-is-the-difference-between-send-data-and-send-file-in-ruby-on-rails

Leave a Reply

Your email address will not be published. Required fields are marked *

https://jmspc.ac.in/ slot4d slotsensa slot gacor https://www.jmsit.ac.in/placement/ suhubet slot4d https://fabritec.pe/asesor.php https://kibf.pk/ slot4d slot gacor https://www.jms.ac.in/admission-procedure/ slot gacor slot luar negeri slot4d slot gacor 4d totogacor slot4d slot4d slot gacor slot gacor slot gacor https://promo-sign.com/ slot4d demo slot slot thailand slot4d https://www.ugelhuaylas.edu.pe/ slot4d slot4d slot4d https://dulsa.com/ slot4d https://fabritec.pe/see/corporacion/ slot4d https://waterco.co.id/pompa-kolam-renang/ https://cliffcrestinn.com/rates/ slot demo https://www.bhagwati.ac.in/academics/ https://seteilhas.com.br/site/ https://www.homeofenglish.edu.kh/ https://umgeg.com/ https://www.paracasoverland.com.pe/ slot gacor slot gacor 4d https://amcstoneandcabinets.com/ totogacor slot gacor wd jutaan https://spingharkabul.edu.af/ https://lpmi.handayani.ac.id/surveyB https://warmihuasi.org/portfolio-items/ https://www.jalwadancecompany.com/x-2-3-bhangra-dancers/ https://gaesasa.com/tienda/ https://impactocontable.co/servicios-tributarios-pago-de-impuestos/ https://www.homeofenglish.edu.kh/tuition-chinese-program/ https://spingharkabul.edu.af/published-papers/ https://littlebigcat.com/ slot4d https://gaesasa.com/sobre-gaesa/ https://warmihuasi.org/proyectos/ https://coppercoat.com.br/ slot gacor https://www.jetfloweurope.com/ slot gacor https://www.norsal-eg.com/ slot4d https://subhkirancapital.com/ slot gacor https://spingharkabul.edu.af/stomatology/ https://solnascentealimentos.com.br/produto/456913/ https://www.balinanny-babbyhire.com/pricing/ https://www.homeofenglish.edu.kh/tuition-international-program-grade-1-8/ https://cliffcrestinn.com/historic-places-to-stay-in-santa-cruz-ca/ https://semplekg.edu.hk/en/2023-2024/ https://moalem24.com/ https://pps.handayani.ac.id/ toto 4d https://ekklesiahouses.com/ https://happyspring.com/ https://hitchparts.com/ https://praisevibration.com/ https://bolsecol.com/ https://www.uniparconstrutora.com.br/empreendimentos/ https://centrobienser.com/ slot gacor slot4d totogacor Slot Gacor situs gacor 4D situs gacor slot4d SLOT4D slot4d slot gacor 4d Slot Thailand slot4d Slot Thailand Slot Thailand Slot Gacor Maxwin Slot Thailand Slot Thailand