You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过S3预签名URL上传对象并配置响应Cache-Control头

How to Generate a Presigned S3 Upload URL with Automatic Cache-Control Header

Got it, let's solve this exactly how you need it: generate a presigned URL for browser uploads, make sure the final S3 object has the Cache-Control: max-age=604800 header, and zero extra work required from the client. Here's the step-by-step breakdown:

Key Idea

When creating the presigned upload URL, you can embed the Cache-Control directive directly into the URL's parameters. Amazon S3 will automatically apply this header to the object once the upload completes—no need for the browser to send any special headers during upload.

Example Implementations

Using Python (Boto3)

If you're using Python on your backend, here's how to generate the presigned URL with the required cache control:

import boto3
from botocore.config import Config

s3_client = boto3.client('s3', config=Config(signature_version='s3v4'))

def generate_presigned_upload_url(bucket_name, object_key):
    # Define the Cache-Control header value we want
    cache_control = "max-age=604800"
    
    # Generate the presigned URL with the CacheControl parameter
    response = s3_client.generate_presigned_url(
        'put_object',
        Params={
            'Bucket': bucket_name,
            'Key': object_key,
            'CacheControl': cache_control  # This is the magic line
        },
        ExpiresIn=3600  # URL expires after 1 hour, adjust as needed
    )
    return response

Using Node.js (AWS SDK v3)

For Node.js backends, here's the equivalent code:

import { S3Client, PutObjectCommand, getSignedUrl } from "@aws-sdk/client-s3";

const s3Client = new S3Client({ region: "your-region" });

async function generatePresignedUploadUrl(bucketName, objectKey) {
    const cacheControl = "max-age=604800";
    
    const command = new PutObjectCommand({
        Bucket: bucketName,
        Key: objectKey,
        CacheControl: cacheControl
    });
    
    const signedUrl = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
    return signedUrl;
}

What Happens Next?

  1. Your backend generates the presigned URL with the embedded CacheControl parameter and sends it to the browser.
  2. The browser uses a standard PUT request to upload the file to this URL—no need to add any custom headers in the browser code.
  3. Once the upload finishes, S3 automatically sets the Cache-Control: max-age=604800 header on the object.
  4. When users request this object later, S3 will include this cache header in the response, exactly as you need.

Important Notes

  • IAM Permissions: Ensure the IAM user/role used to generate the presigned URL has the s3:PutObject permission for the target bucket/object.
  • Signature Version: Always use s3v4 signature version (as shown in the examples) for better security and compatibility, especially for regions that require it.
  • Expiration Time: Adjust the ExpiresIn value based on how long you want the presigned URL to be valid (3600 seconds = 1 hour is a common default).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:15:02