Skip to content

Commit

Permalink
resovles aws#1648 set content type multipart upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Voss committed Nov 22, 2021
1 parent a7ae671 commit 08b8736
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.internal.Mimetypes;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

Expand Down Expand Up @@ -537,6 +539,39 @@ public InitiateMultipartUploadRequest withObjectMetadata(ObjectMetadata objectMe
return this;
}

/**
* Sets the additional information of content type about the new object being created.
* Sets content type in the object metadata. Creates object metadata if does not exist.
*
* @param file
* File that would be uploaded. Uses file extension to determine
* mimetype.
*/
public void setObjectMetadataContentTypeFromFile(File file) {
if (this.objectMetadata == null) {
this.objectMetadata = new ObjectMetadata();
}
this.objectMetadata.setContentType(Mimetypes.getInstance().getMimetype(file));
}

/**
* Sets the additional information of content type about the new object being created.
* Sets content type in the object metadata. Creates object metadata if does not exist.
* <p>
* Returns this updated InitiateMultipartUploadRequest object so that
* additional method calls can be chained together.
*
* @param file
* File that would be uploaded. Uses file extension to determine
* mimetype.
*
* @return This updated InitiateMultipartUploadRequest object.
*/
public InitiateMultipartUploadRequest withObjectMetadataContentTypeFromFile(File file) {
setObjectMetadataContentTypeFromFile(file);
return this;
}

/**
* Sets the optional redirect location for the new object.
*
Expand Down Expand Up @@ -582,7 +617,7 @@ public SSECustomerKey getSSECustomerKey() {
public void setSSECustomerKey(SSECustomerKey sseKey) {
if (sseKey != null && this.sseAwsKeyManagementParams != null) {
throw new IllegalArgumentException(
"Either SSECustomerKey or SSEAwsKeyManagementParams must not be set at the same time.");
"Either SSECustomerKey or SSEAwsKeyManagementParams must not be set at the same time.");
}
this.sseCustomerKey = sseKey;
}
Expand Down Expand Up @@ -621,7 +656,7 @@ public SSEAwsKeyManagementParams getSSEAwsKeyManagementParams() {
public void setSSEAwsKeyManagementParams(SSEAwsKeyManagementParams params) {
if (params != null && this.sseCustomerKey != null) {
throw new IllegalArgumentException(
"Either SSECustomerKey or SSEAwsKeyManagementParams must not be set at the same time.");
"Either SSECustomerKey or SSEAwsKeyManagementParams must not be set at the same time.");
}
this.sseAwsKeyManagementParams = params;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.amazonaws.services.s3.model;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.io.File;

/**
* Test Model InitiateMultipartUploadRequest
*/
public class InitiateMultipartUploadRequestTest {

/**
* Test setting the content type of the metadata for the multipart upload request
*/
@Test
public void testWithObjectMetadataContentTypeFromFile(){
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("bucketName", "key", new ObjectMetadata());
File file = new File("textFile.txt");
request = request.withObjectMetadataContentTypeFromFile(file);
assertEquals("text/plain", request.getObjectMetadata().getContentType());
}

/**
* Test setting the content type of the metadata for the multipart upload request
* Creates the metadata object if it does not exist
*/
@Test
public void testWithObjectMetadataContentTypeFromFileCreatesMetadata(){
InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest("bucketName", "key");
File file = new File("textFile.txt");
request = request.withObjectMetadataContentTypeFromFile(file);
assertEquals("text/plain", request.getObjectMetadata().getContentType());
}
}

0 comments on commit 08b8736

Please sign in to comment.