com.aliyun.oss.internal.OSSBucketOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aliyun-sdk-oss-shade Show documentation
Show all versions of aliyun-sdk-oss-shade Show documentation
The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service, includes all service and dependent JARs.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.aliyun.oss.internal;
import static com.aliyun.oss.common.parser.RequestMarshallers.*;
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
import static com.aliyun.oss.internal.OSSUtils.OSS_RESOURCE_MANAGER;
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameCreationValid;
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
import static com.aliyun.oss.internal.RequestParameters.*;
import static com.aliyun.oss.internal.RequestParameters.ACCESS_MONITOR;
import static com.aliyun.oss.internal.ResponseParsers.*;
import static com.aliyun.oss.internal.ResponseParsers.listAccessPointsResponseParser;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSErrorCode;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.ServiceException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.comm.RequestMessage;
import com.aliyun.oss.common.comm.ResponseHandler;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.common.comm.ServiceClient;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.common.utils.ExceptionFactory;
import com.aliyun.oss.common.utils.HttpHeaders;
import com.aliyun.oss.model.*;
import org.apache.http.HttpStatus;
/**
* Bucket operation.
*/
public class OSSBucketOperation extends OSSOperation {
public OSSBucketOperation(ServiceClient client, CredentialsProvider credsProvider) {
super(client, credsProvider);
}
/**
* Create a bucket.
*/
public Bucket createBucket(CreateBucketRequest createBucketRequest) throws OSSException, ClientException {
assertParameterNotNull(createBucketRequest, "createBucketRequest");
String bucketName = createBucketRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameCreationValid(bucketName);
Map headers = new HashMap();
addOptionalACLHeader(headers, createBucketRequest.getCannedACL());
addOptionalHnsHeader(headers, createBucketRequest.getHnsStatus());
addOptionalResourceGroupIdHeader(headers, createBucketRequest.getResourceGroupId());
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(createBucketRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setHeaders(headers)
.setInputStreamWithLength(createBucketRequestMarshaller.marshall(createBucketRequest))
.setOriginalRequest(createBucketRequest).build();
ResponseMessage result = doOperation(request, emptyResponseParser, bucketName, null);
return new Bucket(bucketName, result.getRequestId());
}
/**
* Delete a bucket.
*/
public VoidResult deleteBucket(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setOriginalRequest(genericRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* List all my buckets.
*/
public List listBuckets() throws OSSException, ClientException {
BucketList bucketList = listBuckets(new ListBucketsRequest(null, null, null));
List buckets = bucketList.getBucketList();
while (bucketList.isTruncated()) {
bucketList = listBuckets(new ListBucketsRequest(null, bucketList.getNextMarker(), null));
buckets.addAll(bucketList.getBucketList());
}
return buckets;
}
/**
* List all my buckets.
*/
public BucketList listBuckets(ListBucketsRequest listBucketRequest) throws OSSException, ClientException {
assertParameterNotNull(listBucketRequest, "listBucketRequest");
Map params = new LinkedHashMap();
if (listBucketRequest.getPrefix() != null) {
params.put(PREFIX, listBucketRequest.getPrefix());
}
if (listBucketRequest.getMarker() != null) {
params.put(MARKER, listBucketRequest.getMarker());
}
if (listBucketRequest.getMaxKeys() != null) {
params.put(MAX_KEYS, Integer.toString(listBucketRequest.getMaxKeys()));
}
if (listBucketRequest.getBid() != null) {
params.put(BID, listBucketRequest.getBid());
}
if (listBucketRequest.getTagKey() != null && listBucketRequest.getTagValue() != null) {
params.put(TAG_KEY, listBucketRequest.getTagKey());
params.put(TAG_VALUE, listBucketRequest.getTagValue());
}
if (listBucketRequest.getParameters() != null && listBucketRequest.getParameters().containsKey(SUBRESOURCE_REGION_LIST)) {
params.put(SUBRESOURCE_REGION_LIST, listBucketRequest.getParameters().get(SUBRESOURCE_REGION_LIST));
}
Map headers = new HashMap();
addOptionalResourceGroupIdHeader(headers, listBucketRequest.getResourceGroupId());
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(listBucketRequest))
.setMethod(HttpMethod.GET).setHeaders(headers).setParameters(params).setOriginalRequest(listBucketRequest).build();
return doOperation(request, listBucketResponseParser, null, null, true);
}
/**
* Set bucket's canned ACL.
*/
public VoidResult setBucketAcl(SetBucketAclRequest setBucketAclRequest) throws OSSException, ClientException {
assertParameterNotNull(setBucketAclRequest, "setBucketAclRequest");
String bucketName = setBucketAclRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map headers = new HashMap();
addOptionalACLHeader(headers, setBucketAclRequest.getCannedACL());
Map params = new HashMap();
params.put(SUBRESOURCE_ACL, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(setBucketAclRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setHeaders(headers).setParameters(params)
.setOriginalRequest(setBucketAclRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* Get bucket's ACL.
*/
public AccessControlList getBucketAcl(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_ACL, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, getBucketAclResponseParser, bucketName, null, true);
}
public BucketMetadata getBucketMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.HEAD).setBucket(bucketName).setOriginalRequest(genericRequest).build();
List reponseHandlers = new ArrayList();
reponseHandlers.add(new ResponseHandler() {
@Override
public void handle(ResponseMessage response) throws ServiceException, ClientException {
if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
safeCloseResponse(response);
throw ExceptionFactory.createOSSException(
response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_BUCKET,
OSS_RESOURCE_MANAGER.getString("NoSuchBucket"));
}
}
});
return doOperation(request, ResponseParsers.getBucketMetadataResponseParser, bucketName, null, true, null,
reponseHandlers);
}
/**
* Set bucket referer.
*/
public VoidResult setBucketReferer(SetBucketRefererRequest setBucketRefererRequest) throws OSSException, ClientException {
assertParameterNotNull(setBucketRefererRequest, "setBucketRefererRequest");
String bucketName = setBucketRefererRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
BucketReferer referer = setBucketRefererRequest.getReferer();
if (referer == null) {
referer = new BucketReferer();
}
Map params = new HashMap();
params.put(SUBRESOURCE_REFERER, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(setBucketRefererRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
.setInputStreamWithLength(bucketRefererMarshaller.marshall(referer))
.setOriginalRequest(setBucketRefererRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* Get bucket referer.
*/
public BucketReferer getBucketReferer(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_REFERER, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, getBucketRefererResponseParser, bucketName, null, true);
}
/**
* Get bucket location.
*/
public String getBucketLocation(GenericRequest genericRequest) {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_LOCATION, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, getBucketLocationResponseParser, bucketName, null, true);
}
/**
* Determine whether a bucket exists or not.
*/
public boolean doesBucketExists(GenericRequest genericRequest) throws OSSException, ClientException {
assertParameterNotNull(genericRequest, "genericRequest");
String bucketName = genericRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
try {
getBucketAcl(new GenericRequest(bucketName));
} catch (OSSException oe) {
if (oe.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)) {
return false;
}
}
return true;
}
/**
* List objects under the specified bucket.
*/
public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) throws OSSException, ClientException {
assertParameterNotNull(listObjectsRequest, "listObjectsRequest");
String bucketName = listObjectsRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new LinkedHashMap();
populateListObjectsRequestParameters(listObjectsRequest, params);
Map headers = new HashMap();
populateRequestPayerHeader(headers, listObjectsRequest.getRequestPayer());
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(listObjectsRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
.setOriginalRequest(listObjectsRequest).build();
return doOperation(request, listObjectsReponseParser, bucketName, null, true);
}
/**
* List objects under the specified bucket.
*/
public ListObjectsV2Result listObjectsV2(ListObjectsV2Request listObjectsV2Request) throws OSSException, ClientException {
assertParameterNotNull(listObjectsV2Request, "listObjectsRequest");
String bucketName = listObjectsV2Request.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new LinkedHashMap();
populateListObjectsV2RequestParameters(listObjectsV2Request, params);
Map headers = new HashMap();
populateRequestPayerHeader(headers, listObjectsV2Request.getRequestPayer());
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(listObjectsV2Request))
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
.setOriginalRequest(listObjectsV2Request).build();
return doOperation(request, listObjectsV2ResponseParser, bucketName, null, true);
}
/**
* List versions under the specified bucket.
*/
public VersionListing listVersions(ListVersionsRequest listVersionsRequest) throws OSSException, ClientException {
assertParameterNotNull(listVersionsRequest, "listVersionsRequest");
String bucketName = listVersionsRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new LinkedHashMap();
populateListVersionsRequestParameters(listVersionsRequest, params);
Map headers = new HashMap();
populateRequestPayerHeader(headers, listVersionsRequest.getRequestPayer());
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(listVersionsRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setHeaders(headers).setParameters(params)
.setOriginalRequest(listVersionsRequest).build();
return doOperation(request, listVersionsReponseParser, bucketName, null, true);
}
/**
* Set bucket logging.
*/
public VoidResult setBucketLogging(SetBucketLoggingRequest setBucketLoggingRequest) throws OSSException, ClientException {
assertParameterNotNull(setBucketLoggingRequest, "setBucketLoggingRequest");
String bucketName = setBucketLoggingRequest.getBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_LOGGING, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(setBucketLoggingRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
.setInputStreamWithLength(setBucketLoggingRequestMarshaller.marshall(setBucketLoggingRequest))
.setOriginalRequest(setBucketLoggingRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* Put bucket image
*/
public VoidResult putBucketImage(PutBucketImageRequest putBucketImageRequest) {
assertParameterNotNull(putBucketImageRequest, "putBucketImageRequest");
String bucketName = putBucketImageRequest.GetBucketName();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_IMG, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(putBucketImageRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
.setOriginalRequest(putBucketImageRequest)
.setInputStreamWithLength(putBucketImageRequestMarshaller.marshall(putBucketImageRequest)).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* Get bucket image
*/
public GetBucketImageResult getBucketImage(String bucketName, GenericRequest genericRequest) {
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_IMG, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, getBucketImageResponseParser, bucketName, null, true);
}
/**
* Delete bucket image attributes.
*/
public VoidResult deleteBucketImage(String bucketName, GenericRequest genericRequest)
throws OSSException, ClientException {
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_IMG, null);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
/**
* put image style
*/
public VoidResult putImageStyle(PutImageStyleRequest putImageStyleRequest) throws OSSException, ClientException {
assertParameterNotNull(putImageStyleRequest, "putImageStyleRequest");
String bucketName = putImageStyleRequest.GetBucketName();
String styleName = putImageStyleRequest.GetStyleName();
assertParameterNotNull(styleName, "styleName");
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_STYLE, null);
params.put(STYLE_NAME, styleName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(putImageStyleRequest))
.setMethod(HttpMethod.PUT).setBucket(bucketName).setParameters(params)
.setOriginalRequest(putImageStyleRequest)
.setInputStreamWithLength(putImageStyleRequestMarshaller.marshall(putImageStyleRequest)).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
public VoidResult deleteImageStyle(String bucketName, String styleName, GenericRequest genericRequest)
throws OSSException, ClientException {
assertParameterNotNull(bucketName, "bucketName");
assertParameterNotNull(styleName, "styleName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_STYLE, null);
params.put(STYLE_NAME, styleName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.DELETE).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, requestIdResponseParser, bucketName, null);
}
public GetImageStyleResult getImageStyle(String bucketName, String styleName, GenericRequest genericRequest)
throws OSSException, ClientException {
assertParameterNotNull(bucketName, "bucketName");
assertParameterNotNull(styleName, "styleName");
ensureBucketNameValid(bucketName);
Map params = new HashMap();
params.put(SUBRESOURCE_STYLE, null);
params.put(STYLE_NAME, styleName);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint(genericRequest))
.setMethod(HttpMethod.GET).setBucket(bucketName).setParameters(params)
.setOriginalRequest(genericRequest).build();
return doOperation(request, getImageStyleResponseParser, bucketName, null, true);
}
/**
* List image style.
*/
public List