html - Amazon s3 文件上传到存储桶成功后不重定向

标签 html asp.net-mvc-4 file-upload amazon-s3 url-redirection

我一直在努力将文件直接从 html 表单上传到 Amazon S3 存储桶并且已经开始工作,但它在成功上传后没有重定向。我正在发送“success_action_redirect”字段,并返回 204 状态代码(耶)。我的想法是它没有识别到​​本地主机(我正在测试的地方)的重定向,但即使我将 www.google.com 作为重定向,它也不会重定向。

这是我编码到我的签名中的 json(日期是临时的):

{
      "expiration": "2014-01-01T00:00:00Z", 
      "conditions": 
      [ 
           {"bucket": "mybucket"}, 
           ["starts-with", $key, "tests/" ], 
           {"success_action_redirect": "www.google.com"}, 
           {"acl": "public-read" } 
      ] 
}

这是我的 html:

<form id="fileform" action="https://mybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data" >
    <input type="hidden" name="key" value="@key" />
    <div class="editor-area">
        <label for="file">File</label>
        <input type="file" name="file" />
    </div>

    <input type="hidden" name="success_action_redirect" value="www.google.com" />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="AWSAccessKeyId" value="@awsAccessKeyId" />
    <input type="hidden" name="policy" value="@b64Policy" />
    <input type="hidden" name="signature" value="@signature" />

    <button id="file-button">Save File</button>
</form>

这是有效的,它只是没有进行重定向(我的 CORS 设置都设置为接受帖子)。是否有必须设置的 CORS 设置才能允许重定向?

如果有人能看出我可能做错了什么,请赐教!

最佳答案

我遇到了同样的问题,很遗憾,您的问题没有得到解答。但是,在重新阅读文档后,我解决了在成功上传到您的 S3 存储桶后未发生重定向的问题。事实证明,我错过了 S3 文档中的一个小片段,看起来它也可能导致您的问题...

http://docs.aws.amazon.com/AmazonS3/latest/dev/HTTPPOSTForms.html#HTTPPOSTFormFields

The file or content must be the last field in the form. Any fields below it are ignored.

在我的表单中,我在文件输入字段之后有一个隐藏的重定向输入字段,因此 S3 忽略了它。在我的文件字段之前移动隐藏的重定向输入字段解决了这个问题。现在我的帖子重定向到指定的重定向。

我的错误表格:

<form action="http://<bucketName>.s3.amazonaws.com/" method="post"
    enctype="multipart/form-data">
    <input type="hidden" name="key" value="${filename}" />
    <input type="file" name="file" />

    <!-- the following fields are ignored as they are after the file field -->
    <input type="hidden" name="success_action_redirect" 
        value="http://www.google.com/" />
    <input type="submit" value="send to aws" />
</form>

我更正后的工作形式:

<form action="http://<bucketName>.s3.amazonaws.com/" method="post"
    enctype="multipart/form-data">
    <input type="hidden" name="key" value="${filename}" />
    <input type="hidden" name="success_action_redirect" 
        value="http://www.google.com/" />
    <input type="file" name="file" />
    <input type="submit" value="send to aws" />
</form>

因此,对于您的示例,我将尝试以下内容:

<form id="fileform" action="https://mybucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data" >
    <input type="hidden" name="key" value="@key" />
    <input type="hidden" name="success_action_redirect" value="http://www.google.com/" />
    <input type="hidden" name="acl" value="public-read" />
    <input type="hidden" name="AWSAccessKeyId" value="@awsAccessKeyId" />
    <input type="hidden" name="policy" value="@b64Policy" />
    <input type="hidden" name="signature" value="@signature" />

    <div class="editor-area">
        <label for="file">File</label>
        <input type="file" name="file" />
    </div>

    <button id="file-button">Save File</button>
</form>

注意:我确实更新了 URL 以包含协议(protocol)

关于html - Amazon s3 文件上传到存储桶成功后不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20435823/

相关文章:

c# - 如何在 Controller 中使用 POST 参数重定向到外部 URL

C++ libcurl FTP 通过代理上传不工作

java - 使用java刷新文件系统

java - 上传文件时处理网络问题

javascript - 如何使用javascript将HTML用户输入数据存储到excel中

javascript - 我可以从输入字段作为数据 URL 获取的最大文件大小是多少?

asp.net - 获取在asp.net Identity 2.0中分配角色的用户列表

c# - ASP.NET MVC 4 jQuery 验证脚本包不工作

html - 使背面图像模糊和正面图像清晰可见

html - 元素收缩和增长与CSS