fix: update sitemap retrieval logic
- Change from using `get_object` to `head_object` to check if the sitemap file exists before attempting to retrieve it. - Implement a presigned URL for accessing the sitemap, which is valid for 15 minutes. - Set cache headers to allow for 1 hour of caching. - Improved logging for better error tracking and debugging. This change enhances the efficiency of sitemap retrieval by reducing unnecessary data transfer and provides a more secure way to access the sitemaps through presigned URLs. It also improves error handling by logging specific errors related to missing sitemaps.
This commit is contained in:
parent
3ae870047a
commit
fe5c0d5113
@ -12,26 +12,39 @@ class SitemapsController < ApplicationController
|
|||||||
Rails.logger.error "S3 Error: #{e.message}"
|
Rails.logger.error "S3 Error: #{e.message}"
|
||||||
render status: :internal_server_error
|
render status: :internal_server_error
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
path = params[:path]
|
path = params[:path]
|
||||||
Rails.logger.info "Sitemap: #{path}"
|
key = "sitemaps/#{path}"
|
||||||
|
|
||||||
|
Rails.logger.info "Requesting sitemap: #{path}"
|
||||||
|
|
||||||
begin
|
begin
|
||||||
response = s3_client.get_object(
|
# 检查文件是否存在
|
||||||
|
s3_client.head_object(
|
||||||
bucket: @bucket_name,
|
bucket: @bucket_name,
|
||||||
key: "sitemaps/#{path}"
|
key: key
|
||||||
)
|
)
|
||||||
|
|
||||||
expires_in 12.hours, public: true
|
# 生成预签名URL,设置15分钟有效期
|
||||||
content_type = response.content_type || "application/xml"
|
signer = Aws::S3::Presigner.new(client: s3_client)
|
||||||
|
url = signer.presigned_url(
|
||||||
send_data(
|
:get_object,
|
||||||
response.body.read,
|
bucket: @bucket_name,
|
||||||
filename: path,
|
key: key,
|
||||||
type: content_type,
|
expires_in: 15 * 60, # 15 minutes
|
||||||
disposition: "inline"
|
# response_content_type: 'application/xml', # 确保正确的内容类型
|
||||||
|
response_content_disposition: "inline" # 在浏览器中直接显示
|
||||||
)
|
)
|
||||||
rescue Aws::S3::Errors::NoSuchKey
|
|
||||||
|
# 设置缓存头
|
||||||
|
response.headers["Cache-Control"] = "public, max-age=3600" # 1小时缓存
|
||||||
|
|
||||||
|
# 重定向到预签名URL
|
||||||
|
redirect_to url, allow_other_host: true, status: :found
|
||||||
|
|
||||||
|
rescue Aws::S3::Errors::NotFound
|
||||||
|
Rails.logger.error "Sitemap not found: #{path}"
|
||||||
render status: :not_found
|
render status: :not_found
|
||||||
rescue Aws::S3::Errors::ServiceError => e
|
rescue Aws::S3::Errors::ServiceError => e
|
||||||
Rails.logger.error "S3 Error: #{e.message}"
|
Rails.logger.error "S3 Error: #{e.message}"
|
||||||
@ -39,6 +52,33 @@ class SitemapsController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def show
|
||||||
|
# path = params[:path]
|
||||||
|
# Rails.logger.info "Sitemap: #{path}"
|
||||||
|
|
||||||
|
# begin
|
||||||
|
# response = s3_client.get_object(
|
||||||
|
# bucket: @bucket_name,
|
||||||
|
# key: "sitemaps/#{path}"
|
||||||
|
# )
|
||||||
|
|
||||||
|
# expires_in 12.hours, public: true
|
||||||
|
# content_type = response.content_type || "application/xml"
|
||||||
|
|
||||||
|
# send_data(
|
||||||
|
# response.body.read,
|
||||||
|
# filename: path,
|
||||||
|
# type: content_type,
|
||||||
|
# disposition: "inline"
|
||||||
|
# )
|
||||||
|
# rescue Aws::S3::Errors::NoSuchKey
|
||||||
|
# render status: :not_found
|
||||||
|
# rescue Aws::S3::Errors::ServiceError => e
|
||||||
|
# Rails.logger.error "S3 Error: #{e.message}"
|
||||||
|
# render status: :internal_server_error
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_bucket_name
|
def set_bucket_name
|
||||||
|
Loading…
Reference in New Issue
Block a user