结合使用Fog和Ruby生成预签名URL,以在Amazon S3中放置文件 [英] Use Fog with Ruby to generate a Pre-signed URL to PUT a file in Amazon S3

查看:134
本文介绍了结合使用Fog和Ruby生成预签名URL,以在Amazon S3中放置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fog gem生成预签名的URL.我可以成功完成此操作以获得对该文件的读取访问权限.这是我的工作:

I am using the Fog gem to generate presigned urls. I can do this successfully to get read access to the file. Here's what I do:

    fog_s3 = Fog::Storage.new({
          :provider                 => 'AWS',
          :aws_access_key_id        => key,
          :aws_secret_access_key    => secret
    })
    object_path = 'foo.wav' 
    expiry = Date.new(2014,2,1).to_time.to_i
    url = fog_s3.directories.new(:key => bucket).files.new(:key => object_path).url(expiry,path_style: true)

但是当我尝试上传文件时,这不起作用.有没有一种方法可以指定http动词,使其成为PUT而不是GET?

But this doesn't work when I try to upload the file. Is there a way to specify the http verb so it would be a PUT and not a GET?

编辑,我看到了一种方法:put_object_url,可能会有所帮助.我不知道如何访问.

EDIT I see a method: put_object_url which might help. I don't know how access it.

谢谢

编辑:

它有帮助-它给了我一个PUT-而不是GET.但是,我仍然有问题.我添加了内容类型:

It helped - it got me a PUT - not GET. However, I'm still having issues. I added content type:

    headers = { "Content-Type" => "audio/wav" }
    options = { path_style: true }
    object_path = 'foo.wav' 
    expiry = Date.new(2014,2,1).to_time.to_i  
    url = fog_s3.put_object_url(bucket,object_path, expiry, headers, options)

,但该URL中不包含Content-Type.从HTML中的Javascript完成后,我在URL中得到了Content-Type,这似乎可行.这是雾的问题吗?还是我的标题不正确?

but the url does not contain Content-Type in it. When done from Javascript in HTML I get the Content-Type in the url and that seems to work. Is this an issue with Fog? or is my header incorrect?

推荐答案

我认为put_object_url确实是您想要的.如果您按照url方法返回定义的位置,则可以在此处看到它使用的一种称为get_object_url的相似方法(

I think put_object_url is indeed what you want. If you follow the url method back to where it is defined, you can see it uses a similar method underlying it called get_object_url here (https://github.com/fog/fog/blob/dc7c5e285a1a252031d3d1570cbf2289f7137ed0/lib/fog/aws/models/storage/files.rb#L83). You should be able to do something similar and can do so by calling this method from the fog_s3 object you already created above. It should end up just looking like this:

headers = {}
options = { path_style: true }
url = fog_s3.put_object_url(bucket, object_path, expires, headers, options)

请注意,与get_object_url不同,那里有一个额外的标头选项(您可以用来做诸如设置Content-Type之类的事情).

Note that unlike get_object_url there is an extra headers option snuck in there (which you can use to do stuff like set Content-Type I believe).

希望能为您排序,但是如果您还有其他问题,请告诉我.谢谢!

Hope that sorts it for you, but just let me know if you have further questions. Thanks!

附录

嗯,似乎毕竟可能存在一个与此相关的错误(我想知道现在这部分代码已被执行了多少).我认为您应该可以解决此问题(但我不确定).我怀疑您也可以将选项中的值复制为查询参数.你可以尝试这样的事情吗?

Hmm, seems there may be a bug related to this after all (I'm wondering now how much this portion of the code has been exercised). I think you should be able to work around it though (but I'm not certain). I suspect you can just duplicate the value in the options as a query param also. Could you try something like this?

headers = query = { 'Content-Type' => 'audio/wav' }
options = { path_style: true, query: query }
url = fog_s3.put_object_url(bucket, object_path, expires, headers, options)

希望这可以为您填补空白(如果可以的话,如果可以的话,我们可以考虑在雾中修复此行为更多).谢谢!

Hopefully that fills in the blanks for you (and if so we can think some more about fixing that behavior within fog if it makes sense to do so). Thanks!

这篇关于结合使用Fog和Ruby生成预签名URL,以在Amazon S3中放置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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