Skip to content

Commit ac24c5a

Browse files
liyanzhang505huiguangjun
authored andcommitted
support object with slash prefix
1 parent 8f4bc45 commit ac24c5a

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/main/java/com/aliyun/oss/internal/OSSUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static boolean validateObjectKey(String key) {
9292
// Validate exculde xml unsupported chars
9393
char keyChars[] = key.toCharArray();
9494
char firstChar = keyChars[0];
95-
if (firstChar == '/' || firstChar == '\\') {
95+
if (firstChar == '\\') {
9696
return false;
9797
}
9898

@@ -188,6 +188,10 @@ public static String makeResourcePath(String bucket, String key) {
188188
* Encode object URI.
189189
*/
190190
private static String urlEncodeKey(String key) {
191+
if (key.startsWith("/")) {
192+
return HttpUtil.urlEncode(key, DEFAULT_CHARSET_NAME);
193+
}
194+
191195
StringBuffer resultUri = new StringBuffer();
192196

193197
String[] keys = key.split("/");

src/test/java/com/aliyun/oss/common/utils/OSSUtilsTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testOSSUtils() {
3838

3939
assertEquals(OSSUtils.validateObjectKey(null), false);
4040
assertEquals(OSSUtils.validateObjectKey(""), false);
41-
assertEquals(OSSUtils.validateObjectKey("/key-with-invalid-prefix"), false);
41+
assertEquals(OSSUtils.validateObjectKey("/key-with-invalid-prefix"), true);
4242
assertEquals(OSSUtils.validateObjectKey("\\key-with-invalid-prefix"), false);
4343

4444
try {
@@ -58,12 +58,15 @@ public void testOSSUtils() {
5858
assertEquals(OSSUtils.makeResourcePath("bucket", "key"),"bucket/key");
5959
assertEquals(OSSUtils.makeResourcePath("bucket", null),"bucket/");
6060
assertEquals(OSSUtils.makeResourcePath(null, "key"),null);
61+
assertEquals(OSSUtils.makeResourcePath("key/123/"),"key/123/" );
62+
assertEquals(OSSUtils.makeResourcePath("/key/123/"),"%2Fkey%2F123%2F" );
63+
assertEquals(OSSUtils.makeResourcePath("bucket", "key/123/"),"bucket/key/123/" );
64+
assertEquals(OSSUtils.makeResourcePath("bucket", "/key/123/"),"bucket/%2Fkey%2F123%2F" );
6165

6266
assertEquals(OSSUtils.trimQuotes(null),null);
6367
assertEquals(OSSUtils.trimQuotes("\"test"),"test");
6468
assertEquals(OSSUtils.trimQuotes("test\""),"test");
6569
assertEquals(OSSUtils.trimQuotes("test"),"test");
6670
assertEquals(OSSUtils.trimQuotes("\"test\""),"test");
67-
6871
}
6972
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.aliyun.oss.integrationtests;
21+
22+
import com.aliyun.oss.HttpMethod;
23+
import com.aliyun.oss.ServiceException;
24+
import com.aliyun.oss.common.utils.DateUtil;
25+
import com.aliyun.oss.common.utils.HttpHeaders;
26+
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
27+
import junit.framework.Assert;
28+
import org.junit.Test;
29+
import java.io.ByteArrayInputStream;
30+
import java.io.InputStream;
31+
import java.net.URL;
32+
import java.util.Date;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import static com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream;
36+
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_OBJECT_CONTENT_TYPE;
37+
38+
39+
public class ObjectWithPrefixSlashTest extends TestBase {
40+
41+
@Test
42+
public void testPutObject() {
43+
String objectName = "/abc中/put-object-slash-prefix/";
44+
try {
45+
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream("123".getBytes()));
46+
} catch (ServiceException e) {
47+
Assert.assertEquals(e.getErrorCode(), "InvalidObjectName");
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
Assert.fail(e.getMessage());
51+
}
52+
}
53+
54+
@Test
55+
public void testPutObjectWithSignedUrl() {
56+
final String key = "/abc中/put-object-prefix-slash-by-urlSignature";
57+
58+
final String expirationString = "Sun, 12 Apr 2022 12:00:00 GMT";
59+
final long inputStreamLength = 1024;
60+
61+
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
62+
try {
63+
Date expiration = DateUtil.parseRfc822Date(expirationString);
64+
request.setExpiration(expiration);
65+
request.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
66+
request.addHeader("x-oss-head1", "value");
67+
request.addHeader("abc", "value");
68+
request.addQueryParameter("param1", "value1");
69+
URL signedUrl = ossClient.generatePresignedUrl(request);
70+
71+
Map<String, String> requestHeaders = new HashMap<String, String>();
72+
73+
requestHeaders.put("x-oss-head1", "value");
74+
requestHeaders.put("abc", "value");
75+
requestHeaders.put(HttpHeaders.CONTENT_TYPE, DEFAULT_OBJECT_CONTENT_TYPE);
76+
InputStream instream = genFixedLengthInputStream(inputStreamLength);
77+
// Using url signature & chunked encoding to upload specified inputstream.
78+
ossClient.putObject(signedUrl, instream, -1, requestHeaders, true);
79+
} catch (ServiceException e) {
80+
Assert.assertEquals(e.getErrorCode(), "InvalidObjectName");
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
Assert.fail(e.getMessage());
84+
}
85+
}
86+
87+
}

0 commit comments

Comments
 (0)