From fcc16d623fdf907f43068b5f4c4c969f8473b20c Mon Sep 17 00:00:00 2001 From: TAN JIACHENG <113982129+LeoTan2004@users.noreply.github.com> Date: Tue, 25 Feb 2025 15:41:52 +0000 Subject: [PATCH] fix(pre-signed): Add Content-Length header support to presigned URL requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix issue 在生成COS 的预签名中,无法预设置Content-Length #233 --- src/main/java/com/qcloud/cos/COSClient.java | 6 ++- .../model/GeneratePresignedUrlRequest.java | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/qcloud/cos/COSClient.java b/src/main/java/com/qcloud/cos/COSClient.java index 9a73f725..9e4d4f07 100644 --- a/src/main/java/com/qcloud/cos/COSClient.java +++ b/src/main/java/com/qcloud/cos/COSClient.java @@ -629,9 +629,9 @@ private static void addParameterIfNotNull(CosHttpRequest request, String para * @param header The header name. * @param value The header value. */ - private static void addHeaderIfNotNull(CosHttpRequest request, String header, String value) { + private static void addHeaderIfNotNull(CosHttpRequest request, String header, Object value) { if (value != null) { - request.addHeader(header, value); + request.addHeader(header, value.toString()); } } @@ -3000,6 +3000,8 @@ public URL generatePresignedUrl(GeneratePresignedUrlRequest req, Boolean signHos addHeaderIfNotNull(request, Headers.CONTENT_TYPE, req.getContentType()); addHeaderIfNotNull(request, Headers.CONTENT_MD5, req.getContentMd5()); + addHeaderIfNotNull(request, Headers.CONTENT_LENGTH, req.getContentLength()); + // Custom headers that open up the possibility of supporting unexpected // cases. diff --git a/src/main/java/com/qcloud/cos/model/GeneratePresignedUrlRequest.java b/src/main/java/com/qcloud/cos/model/GeneratePresignedUrlRequest.java index 4eebb1ab..7f9156bd 100644 --- a/src/main/java/com/qcloud/cos/model/GeneratePresignedUrlRequest.java +++ b/src/main/java/com/qcloud/cos/model/GeneratePresignedUrlRequest.java @@ -44,6 +44,9 @@ public class GeneratePresignedUrlRequest extends CosServiceRequest { /** The optional Content-Type header that will be sent when the presigned URL is accessed */ private String contentType; + /** The optional Content-Length header that will be sent when the presigned URL is accessed */ + private Long contentLength; + /** The optional Content-MD5 header that will be sent when the presigned URL is accessed */ private String contentMd5; @@ -155,6 +158,51 @@ public GeneratePresignedUrlRequest withMethod(HttpMethodName method) { return this; } + /** + * The Content-Length HTTP header value to be used in this request. The + * same Content-Length header value must be used in the request when + * the pre-signed URL is used. + * If it was null, it means the Content-Length header was not set. + * + * @return The Content-Length HTTP header value to be used in this request. + * If it was null, it means the Content-Length header value is not set. + */ + public Long getContentLength() { + return contentLength; + } + + /** + * Sets the Content-Length HTTP header value to be used in this request. + * The same Content-Length header value must be used in the request + * when the pre-signed URL is used. + * If it was null, it means the Content-Length header was not set. + * + * @param contentLength The Content-Length HTTP header value to be used in this request. + */ + public void setContentLength(Long contentLength) { + + if(contentLength != null && contentLength < 0) { + throw new IllegalArgumentException("Content Length Can't be negative"); + } + this.contentLength = contentLength; + } + + /** + * Sets the Content-Length HTTP header value to be used in this request, and + * returns this request object to enable additional method calls to be chained + * together. + * + * @param contentLength + * The Content-Length HTTP header value to be used in this request. + * + * @return The updated request object, so that additional method calls can + * be chained together. + */ + public GeneratePresignedUrlRequest withContentLength(Long contentLength) { + setContentLength(contentLength);; + return this; + } + /** * Returns the name of the bucket involved in this request. *