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

如何用AWS SDK CPP生成带response-content-disposition的S3预签名URL?

生成带response-content-disposition的S3预签名URL(AWS SDK CPP)

要生成附加response-content-disposition参数的预签名URL,你需要使用GeneratePresignedUrlRequest对象配置额外请求参数(而非简化的重载方法),具体实现如下:

完整代码示例

#include <aws/s3/S3Client.h>
#include <aws/s3/model/GeneratePresignedUrlRequest.h>

// 替换为你的实际参数
const Aws::String bucket = "your-bucket-name";
const Aws::String key = "your-object-key";
const std::chrono::seconds EXPIRATION_TIME(3600); // 1小时过期
const Aws::String custom_filename = "自定义下载文件名.pdf";

// 构建请求对象
Aws::S3::Model::GeneratePresignedUrlRequest request;
request.SetBucket(bucket);
request.SetKey(key);
request.SetExpires(EXPIRATION_TIME);
request.SetMethod(Aws::Http::HttpMethod::HTTP_GET);

// 添加response-content-disposition参数,指定下载文件名
Aws::Http::HeaderValueCollection query_params;
query_params.emplace("response-content-disposition", 
                     "attachment; filename=\"" + custom_filename + "\"");
request.SetQueryParameters(query_params);

// 生成预签名URL
Aws::S3::S3Client s3_client;
std::string presigned_url = s3_client.GeneratePresignedUrl(request);

关键说明

  • 必须使用GeneratePresignedUrlRequest:你之前调用的简化重载方法不支持添加自定义查询参数,必须通过请求对象扩展配置
  • 参数格式遵循HTTP规范:attachment触发浏览器下载行为,filename指定下载时显示的文件名
  • 自动处理特殊字符:如果文件名包含空格、中文或特殊符号,AWS SDK会自动完成URL编码,无需手动操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:49:57