【问题标题】:Upload files to S3 using AWS S3 static web hosting使用 AWS S3 静态 Web 托管将文件上传到 S3
【发布时间】:2017-05-04 21:57:54
【问题描述】:

我们一直在研究将文件上传到 S3 的选项。我们正在研究使用 S3 静态 Web 托管的一种选择。在这个选项中,我们将有一个页面,我正在尝试编写一个带有上传选项的简单 Index.html 页面。我设置了以下存储桶策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Allow Public Access to All Objects",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::<bucketname>/*"
        }
    ]
} 

以下是CORS设置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>

以下是Index.html中的代码

<html>
<header><title>Hello World</title></header>
<body>
Hello world

<form action="/action_page.php">
  <input type="file" name="pic" accept="image/*">
  <input type="submit">
</form>
</body>
</html>

当我尝试使用虚拟主机 URI 访问我的 s3 存储桶时,我正在获取页面,当我尝试上传文档时,我收到以下错误:

404 Not Found

Code: NoSuchKey
Message: The specified key does not exist.
Key: action_page.php
RequestId: <requestid>
HostId: <hostid>
An Error Occurred While Attempting to Retrieve a Custom Error Document

Code: NoSuchKey
Message: The specified key does not exist.
Key: error.html

如果我添加 error.html 页面,它会显示我的错误页面。

现在我的想法是使用 S3 虚拟主机将文件上传到存储桶而不进行任何编码是正确的方法吗?我在这里错过了什么?

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    您无法在 S3 静态网站上运行任何 PHP 文件 - 这需要后端服务器,并且您的表单正试图发布到“action_page.php”。

    那根本行不通。

    这只是您可能如何进行的一个示例:

    在此示例中,一个简单的 HTML 页面提供了一个基于浏览器的应用程序,用于在您可以上传照片的 Amazon S3 存储桶中创建相册。该应用程序允许您删除您添加的照片和相册。

    http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 2020-07-24
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 2021-07-16
      相关资源
      最近更新 更多