Skip to content

Commit 7cb7b5b

Browse files
committed
Support head bucket
1 parent 855ceb3 commit 7cb7b5b

File tree

7 files changed

+244
-0
lines changed

7 files changed

+244
-0
lines changed

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ public interface OSS {
177177
*/
178178
public AccessControlList getBucketAcl(GenericRequest genericRequest) throws OSSException, ClientException;
179179

180+
/**
181+
* Gets the metadata of {@link Bucket}.
182+
*
183+
* @param bucketName
184+
* Bucket name.
185+
*
186+
* @return The {@link BucketMetadata} instance.
187+
*/
188+
public BucketMetadata getBucketMetadata(String bucketName) throws OSSException, ClientException;
189+
190+
/**
191+
* Gets all the metadata of {@link Bucket}.
192+
*
193+
* @param genericRequest
194+
* Generic request which specifies the bucket name.
195+
*
196+
* @return The {@link BucketMetadata} instance.
197+
*
198+
*/
199+
public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException;
200+
180201
/**
181202
* Sets the http referer on the {@link Bucket} instance specified by the
182203
* bucket name.

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ public AccessControlList getBucketAcl(GenericRequest genericRequest) throws OSSE
384384
return bucketOperation.getBucketAcl(genericRequest);
385385
}
386386

387+
@Override
388+
public BucketMetadata getBucketMetadata(String bucketName) throws OSSException, ClientException {
389+
return this.getBucketMetadata(new GenericRequest(bucketName));
390+
}
391+
392+
@Override
393+
public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
394+
return bucketOperation.getBucketMetadata(genericRequest);
395+
}
396+
387397
@Override
388398
public void setBucketReferer(String bucketName, BucketReferer referer) throws OSSException, ClientException {
389399
this.setBucketReferer(new SetBucketRefererRequest(bucketName, referer));

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
import static com.aliyun.oss.common.parser.RequestMarshallers.setBucketQosRequestMarshaller;
3535
import static com.aliyun.oss.common.parser.RequestMarshallers.bucketImageProcessConfMarshaller;
3636
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
37+
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
3738
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
39+
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
3840
import static com.aliyun.oss.internal.RequestParameters.DELIMITER;
3941
import static com.aliyun.oss.internal.RequestParameters.ENCODING_TYPE;
4042
import static com.aliyun.oss.internal.RequestParameters.MARKER;
@@ -74,6 +76,7 @@
7476
import static com.aliyun.oss.internal.ResponseParsers.getBucketImageProcessConfResponseParser;
7577

7678
import java.io.ByteArrayInputStream;
79+
import java.util.ArrayList;
7780
import java.util.HashMap;
7881
import java.util.LinkedHashMap;
7982
import java.util.List;
@@ -83,17 +86,21 @@
8386
import com.aliyun.oss.HttpMethod;
8487
import com.aliyun.oss.OSSErrorCode;
8588
import com.aliyun.oss.OSSException;
89+
import com.aliyun.oss.ServiceException;
8690
import com.aliyun.oss.common.auth.CredentialsProvider;
8791
import com.aliyun.oss.common.comm.RequestMessage;
92+
import com.aliyun.oss.common.comm.ResponseHandler;
8893
import com.aliyun.oss.common.comm.ResponseMessage;
8994
import com.aliyun.oss.common.comm.ServiceClient;
9095
import com.aliyun.oss.common.utils.BinaryUtil;
96+
import com.aliyun.oss.common.utils.ExceptionFactory;
9197
import com.aliyun.oss.common.utils.HttpHeaders;
9298
import com.aliyun.oss.model.AccessControlList;
9399
import com.aliyun.oss.model.Bucket;
94100
import com.aliyun.oss.model.BucketInfo;
95101
import com.aliyun.oss.model.BucketList;
96102
import com.aliyun.oss.model.BucketLoggingResult;
103+
import com.aliyun.oss.model.BucketMetadata;
97104
import com.aliyun.oss.model.BucketProcess;
98105
import com.aliyun.oss.model.BucketReferer;
99106
import com.aliyun.oss.model.BucketReplicationProgress;
@@ -129,6 +136,7 @@
129136
import com.aliyun.oss.model.TagSet;
130137
import com.aliyun.oss.model.Style;
131138
import com.aliyun.oss.model.UserQos;
139+
import org.apache.http.HttpStatus;
132140

133141
/**
134142
* Bucket operation.
@@ -264,6 +272,34 @@ public AccessControlList getBucketAcl(GenericRequest genericRequest) throws OSSE
264272
return doOperation(request, getBucketAclResponseParser, bucketName, null, true);
265273
}
266274

275+
public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
276+
277+
assertParameterNotNull(genericRequest, "genericRequest");
278+
279+
String bucketName = genericRequest.getBucketName();
280+
assertParameterNotNull(bucketName, "bucketName");
281+
ensureBucketNameValid(bucketName);
282+
283+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
284+
.setMethod(HttpMethod.HEAD).setBucket(bucketName).setOriginalRequest(genericRequest).build();
285+
286+
List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
287+
reponseHandlers.add(new ResponseHandler() {
288+
@Override
289+
public void handle(ResponseMessage response) throws ServiceException, ClientException {
290+
if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
291+
safeCloseResponse(response);
292+
throw ExceptionFactory.createOSSException(
293+
response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_BUCKET,
294+
OSS_RESOURCE_MANAGER.getString("NoSuchBucket"));
295+
}
296+
}
297+
});
298+
299+
return doOperation(request, ResponseParsers.getBucketMetadataResponseParser, bucketName, null, true, null,
300+
reponseHandlers);
301+
}
302+
267303
/**
268304
* Set bucket referer.
269305
*/

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ public interface OSSHeaders extends HttpHeaders {
7878
static final String OSS_STORAGE_CLASS = "x-oss-storage-class";
7979
static final String OSS_RESTORE = "x-oss-restore";
8080
static final String OSS_ONGOING_RESTORE = "ongoing-request=\"true\"";
81+
82+
static final String OSS_BUCKET_REGION = "x-oss-bucket-region";
8183
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.aliyun.oss.model.BucketInfo;
5151
import com.aliyun.oss.model.BucketList;
5252
import com.aliyun.oss.model.BucketLoggingResult;
53+
import com.aliyun.oss.model.BucketMetadata;
5354
import com.aliyun.oss.model.BucketProcess;
5455
import com.aliyun.oss.model.BucketReferer;
5556
import com.aliyun.oss.model.BucketReplicationProgress;
@@ -121,6 +122,7 @@ public final class ResponseParsers {
121122
public static final ListImageStyleResponseParser listImageStyleResponseParser = new ListImageStyleResponseParser();
122123
public static final GetBucketRefererResponseParser getBucketRefererResponseParser = new GetBucketRefererResponseParser();
123124
public static final GetBucketAclResponseParser getBucketAclResponseParser = new GetBucketAclResponseParser();
125+
public static final GetBucketMetadataResponseParser getBucketMetadataResponseParser = new GetBucketMetadataResponseParser();
124126
public static final GetBucketLocationResponseParser getBucketLocationResponseParser = new GetBucketLocationResponseParser();
125127
public static final GetBucketLoggingResponseParser getBucketLoggingResponseParser = new GetBucketLoggingResponseParser();
126128
public static final GetBucketWebsiteResponseParser getBucketWebsiteResponseParser = new GetBucketWebsiteResponseParser();
@@ -236,6 +238,19 @@ public AccessControlList parse(ResponseMessage response) throws ResponseParseExc
236238
}
237239

238240
}
241+
242+
public static final class GetBucketMetadataResponseParser implements ResponseParser<BucketMetadata> {
243+
244+
@Override
245+
public BucketMetadata parse(ResponseMessage response) throws ResponseParseException {
246+
try {
247+
return parseBucketMetadata(response.getHeaders());
248+
} finally {
249+
safeCloseResponse(response);
250+
}
251+
}
252+
253+
}
239254

240255
public static final class GetBucketLocationResponseParser implements ResponseParser<String> {
241256

@@ -1256,6 +1271,30 @@ public static String parseGetBucketLocation(InputStream responseBody) throws Res
12561271

12571272
}
12581273

1274+
/**
1275+
* Unmarshall bucket metadata from response headers.
1276+
*/
1277+
public static BucketMetadata parseBucketMetadata(Map<String, String> headers) throws ResponseParseException {
1278+
1279+
try {
1280+
BucketMetadata bucketMetadata = new BucketMetadata();
1281+
1282+
for (Iterator<String> it = headers.keySet().iterator(); it.hasNext();) {
1283+
String key = it.next();
1284+
1285+
if (key.equals(OSSHeaders.OSS_BUCKET_REGION)) {
1286+
bucketMetadata.setBucketRegion(headers.get(key));
1287+
} else {
1288+
bucketMetadata.addHttpMetadata(key, headers.get(key));
1289+
}
1290+
}
1291+
1292+
return bucketMetadata;
1293+
} catch (Exception e) {
1294+
throw new ResponseParseException(e.getMessage(), e);
1295+
}
1296+
}
1297+
12591298
/**
12601299
* Unmarshall simplified object meta from response headers.
12611300
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.model;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import com.aliyun.oss.internal.OSSHeaders;
26+
27+
/**
28+
* OSS Bucket's metadata.
29+
*/
30+
public class BucketMetadata {
31+
32+
/**
33+
* Gets bucket region.
34+
*
35+
* @return bucket region.
36+
*/
37+
public String getBucketRegion() {
38+
return httpMetadata.get(OSSHeaders.OSS_BUCKET_REGION);
39+
}
40+
41+
/**
42+
* Sets bucket region. SDK uses.
43+
*
44+
* @param bucketRegion
45+
* bucket Region.
46+
*/
47+
public void setBucketRegion(String bucketRegion) {
48+
httpMetadata.put(OSSHeaders.OSS_BUCKET_REGION, bucketRegion);
49+
}
50+
51+
/**
52+
* Add http metadata.
53+
*
54+
* @param key
55+
* The key of header.
56+
* @param value
57+
* The value of the key.
58+
*/
59+
public void addHttpMetadata(String key, String value) {
60+
httpMetadata.put(key, value);
61+
}
62+
63+
/**
64+
* Gets the http metadata.
65+
*
66+
* @return http metadata
67+
*/
68+
public Map<String, String> getHttpMetadata() {
69+
return httpMetadata;
70+
}
71+
72+
// http headers.
73+
private Map<String, String> httpMetadata = new HashMap<String, String>();
74+
75+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 junit.framework.Assert;
23+
24+
import org.junit.Test;
25+
26+
import com.aliyun.oss.OSSErrorCode;
27+
import com.aliyun.oss.OSSException;
28+
import com.aliyun.oss.internal.OSSHeaders;
29+
import com.aliyun.oss.model.BucketMetadata;
30+
31+
public class BucketMetadataTest extends TestBase {
32+
33+
@Test
34+
public void testGetBucketMetadata() {
35+
try {
36+
BucketMetadata meta = ossClient.getBucketMetadata(bucketName);
37+
Assert.assertEquals(meta.getBucketRegion(), TestConfig.OSS_TEST_REGION);
38+
Assert.assertEquals(meta.getHttpMetadata().get(OSSHeaders.OSS_HEADER_REQUEST_ID).length(),
39+
"59F2AC3B349A25FA4C44BF8A".length());
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
Assert.fail(e.getMessage());
43+
}
44+
}
45+
46+
@Test
47+
public void testUnormalGetBucketMetadata() {
48+
final String bucketName = "unormal-get-bucket-meta";
49+
50+
// bucket non-existent
51+
try {
52+
ossClient.getBucketMetadata(bucketName);
53+
Assert.fail("Get bucket meta should not be successful");
54+
} catch (OSSException e) {
55+
Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
56+
Assert.assertEquals(e.getRequestId().length(), "59F2AC3B349A25FA4C44BF8A".length());
57+
}
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)