Ruby on rails 上传文件amazon s3预签名帖子
我正在开发一个web应用程序,我想用预先签名的帖子将文件上传到AmazonS3。按照这个链接上的说明:我有以下表格Ruby on rails 上传文件amazon s3预签名帖子,ruby-on-rails,amazon-web-services,amazon-s3,Ruby On Rails,Amazon Web Services,Amazon S3,我正在开发一个web应用程序,我想用预先签名的帖子将文件上传到AmazonS3。按照这个链接上的说明:我有以下表格 <form action=<%= @aws_s3_url %> method="post" enctype="multipart/form-data"> <input type="hidden" name="key" value= <%= @base_path + "${filename}" %> > <
<form action=<%= @aws_s3_url %> method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value= <%= @base_path + "${filename}" %> >
<input type="hidden" name="AWSAccessKeyId" value= <%= @aws_access_key_id %> >
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value=<%= "https://localhost:3000/projects/" + @project.id.to_s %> >
<input type="hidden" name="policy" value= <%= @aws_policy %>>
<input type="hidden" name="signature" value=<%= @aws_signature %>>
<!-- Include any additional input fields here -->
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
然后像上面的链接一样进行编码和签名:
policy=generate\u policy\u pre\u signed\u post(“/users\u data/”+user\u creds.aws\u identity\u id+”/”)
但是,当我尝试将文件上载到S3时,它会给出以下错误:“不允许针对此资源使用指定的方法。”
这怎么可能?在IAM中,我的用户拥有写入S3的所有权限,我还尝试添加一个bucket策略,如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::my_iam"
},
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my_bucket/*"
]
}
]
}
对可能发生的事情有什么想法吗
编辑:
我也尝试过修改CORS权限(但仍然不起作用)
https://0.0.0.0:3000/*
得到
邮递
放
3000
*
好的,我找到了!真是太愚蠢了,我在s3-eu-west-1.s3.amazonaws.com/my_bucket中多了一个s3,正确的方法是:s3-eu-west-1.amazonaws.com/my_bucket
@aws_policy = Base64.encode64(policy).gsub("\n","")
@aws_signature = Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
server_credentials.secret_access_key, @aws_policy)
).gsub("\n","")
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::my_iam"
},
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my_bucket/*"
]
}
]
}
<CORSRule>
<AllowedOrigin>https://0.0.0.0:3000/*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>