我一直在尝试在使用https的网站上实现ajax上传到Amazon的S3,所以当然我也必须上传到S3的安全版本,https://bucket.name.s3.amazonaws.com 。但是,当我尝试此上传时,最终收到错误消息,指出该网站的安全证书无效。根据我的阅读,这是预期的,因为亚马逊的 s3 证书仅涵盖一级子域( http://shlomoswidler.com/2009/08/amazon-s3-gotcha-using-virtual-host.html )。因此,我将要上传的 URL 更改为 https://s3.amazonaws.com/bucket.name ,从我也读过的内容来看,应该相当于 https://bucket.name.s3.amazonaws.com 。然而,尝试上传时出现了 301 永久重定向错误。我知道我的代码确实有效,因为当我尝试上传到名称中没有点的存储桶时,到 https://bucket.s3.amazonaws.com ,一切顺利,但当我尝试 https://s3.amazonaws.com/bucket 时,它也得到 301 。
<form id="form">
<input type="hidden" name="key" value="..." />
<input type="hidden" name="acl" value="public-read" />
<input type="hidden" name="X-Amz-Credential" value="..." />
<input type="hidden" name="X-Amz-Algorithm" value="..." />
<input type="hidden" name="X-Amz-Date" value="..." />
<input type="hidden" name="Policy" value="..." />
<input type="hidden" name="X-Amz-Signature" value="..." />
<input type="hidden" name="success_action_redirect" value="..." />
<input id="upload" type="submit" value="Upload" class="submit-upload clearfix button blue save"/>
</form>
<script type="text/javascript">
document.getElementById('upload').addEventListener('click', function (e) {
e.preventDefault();
var formData = new FormData(document.getElementById('form'));
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://s3.amazonaws.com/bucket.name/', true);
xhr.send(formData);
});
</script>
所以我的问题是:有没有办法通过 https 实现 ajax 上传到名称中带有点的 S3 存储桶?
最佳答案
关于http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
Amazon S3 supports virtual hosted-style and path-style access in all regions. The path-style syntax, however, requires that you use the region-specific endpoint when attempting to access a bucket. For example, if you have a bucket called mybucket that resides in the EU, you want to use path-style syntax, and the object is named puppy.jpg, the correct URI is http://s3-eu-west-1.amazonaws.com/mybucket/puppy.jpg. You will receive a "PermanentRedirect" error, an HTTP response code 301, and a message indicating what the correct URI is for your resource if you try to access a bucket outside the US East (N. Virginia) region with path-style syntax that uses either of the following:
An endpoint for a region different from the one where the bucket resides, for example, http://s3-eu-west-1.amazonaws.com for a bucket that was created in the US West (Northern California) region
关于amazon-s3 - S3 在 https 上上传,存储桶名称中带有点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35705616/