Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support copyObjects API #354

Open
wants to merge 1 commit into
base: dev-2101
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/com/aliyun/oss/OSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,19 @@ public CopyObjectResult copyObject(String sourceBucketName, String sourceKey, St
*/
public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws OSSException, ClientException;

/**
* Copies existing keys to target keys in an OSS bucket.
* If target key exist, it would be overwritten by the source key.
*
* @param copyObjectsRequest
* A {@link CopyObjectsRequest} instance that specifies bucket,
* source keys and target keys.
* @return A {@link CopyObjectsResult} instance.
* @throws OSSException
* @throws ClientException
*/
public CopyObjectsResult copyObjects(CopyObjectsRequest copyObjectsRequest) throws OSSException, ClientException;

/**
* Gets a {@link OSSObject} from {@link Bucket}.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/aliyun/oss/OSSClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws O
return objectOperation.copyObject(copyObjectRequest);
}

@Override
public CopyObjectsResult copyObjects(CopyObjectsRequest copyObjectsRequest) throws OSSException, ClientException {
return objectOperation.copyObjects(copyObjectsRequest);
}

@Override
public OSSObject getObject(String bucketName, String key) throws OSSException, ClientException {
return this.getObject(new GetObjectRequest(bucketName, key));
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public final class RequestMarshallers {
public static final DeleteVpcipRequestMarshaller deleteVpcipRequestMarshaller = new DeleteVpcipRequestMarshaller();
public static final DeleteBucketVpcipRequestMarshaller deleteBucketVpcipRequestMarshaller = new DeleteBucketVpcipRequestMarshaller();

public static final CopyObjectsRequestMarshaller copyObjectsRequestMarshaller = new CopyObjectsRequestMarshaller();
public interface RequestMarshaller<R> extends Marshaller<FixedLengthInputStream, R> {

}
Expand All @@ -102,6 +103,27 @@ public interface RequestMarshaller2<R> extends Marshaller<byte[], R> {

}

public static final class CopyObjectsRequestMarshaller implements RequestMarshaller<CopyObjectsRequest> {

@Override
public FixedLengthInputStream marshall(CopyObjectsRequest request) {
StringBuffer xmlBody = new StringBuffer();
xmlBody.append("<Copy>");
for (CopyObjectEntity entity : request.getCopyObjectEntities()) {
xmlBody.append("<Object>");
xmlBody.append("<SourceKey>");
xmlBody.append(entity.getSourceKey());
xmlBody.append("</SourceKey>");
xmlBody.append("<TargetKey>");
xmlBody.append(entity.getTargetKey());
xmlBody.append("</TargetKey>");
xmlBody.append("</Object>");
}
xmlBody.append("</Copy>");
return stringMarshaller.marshall(xmlBody.toString());
}
}

public static final class StringMarshaller implements Marshaller<FixedLengthInputStream, String> {

@Override
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/aliyun/oss/internal/OSSObjectOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
import static com.aliyun.oss.internal.OSSUtils.safeCloseResponse;
import static com.aliyun.oss.internal.RequestParameters.ENCODING_TYPE;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_ACL;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_COPY;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_DELETE;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_OBJECTMETA;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_SYMLINK;
import static com.aliyun.oss.internal.RequestParameters.SUBRESOURCE_TAGGING;
import static com.aliyun.oss.internal.ResponseParsers.appendObjectResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.copyObjectResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.copyObjectsResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.deleteObjectsResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.getTaggingResponseParser;
import static com.aliyun.oss.internal.ResponseParsers.getObjectAclResponseParser;
Expand Down Expand Up @@ -496,6 +498,22 @@ public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws O
copyObjectRequest.getDestinationKey(), true);
}

/**
* Copy existing objects to targets.
*/
public CopyObjectsResult copyObjects(CopyObjectsRequest copyObjectsRequest) throws OSSException, ClientException {
assertParameterNotNull(copyObjectsRequest, "copyObjectsRequest");

Map<String, String> params = new HashMap<String, String>();
params.put(SUBRESOURCE_COPY, null);

RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint())
.setMethod(HttpMethod.POST).setBucket(copyObjectsRequest.getBucketName())
.setInputStreamWithLength(copyObjectsRequestMarshaller.marshall(copyObjectsRequest))
.setParameters(params).setOriginalRequest(copyObjectsRequest).build();
return doOperation(request, copyObjectsResponseParser, copyObjectsRequest.getBucketName(), null, true);
}

