Skip to content

fix(pre-signed): Add Content-Length header support to presigned URL requests #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/qcloud/cos/COSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <b>must</b> 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 <b>must</b> 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.
*
Expand Down