如何在Object.get的输出中获取文件流 [英] How to get file stream in the output of Object.get

查看:130
本文介绍了如何在Object.get的输出中获取文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在 aws-sdk-ruby 的帮助下编写的Rails API. ,它从AWS检索文件并返回API的响应.我能否以某种方式获取文件流以响应 object.get ,我可以直接从Rails API返回.

I a writing a Rails API, with help of aws-sdk-ruby, which retrieves a file from AWS and returns in the response of API. Can I get somehow file stream in response of object.get, which I can directly return from the Rails API.

s3 = Aws::S3::Resource.new
bucket_name = "my_bucket"
bucket = s3.bucket(bucket_name)
object = bucket.object("a/b/my.pdf")
Rails.logger.info 'Downloading file to AWS'
downloaded_data = object.get({})
send_data(downloaded_data,
    :filename => "my.pdf",
    :type => "mime/type"
)

但是它不返回文件.

我知道的一个选择是首先使用此行将文件保存在本地:

One option I know is to first save the file in local using this line:

object.get(response_target: '/tmp/my.pdf')

我可以返回此文件,但是有一种方法可以跳过此步骤,直接返回object.get的响应,而无需保存在本地中.

Than I can return this file but is there a way to skip this step and directly return the response of object.get without saving in local.

我不能使用此解决方案,因为我的网址不是公开的,我只是在创建REST API.

I can not use this solution as my URL are not public and I am just creating a REST API.

尝试解决方案时,我得到了如下所示的屏幕.

I got screen like following when I tried this solution.

推荐答案

到目前为止,我正在从这样的对象获取URL:

As of now what I am doing is getting a URL from the object like this:

url = object.presigned_url(:get, expires_in: 3600)

,并使用以下代码发送响应:

and using following code to send the response:

data = open(url)
send_data data.read, filename: file_name, type: "mime/type"

这篇关于如何在Object.get的输出中获取文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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