/**
* Delete an object.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public final class RequestParameters {
public static final String SUBRESOURCE_UPLOADS = "uploads";
public static final String SUBRESOURCE_DELETE = "delete";
public static final String SUBRESOURCE_CORS = "cors";
public static final String SUBRESOURCE_COPY = "copy";
public static final String SUBRESOURCE_APPEND = "append";
public static final String SUBRESOURCE_TAGGING = "tagging";
public static final String SUBRESOURCE_IMG = "img";
Expand Down
54 changes: 53 additions & 1 deletion src/main/java/com/aliyun/oss/internal/ResponseParsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import com.aliyun.oss.model.CnameConfiguration;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.CopyObjectsResult;
import com.aliyun.oss.model.CreateLiveChannelResult;
import com.aliyun.oss.model.DataRedundancyType;
import com.aliyun.oss.model.DeleteObjectsResult;
Expand Down Expand Up @@ -126,6 +127,7 @@
import com.aliyun.oss.model.Permission;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.oss.model.PushflowStatus;
import com.aliyun.oss.model.SingleCopyObjectResult;
import com.aliyun.oss.model.SetBucketCORSRequest.CORSRule;
import com.aliyun.oss.model.CORSConfiguration;
import com.aliyun.oss.model.SimplifiedObjectMeta;
Expand Down Expand Up @@ -196,6 +198,7 @@ public final class ResponseParsers {
public static final AppendObjectResponseParser appendObjectResponseParser = new AppendObjectResponseParser();
public static final GetObjectMetadataResponseParser getObjectMetadataResponseParser = new GetObjectMetadataResponseParser();
public static final CopyObjectResponseParser copyObjectResponseParser = new CopyObjectResponseParser();
public static final CopyObjectsResponseParser copyObjectsResponseParser = new CopyObjectsResponseParser();
public static final DeleteObjectsResponseParser deleteObjectsResponseParser = new DeleteObjectsResponseParser();
public static final DeleteVersionsResponseParser deleteVersionsResponseParser = new DeleteVersionsResponseParser();
public static final GetObjectAclResponseParser getObjectAclResponseParser = new GetObjectAclResponseParser();
Expand Down Expand Up @@ -995,7 +998,22 @@ public DeleteObjectsResult parse(ResponseMessage response) throws ResponseParseE
}

}


public static final class CopyObjectsResponseParser implements ResponseParser<CopyObjectsResult> {

@Override
public CopyObjectsResult parse(ResponseMessage response) throws ResponseParseException {
try {
CopyObjectsResult result = parseCopyObjectsResult(response.getContent());
result.setRequestId(response.getRequestId());
result.setResponse(response);
return result;
} finally {
safeCloseResponse(response);
}
}
}

public static final class DeleteVersionsResponseParser implements ResponseParser<DeleteVersionsResult> {

@Override
Expand Down Expand Up @@ -2201,6 +2219,40 @@ public static CopyObjectResult parseCopyObjectResult(InputStream responseBody) t
}
}

/**
* Unmarshall copy objects response body to corresponding result.
*/
public static CopyObjectsResult parseCopyObjectsResult(InputStream responseBody) throws ResponseParseException {
try {
Element root = getXmlRootElement(responseBody);
CopyObjectsResult result = new CopyObjectsResult();
for (Element element : root.getChildren()) {
if (element.getName().equals("Success")) {
for (Element object : element.getChildren()) {
result.addSuccessResult(
SingleCopyObjectResult.success(
object.getChild("SourceKey").getText(),
object.getChild("TargetKey").getText(),
object.getChild("ETag").getText()));
}
} else if (element.getName().equals("Failed")) {
for (Element object : element.getChildren()) {
result.addFailureResult(
SingleCopyObjectResult.failure(
object.getChild("SourceKey").getText(),
object.getChild("TargetKey").getText(),
object.getChild("ErrorStatus").getText()));
}
} else {
throw new Exception("Invalid xml node: " + element.getName());
}
}
return result;
} catch (Exception e) {
throw new ResponseParseException(e.getMessage(), e);
}
}

