Skip to content

Commit af50bc7

Browse files
committed
when auto-creating a bucket with includeVersions enabled, S3Target now checks to see if the source bucket has versioning enabled before enabling it on the new bucket
1 parent 1828103 commit af50bc7

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/main/java/com/emc/vipr/sync/source/S3Source.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.net.URLDecoder;
4040
import java.util.Arrays;
4141
import java.util.Iterator;
42+
import java.util.List;
4243
import java.util.ListIterator;
4344

4445
/**
@@ -70,6 +71,7 @@ public class S3Source extends SyncSource<S3SyncObject> {
7071
private String rootKey;
7172
private boolean decodeKeys;
7273
private S3Target s3Target;
74+
private boolean versioningEnabled = false;
7375

7476
private AmazonS3 s3;
7577

@@ -123,6 +125,15 @@ public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarge
123125
if (endpoint != null)
124126
s3.setEndpoint(endpoint);
125127

128+
// TODO: generalize uri translation
129+
S3Util.S3Uri s3Uri = new S3Util.S3Uri();
130+
s3Uri.protocol = protocol;
131+
s3Uri.endpoint = endpoint;
132+
s3Uri.accessKey = accessKey;
133+
s3Uri.secretKey = secretKey;
134+
s3Uri.rootKey = rootKey;
135+
if (sourceUri == null) sourceUri = s3Uri.toUri();
136+
126137
if (disableVHosts) {
127138
l4j.info("The use of virtual hosted buckets on the s3 source has been DISABLED. Path style buckets will be used.");
128139
S3ClientOptions opts = new S3ClientOptions();
@@ -139,7 +150,14 @@ public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarge
139150
if (rootKey.length() > 0 && !rootKey.endsWith("/")) rootKey += "/"; // " ends with slash
140151

141152
// for version support. TODO: genericize version support
142-
if (target instanceof S3Target) s3Target = (S3Target) target;
153+
if (target instanceof S3Target) {
154+
s3Target = (S3Target) target;
155+
if (s3Target.isIncludeVersions()) {
156+
BucketVersioningConfiguration versioningConfig = s3.getBucketVersioningConfiguration(bucketName);
157+
List<String> versionedStates = Arrays.asList(BucketVersioningConfiguration.ENABLED, BucketVersioningConfiguration.SUSPENDED);
158+
versioningEnabled = versionedStates.contains(versioningConfig.getStatus());
159+
}
160+
}
143161
}
144162

145163
@Override
@@ -493,4 +511,8 @@ public boolean isDecodeKeys() {
493511
public void setDecodeKeys(boolean decodeKeys) {
494512
this.decodeKeys = decodeKeys;
495513
}
514+
515+
public boolean isVersioningEnabled() {
516+
return versioningEnabled;
517+
}
496518
}

src/main/java/com/emc/vipr/sync/target/S3Target.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,30 @@ public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarge
121121
if (endpoint != null)
122122
s3.setEndpoint(endpoint);
123123

124+
// TODO: generalize uri translation
125+
S3Util.S3Uri s3Uri = new S3Util.S3Uri();
126+
s3Uri.protocol = protocol;
127+
s3Uri.endpoint = endpoint;
128+
s3Uri.accessKey = accessKey;
129+
s3Uri.secretKey = secretKey;
130+
s3Uri.rootKey = rootKey;
131+
if (targetUri == null) targetUri = s3Uri.toUri();
132+
124133
if (disableVHosts) {
125134
l4j.info("The use of virtual hosted buckets on the s3 source has been DISABLED. Path style buckets will be used.");
126135
S3ClientOptions opts = new S3ClientOptions();
127136
opts.setPathStyleAccess(true);
128137
s3.setS3ClientOptions(opts);
129138
}
130139

140+
// for version support. TODO: genericize version support
141+
if (source instanceof S3Source) {
142+
s3Source = (S3Source) source;
143+
if (!s3Source.isVersioningEnabled()) includeVersions = false; // don't include versions if source versioning is off
144+
} else if (includeVersions) {
145+
throw new ConfigurationException("Object versions are currently only supported with the S3 source & target plugins.");
146+
}
147+
131148
if (!s3.doesBucketExist(bucketName)) {
132149
if (createBucket) {
133150
s3.createBucket(bucketName);
@@ -141,11 +158,7 @@ public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarge
141158

142159
if (rootKey == null) rootKey = ""; // make sure rootKey isn't null
143160

144-
// for version support. TODO: genericize version support
145-
if (source instanceof S3Source) s3Source = (S3Source) source;
146161
if (includeVersions) {
147-
if (s3Source == null)
148-
throw new ConfigurationException("Object versions are currently only supported with the S3 source & target plugins.");
149162
String status = s3.getBucketVersioningConfiguration(bucketName).getStatus();
150163
if (BucketVersioningConfiguration.OFF.equals(status))
151164
throw new ConfigurationException("The specified bucket does not have versioning enabled.");

0 commit comments

Comments
 (0)