You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何从S3预签名URL获取过期日期与时间?

Hey there! Let's walk through how to get the expiration time from your S3 presigned URL—both for your specific HTML file case and in general.

1. Extract Expiration Time from an Existing Presigned URL

S3 presigned URLs (using AWS Signature Version 4, the current default) include two key query parameters you can use to calculate the expiration time:

  • X-Amz-Date: The UTC timestamp when the signature was generated, formatted like YYYYMMDDTHHMMSSZ
  • X-Amz-Expires: The number of seconds the URL remains valid (in your case, that's 14400 seconds for 4 hours)

Example with Python

If you're comfortable with code, here's a quick script to parse the URL and compute the expiration time:

from urllib.parse import urlparse, parse_qs
from datetime import datetime, timedelta

# Replace with your actual presigned URL
presigned_url = "https://your-bucket.s3.your-region.amazonaws.com/your-file.html?..."

# Parse the URL's query parameters
parsed_url = urlparse(presigned_url)
query_params = parse_qs(parsed_url.query)

# Extract the required parameters
amz_date_str = query_params.get("X-Amz-Date", [""])[0]
expires_seconds = int(query_params.get("X-Amz-Expires", [0])[0])

# Calculate expiration time
signature_time = datetime.strptime(amz_date_str, "%Y%m%dT%H%M%SZ")
expiration_time = signature_time + timedelta(seconds=expires_seconds)

print(f"Expiration Time (UTC): {expiration_time.strftime('%Y-%m-%d %H:%M:%S UTC')}")

Command-Line Alternative (Linux/macOS)

If you prefer using terminal tools, you can parse the URL with awk and compute the time with date:

# Replace with your presigned URL
PRE_SIGNED_URL="https://your-bucket.s3.your-region.amazonaws.com/your-file.html?..."

# Extract X-Amz-Date and X-Amz-Expires
AMZ_DATE=$(echo "$PRE_SIGNED_URL" | awk -F'&' '{for(i=1;i<=NF;i++){if($i ~ /X-Amz-Date/){split($i,a,"=");print a[2]}}}')
EXPIRES_SEC=$(echo "$PRE_SIGNED_URL" | awk -F'&' '{for(i=1;i<=NF;i++){if($i ~ /X-Amz-Expires/){split($i,a,"=");print a[2]}}}')

# Calculate and print expiration time (UTC)
EXPIRATION_TIME=$(date -d "$AMZ_DATE + $EXPIRES_SEC seconds" -u +"%Y-%m-%d %H:%M:%S UTC")
echo "Expiration Time: $EXPIRATION_TIME"

Note for Older Signature Versions

If you're dealing with a legacy Signature Version 2 URL, it will have an Expires parameter instead—this is a Unix timestamp (e.g., 1716211200). You can convert this directly to a readable time using tools like date -d @<timestamp> -u.

2. Other Ways to Get Expiration Time (If You Generated the URL)

If you created the presigned URL yourself (e.g., via AWS SDKs like boto3), you can record the expiration time at generation time instead of parsing the URL later. Here's an example with boto3:

import boto3
from datetime import datetime, timedelta

s3_client = boto3.client("s3")
expires_in_seconds = 14400  # 4 hours

# Calculate expiration time upfront
expiration_time = datetime.utcnow() + timedelta(seconds=expires_in_seconds)

# Generate the presigned URL
presigned_url = s3_client.generate_presigned_url(
    "get_object",
    Params={"Bucket": "your-bucket", "Key": "your-file.html"},
    ExpiresIn=expires_in_seconds
)

# Store or print both the URL and expiration time
print(f"Presigned URL: {presigned_url}")
print(f"Expiration Time (UTC): {expiration_time.strftime('%Y-%m-%d %H:%M:%S UTC')}")
3. General Rule for S3 Presigned URLs
  • For Signature Version 4 (current standard): Combine the X-Amz-Date (signature generation time) with X-Amz-Expires (validity in seconds) to get the expiration time.
  • For Signature Version 2 (legacy): Use the Expires Unix timestamp directly.
  • Important: AWS does not store presigned URL metadata on their servers—so the only way to get the expiration time is either by parsing the URL itself, or recording it when you generate the URL.

内容的提问来源于stack exchange,提问作者vartika

火山引擎 最新活动