我试图遵循这个“如何-到” (表单发布到S3桶),并且似乎在策略和签名上失败了。
我不太确定我的政策和签名是不是错了?但我确实知道,我在评估我在我的助手中创建的方法时遇到了问题。我尝试将策略和签名值转换为符号<%= :S3_UPLOAD_SIGNATURE %>,并尝试了<%=h、<%=raw、“#{<%=.}”。
我以前调用过助手方法,没有任何问题,所以我有点迷路了。
错误:
NameError in Proj_files#new
Showing /app/views/proj_files/new.html.erb where line #8 raised:
uninitialized constant ActionView::CompiledTemplates::S3_UPLOAD_POLICY
Extracted source (around line #8):
5: <input type="hidden" name="AWSAccessKeyId" value= <%= ENV['AWS_ACCESS_KEY_ID'] %> >
6: <input type="hidden" name="acl" value="private">
7: <input type="hidden" name="success_action_redirect" value="http://localhost/">
8: <input type="hidden" name="policy" value= <%= S3_UPLOAD_POLICY %> >
9: <input type="hidden" name="signature" value= <%= S3_UPLOAD_SIGNATURE %> >
10: <input type="hidden" name="Content-Type" value="image/png">
11: <!-- Include any additional input fields here -->我有一个proj_files控制器,它有一个相应的new.html.erb:
<form action="https://s3.amazonaws.com/MY_BUCKET" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="uploads/${filename}">
<input type="hidden" name="AWSAccessKeyId" value= <%= ENV['AWS_ACCESS_KEY_ID'] %> >
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value="http://localhost/">
<input type="hidden" name="policy" value= <%= S3_UPLOAD_POLICY %> >
<input type="hidden" name="signature" value= <%= S3_UPLOAD_SIGNATURE %> >
<input type="hidden" name="Content-Type" value="image/png">
<!-- 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> proj_files_helper.rb:
module ProjFilesHelper
def S3_UPLOAD_POLICY options = {}
options[:content_type] ||= ''
options[:acl] ||= 'private'
options[:max_file_size] ||= 500.megabyte
options[:path] ||= ''
Base64.encode64(
"{'expiration': '#{10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')}',
'conditions': [
{'bucket': '#{ENV['S3_BUCKET']}'},
['starts-with', '$key', ''],
{'acl': '#{options[:acl]}'},
{'success_action_status': '201'},
['content-length-range', 0, #{options[:max_file_size]}],
['starts-with','$Content-Type','']
]
}").gsub(/\n|\r/, '')
end
def S3_UPLOAD_SIGNATURE options = {}
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
ENV['AWS_SECRET_ACCESS_KEY'], s3_policy(options))).gsub("\n","")
end
end谢谢你看一看!
更新:--我将方法名更改为小写,这使我更进一步(我应该意识到这一点!)。
现在我得到一个S3错误:
<Error>
<Code>AccessDenied</Code>
<Message>
Invalid according to Policy: Policy Condition failed: ["eq", "$bucket", "MY_BUCKETS_NAME"]
</Message>似乎有一个ENV变量$bucket坏引用,我要找."MY_BUCKETS_NAME“显示了合适的存储桶,任何人都可以提供任何帮助,帮助rails post表单启动和运行/指出我的错误--我会很感激的。
谢谢
在下面的注释中,UPDATE2将表单操作修改为"桶,桶“,并收到以下错误:
Invalid according to Policy: Policy Condition failed: ["eq", "$acl", "public-read"]接近..。谢谢!
UPDATE3我在战斗!
我修改了策略和ACL,使其具有相同的一致值(私有或公共读取)。
下面的注释导致我将表单操作修改为:http://MY_BUCKET.s3.amazonaws.com/,我得到了这个错误:
<Code>AccessDenied</Code>
<Message>
Invalid according to Policy: Policy Condition failed: ["eq", "$success_action_status", "201"]
</Message>奇怪的是,当我到AWS S3管理控制台并将一个文件上传到我的桶中时,它告诉我链接是‘桶,桶’表单。我之前和之后都添加了MY_BUCKET,但是仍然收到了相同的错误.
我不知道哪里发生了错误配置.我要创造一个新的桶看看我是否安装错了.
谢谢!
,它现在起作用了!,我修好了所有的答案.但不得不再做一次改变..。
我的表单有一个"success_action_redirect“字段,但是我的策略有一个success_action_status!
策略字段和表单字段必须匹配!啊!
谢谢你的帮助..。是时候进一步调整一下了!
发布于 2012-12-26 21:49:49
1/要上传文件,您必须使用这个url "http://#{bucket_name}.s3.amazonaws.com/"。这一点在文档中有明确的表述
该操作定义将处理请求的URL;必须将其设置为桶的URL。例如,如果您的桶名为"johnsmith",则URL将为"http://johnsmith.s3.amazonaws.com/“
[2]您必须有一致的策略,似乎您在签名中设置了public-read,在表单中设置了private。
发布于 2013-06-07 16:47:18
我得到以下错误:根据策略无效:策略条件失败:[\"eq\",\"$bucket\“
许多小时后,我了解到,你不能有一个桶大写字母。把水桶改为小写,把它修好。
https://stackoverflow.com/questions/14046441
复制相似问题