问题
Is it possible to configure S3 bucket to return a default object when the requested object is not found/available? I don't want to return any kind of 403 or 404 error.
回答1:
[EDITED TO REFLECT COMMENTS BELOW]
In standard mode, Amazon S3 can not be configured to return a default object when the requested object is not available.
The default behaviour is to return an HTTP 403 when the object does not exist
# existing object
$ curl -I http://s3-eu-west-1.amazonaws.com/public-sst/wifi.jpg
HTTP/1.1 200 OK
x-amz-id-2: K8PxI6YUES0Ua2/rPaLwKhmhZv/lVMbWnvagmfQpzs2BI6NNyHplTB4ROaYvIXCWtE/S+xXIImk=
x-amz-request-id: 9853D002E3968F0D
Date: Mon, 16 Feb 2015 11:08:56 GMT
Last-Modified: Sun, 30 Nov 2014 21:15:06 GMT
ETag: "553353dcf5d98841970e95a6d1af0741"
Accept-Ranges: bytes
Content-Type: image/jpeg
Content-Length: 113572
Server: AmazonS3
# non existing object
$ curl -I http://s3-eu-west-1.amazonaws.com/public-sst/wifi.png
HTTP/1.1 403 Forbidden
x-amz-request-id: F78E2F047DC09F00
x-amz-id-2: wydGOgs8aHMn1qc5MLbNjKGKyqshpBhA0kCgWX0zHUm0fXFRU7Z1eWdPKgtkv2ZVtPPhAqP+O+k=
Content-Type: application/xml
Transfer-Encoding: chunked
Date: Mon, 16 Feb 2015 11:08:59 GMT
Server: AmazonS3
When Amazon S3 is configured in web site hosting mode (as per http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html), the default behaviour is different :
- HTTP 403 is returned when the object is not publicly readable
- HTTP 404 is returned when the object or the bucket does not exist or when the bucket is not configured as a web site.
Amazon S3 in Web Hosting Mode can be configured to return a custom error page instead of the default HTML HTTP 404 error generated by Amazon S3.
As the original question also mentioned HTTP 403 : there is no way, using Amazon S3 only to configure Amazon S3 to return an custom error page on HTTP 403. Details are at http://docs.aws.amazon.com/AmazonS3/latest/dev/CustomErrorDocSupport.html
To return a custom error page on both HTTP 403 and 404, you can use CloudFront in front of your Amazon S3 bucket.
http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesErrorPages
Cloudfront can be configured to return a custom error page for any HTTP 4xx or HTTP 5xx error returned by Amazon S3.
CloudFront Distributions comes at no additional cost, you just pay for the bandwidth coming out of CloudFront. CloudFront outgoing bandwidth cost less ($0.085/Gb) than S3 outgoing bandwidth ($0.09 /Gb), so using CloudFront should be a no brainer.
来源:https://stackoverflow.com/questions/28523693/return-a-default-object-without-error-when-requested-object-is-not-found-from