|
| 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