Skip to content

Commit

Permalink
api: putObject() should properly save contentType. (#465)
Browse files Browse the repository at this point in the history
Current code was not working when contentType is set
to a custom value.

Fixes #464
  • Loading branch information
harshavardhana authored and balamurugana committed Oct 7, 2016
1 parent abdafb4 commit 1284591
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2065,8 +2065,9 @@ public void putObject(String bucketName, String objectName, InputStream stream,
* @param uploadId Upload ID of multipart put object.
* @param partNumber Part number of multipart put object.
*/
private String putObject(String bucketName, String objectName, int length, Object data,
String uploadId, int partNumber)
private String putObject(String bucketName, String objectName, int length,
Object data, String uploadId, String contentType,
int partNumber)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException {
Expand All @@ -2076,8 +2077,14 @@ private String putObject(String bucketName, String objectName, int length, Objec
queryParamMap.put("partNumber", Integer.toString(partNumber));
queryParamMap.put(UPLOAD_ID, uploadId);
}

HttpResponse response = executePut(bucketName, objectName, null, queryParamMap, data, length);
Map<String,String> headerMap = new HashMap<>();
if (contentType != null) {
headerMap.put("Content-Type", contentType);
} else {
headerMap.put("Content-Type", "application/octet-stream");
}
HttpResponse response = executePut(bucketName, objectName, headerMap,
queryParamMap, data, length);
return response.header().etag();
}

Expand All @@ -2098,8 +2105,8 @@ private void putObject(String bucketName, String objectName, String contentType,
InternalException,
InvalidArgumentException, InsufficientDataException {
if (size <= MIN_MULTIPART_SIZE) {
// single put object
putObject(bucketName, objectName, (int) size, data, null, 0);
// Single put object.
putObject(bucketName, objectName, (int) size, data, null, contentType, 0);
return;
}

Expand Down Expand Up @@ -2146,7 +2153,7 @@ private void putObject(String bucketName, String objectName, String contentType,
}
}

String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, partNumber);
String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, null, partNumber);
totalParts[partNumber - 1] = new Part(partNumber, etag);
}

Expand Down
24 changes: 14 additions & 10 deletions src/main/java/io/minio/ObjectStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public boolean equals(Object o) {

if (length != that.length) {
return false;
}
}
if (!bucketName.equals(that.bucketName)) {
return false;
}
Expand Down Expand Up @@ -97,6 +97,13 @@ public int hashCode() {
}


/**
* Returns bucket name.
*/
public String bucketName() {
return bucketName;
}

/**
* Returns object name.
*/
Expand All @@ -120,22 +127,19 @@ public long length() {
return length;
}


/**
* Returns bucket name.
*/
public String bucketName() {
return bucketName;
}


/**
* Returns ETag.
*/
public String etag() {
return etag;
}

/**
* Returns content type of object.
*/
public String contentType() {
return contentType;
}

/**
* Returns ObjectStat as string.
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/io/minio/MinioClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class MinioClientTest {
private static final String BUCKET = "bucket";
private static final String CONTENT_LENGTH = "Content-Length";
private static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
private static final String APPLICATION_JAVASCRIPT = "application/javascript";
private static final String CONTENT_TYPE = "Content-Type";
private static final String MON_04_MAY_2015_07_58_51_GMT = "Mon, 04 May 2015 07:58:51 GMT";
private static final String LAST_MODIFIED = "Last-Modified";
Expand Down Expand Up @@ -614,6 +615,28 @@ public void testNullContentTypeWorks()
client.putObject(BUCKET, "key", data, 11, null);
}

@Test
public void testCustomContentTypeWorks()
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {
MockWebServer server = new MockWebServer();
MockResponse response = new MockResponse();

response.addHeader("Date", SUN_29_JUN_2015_22_01_10_GMT);
response.addHeader(LAST_MODIFIED, MON_04_MAY_2015_07_58_51_UTC);
response.addHeader("ETag", MD5_HASH_STRING);
response.setResponseCode(200);

server.enqueue(response);
server.start();

MinioClient client = new MinioClient(server.url(""));

String inputString = HELLO_WORLD;
ByteArrayInputStream data = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));

client.putObject(BUCKET, "key", data, 11, APPLICATION_JAVASCRIPT);
}

@Test
public void testSigningKey()
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {
Expand Down
7 changes: 6 additions & 1 deletion test/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class FunctionalTest {
public static final int MB = 1024 * 1024;
public static final SecureRandom random = new SecureRandom();
public static final String bucketName = getRandomName();
public static final String customContenType = "application/javascript";
public static String endpoint;
public static String accessKey;
public static String secretKey;
Expand Down Expand Up @@ -193,9 +194,13 @@ public static void putObject_test4() throws Exception {
println("Test: putObject(String bucketName, String objectName, String contentType, long size, InputStream body)");
String fileName = createFile(3 * MB);
InputStream is = Files.newInputStream(Paths.get(fileName));
client.putObject(bucketName, fileName, is, 1024 * 1024, null);
client.putObject(bucketName, fileName, is, 1024 * 1024, customContenType);
is.close();
Files.delete(Paths.get(fileName));
ObjectStat objectStat = client.statObject(bucketName, fileName);
if (!customContenType.equals(objectStat.contentType())) {
println("FAILED");
}
client.removeObject(bucketName, fileName);
}

Expand Down

0 comments on commit 1284591

Please sign in to comment.