/**
* Unmarshall delete objects response body to corresponding result.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/aliyun/oss/internal/SignParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class SignParameters {
SUBRESOURCE_ENCRYPTION, SUBRESOURCE_POLICY, SUBRESOURCE_REQUEST_PAYMENT, OSS_TRAFFIC_LIMIT,
SUBRESOURCE_QOS_INFO, SUBRESOURCE_ASYNC_FETCH, SEQUENTIAL, OSS_REQUEST_PAYER, VPCIP, VIP,
SUBRESOURCE_INVENTORY, SUBRESOURCE_INVENTORY_ID, SUBRESOURCE_CONTINUATION_TOKEN, SUBRESOURCE_WORM,
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR});
SUBRESOURCE_WORM_ID, SUBRESOURCE_WORM_EXTEND, SUBRESOURCE_CALLBACK, SUBRESOURCE_CALLBACK_VAR,SUBRESOURCE_COPY});


}
49 changes: 49 additions & 0 deletions src/main/java/com/aliyun/oss/model/CopyObjectEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.model;

/**
* Copy object entity used by {@link CopyObjectsRequest}.
*/
public class CopyObjectEntity {
private String sourceKey;
private String targetKey;

public CopyObjectEntity(String sourceKey, String targetKey) {
setSourceKey(sourceKey);
setTargetKey(targetKey);
}

public String getSourceKey() {
return sourceKey;
}

public void setSourceKey(String sourceKey) {
this.sourceKey = sourceKey;
}

public String getTargetKey() {
return targetKey;
}

public void setTargetKey(String targetKey) {
this.targetKey = targetKey;
}
}
53 changes: 53 additions & 0 deletions src/main/java/com/aliyun/oss/model/CopyObjectsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.model;

import java.util.ArrayList;
import java.util.List;

/**
* The request class that is used to copy objects. It wraps all parameters
* needed to copy objects.
*/
public class CopyObjectsRequest extends WebServiceRequest {
private String bucketName;
private List<CopyObjectEntity> copyObjectEntities;

public CopyObjectsRequest(String bucketName) {
setBucketName(bucketName);
this.copyObjectEntities = new ArrayList<CopyObjectEntity>();
}

public String getBucketName() {
return bucketName;
}

public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}

public void addCopyObjectEntity(CopyObjectEntity copyObjectEntity) {
copyObjectEntities.add(copyObjectEntity);
}

public List<CopyObjectEntity> getCopyObjectEntities() {
return copyObjectEntities;
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/aliyun/oss/model/CopyObjectsResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.model;

import java.util.ArrayList;
import java.util.List;

/**
* The result of copying existing OSS objects.
*/
public class CopyObjectsResult extends GenericResult {
private List<SingleCopyObjectResult> successResults;
private List<SingleCopyObjectResult> failureResults;

public CopyObjectsResult() {
successResults = new ArrayList<SingleCopyObjectResult>();
failureResults = new ArrayList<SingleCopyObjectResult>();
}

public void addSuccessResult(SingleCopyObjectResult successResult) {
successResults.add(successResult);
}

public void addFailureResult(SingleCopyObjectResult failureResult) {
failureResults.add(failureResult);
}

public List<SingleCopyObjectResult> getSuccessResults() {
return successResults;
}

public List<SingleCopyObjectResult> getFailureResults() {
return failureResults;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("CopyObjectsResult:\n");
sb.append("\tSuccessObjects:\n");
for (SingleCopyObjectResult result : successResults) {
sb.append("\t\t").append(result.toString()).append("\n");
}
sb.append("\tFailureObjects:\n");
for (SingleCopyObjectResult result : failureResults) {
sb.append("\t\t").append(result.toString()).append("\n");
}
return sb.toString();
}
}
Loading