Skip to content

Commit 1284591

Browse files
harshavardhanabalamurugana
authored andcommitted
api: putObject() should properly save contentType. (#465)
Current code was not working when contentType is set to a custom value. Fixes #464
1 parent abdafb4 commit 1284591

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

src/main/java/io/minio/MinioClient.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,8 +2065,9 @@ public void putObject(String bucketName, String objectName, InputStream stream,
20652065
* @param uploadId Upload ID of multipart put object.
20662066
* @param partNumber Part number of multipart put object.
20672067
*/
2068-
private String putObject(String bucketName, String objectName, int length, Object data,
2069-
String uploadId, int partNumber)
2068+
private String putObject(String bucketName, String objectName, int length,
2069+
Object data, String uploadId, String contentType,
2070+
int partNumber)
20702071
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
20712072
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
20722073
InternalException {
@@ -2076,8 +2077,14 @@ private String putObject(String bucketName, String objectName, int length, Objec
20762077
queryParamMap.put("partNumber", Integer.toString(partNumber));
20772078
queryParamMap.put(UPLOAD_ID, uploadId);
20782079
}
2079-
2080-
HttpResponse response = executePut(bucketName, objectName, null, queryParamMap, data, length);
2080+
Map<String,String> headerMap = new HashMap<>();
2081+
if (contentType != null) {
2082+
headerMap.put("Content-Type", contentType);
2083+
} else {
2084+
headerMap.put("Content-Type", "application/octet-stream");
2085+
}
2086+
HttpResponse response = executePut(bucketName, objectName, headerMap,
2087+
queryParamMap, data, length);
20812088
return response.header().etag();
20822089
}
20832090

@@ -2098,8 +2105,8 @@ private void putObject(String bucketName, String objectName, String contentType,
20982105
InternalException,
20992106
InvalidArgumentException, InsufficientDataException {
21002107
if (size <= MIN_MULTIPART_SIZE) {
2101-
// single put object
2102-
putObject(bucketName, objectName, (int) size, data, null, 0);
2108+
// Single put object.
2109+
putObject(bucketName, objectName, (int) size, data, null, contentType, 0);
21032110
return;
21042111
}
21052112

@@ -2146,7 +2153,7 @@ private void putObject(String bucketName, String objectName, String contentType,
21462153
}
21472154
}
21482155

2149-
String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, partNumber);
2156+
String etag = putObject(bucketName, objectName, expectedReadSize, data, uploadId, null, partNumber);
21502157
totalParts[partNumber - 1] = new Part(partNumber, etag);
21512158
}
21522159

src/main/java/io/minio/ObjectStat.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public boolean equals(Object o) {
6464

6565
if (length != that.length) {
6666
return false;
67-
}
67+
}
6868
if (!bucketName.equals(that.bucketName)) {
6969
return false;
7070
}
@@ -97,6 +97,13 @@ public int hashCode() {
9797
}
9898

9999

100+
/**
101+
* Returns bucket name.
102+
*/
103+
public String bucketName() {
104+
return bucketName;
105+
}
106+
100107
/**
101108
* Returns object name.
102109
*/
@@ -120,22 +127,19 @@ public long length() {
120127
return length;
121128
}
122129

123-
124-
/**
125-
* Returns bucket name.
126-
*/
127-
public String bucketName() {
128-
return bucketName;
129-
}
130-
131-
132130
/**
133131
* Returns ETag.
134132
*/
135133
public String etag() {
136134
return etag;
137135
}
138136

137+
/**
138+
* Returns content type of object.
139+
*/
140+
public String contentType() {
141+
return contentType;
142+
}
139143

140144
/**
141145
* Returns ObjectStat as string.

src/test/java/io/minio/MinioClientTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class MinioClientTest {
5454
private static final String BUCKET = "bucket";
5555
private static final String CONTENT_LENGTH = "Content-Length";
5656
private static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
57+
private static final String APPLICATION_JAVASCRIPT = "application/javascript";
5758
private static final String CONTENT_TYPE = "Content-Type";
5859
private static final String MON_04_MAY_2015_07_58_51_GMT = "Mon, 04 May 2015 07:58:51 GMT";
5960
private static final String LAST_MODIFIED = "Last-Modified";
@@ -614,6 +615,28 @@ public void testNullContentTypeWorks()
614615
client.putObject(BUCKET, "key", data, 11, null);
615616
}
616617

618+
@Test
619+
public void testCustomContentTypeWorks()
620+
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {
621+
MockWebServer server = new MockWebServer();
622+
MockResponse response = new MockResponse();
623+
624+
response.addHeader("Date", SUN_29_JUN_2015_22_01_10_GMT);
625+
response.addHeader(LAST_MODIFIED, MON_04_MAY_2015_07_58_51_UTC);
626+
response.addHeader("ETag", MD5_HASH_STRING);
627+
response.setResponseCode(200);
628+
629+
server.enqueue(response);
630+
server.start();
631+
632+
MinioClient client = new MinioClient(server.url(""));
633+
634+
String inputString = HELLO_WORLD;
635+
ByteArrayInputStream data = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));
636+
637+
client.putObject(BUCKET, "key", data, 11, APPLICATION_JAVASCRIPT);
638+
}
639+
617640
@Test
618641
public void testSigningKey()
619642
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {

test/FunctionalTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class FunctionalTest {
4343
public static final int MB = 1024 * 1024;
4444
public static final SecureRandom random = new SecureRandom();
4545
public static final String bucketName = getRandomName();
46+
public static final String customContenType = "application/javascript";
4647
public static String endpoint;
4748
public static String accessKey;
4849
public static String secretKey;
@@ -193,9 +194,13 @@ public static void putObject_test4() throws Exception {
193194
println("Test: putObject(String bucketName, String objectName, String contentType, long size, InputStream body)");
194195
String fileName = createFile(3 * MB);
195196
InputStream is = Files.newInputStream(Paths.get(fileName));
196-
client.putObject(bucketName, fileName, is, 1024 * 1024, null);
197+
client.putObject(bucketName, fileName, is, 1024 * 1024, customContenType);
197198
is.close();
198199
Files.delete(Paths.get(fileName));
200+
ObjectStat objectStat = client.statObject(bucketName, fileName);
201+
if (!customContenType.equals(objectStat.contentType())) {
202+
println("FAILED");
203+
}
199204
client.removeObject(bucketName, fileName);
200205
}
201206

0 commit comments

Comments
 (0)