Skip to content

Commit

Permalink
Merge pull request #11 from jogaco/master
Browse files Browse the repository at this point in the history
#6: Copy within same bucket, different prefix
  • Loading branch information
cobbzilla committed Dec 3, 2013
2 parents 03f547c + 18c3559 commit 8766708
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/main/java/org/cobbzilla/s3s3mirror/KeyJob.java
Expand Up @@ -32,7 +32,12 @@ public void run() {
try {
if (!shouldTransfer()) return;

final CopyObjectRequest request = new CopyObjectRequest(options.getSourceBucket(), key, options.getDestinationBucket(), key);
String keydest = key;
if (!options.getAddPrefix().equals("")) {
keydest = key.substring(options.getPrefix().length());
keydest = options.getAddPrefix() + keydest;
}
final CopyObjectRequest request = new CopyObjectRequest(options.getSourceBucket(), key, options.getDestinationBucket(), keydest);

final ObjectMetadata sourceMetadata = client.getObjectMetadata(options.getSourceBucket(), key);
request.setNewObjectMetadata(sourceMetadata);
Expand All @@ -41,22 +46,22 @@ public void run() {
request.setAccessControlList(objectAcl);

if (options.isDryRun()) {
log.info("Would have copied "+ key +" to destination");
log.info("Would have copied "+ key +" to destination: "+keydest);
} else {
boolean copiedOK = false;
for (int tries=0; tries<maxRetries; tries++) {
log.info("copying (try #"+tries+"): "+key);
log.info("copying (try #"+tries+"): "+key+" to: "+keydest);
try {
client.copyObject(request);
copiedOK = true;
if (verbose) log.info("successfully copied (on try #"+tries+"): "+key);
if (verbose) log.info("successfully copied (on try #"+tries+"): "+key+" to: "+keydest);
break;

} catch (AmazonS3Exception s3e) {
log.error("s3 exception copying (try #"+tries+") "+key+": "+s3e);
log.error("s3 exception copying (try #"+tries+") "+key+" to: "+keydest+": "+s3e);

} catch (Exception e) {
log.error("unexpected exception copying (try #"+tries+") "+key+": "+e);
log.error("unexpected exception copying (try #"+tries+") "+key+" to: "+keydest+": "+e);
}
try {
Thread.sleep(10);
Expand Down Expand Up @@ -86,6 +91,11 @@ private boolean shouldTransfer() {

final String key = summary.getKey();
final boolean verbose = options.isVerbose();
String keydest = key;
if (!options.getAddPrefix().equals("")) {
keydest = key.substring(options.getPrefix().length());
keydest = options.getAddPrefix() + keydest;
}

if (options.hasCtime()) {
final Date lastModified = summary.getLastModified();
Expand All @@ -107,17 +117,17 @@ private boolean shouldTransfer() {

final ObjectMetadata metadata;
try {
metadata = client.getObjectMetadata(options.getDestinationBucket(), key);
metadata = client.getObjectMetadata(options.getDestinationBucket(), keydest);
} catch (AmazonS3Exception e) {
if (e.getStatusCode() == 404) {
if (verbose) log.info("Key not found in destination bucket (will copy): "+ key);
if (verbose) log.info("Key not found in destination bucket (will copy): "+ keydest);
return true;
} else {
log.info("Error getting metadata for "+options.getDestinationBucket()+"/"+ key +" (not copying): "+e);
log.info("Error getting metadata for "+options.getDestinationBucket()+"/"+ keydest +" (not copying): "+e);
return false;
}
} catch (Exception e) {
log.info("Error getting metadata for "+options.getDestinationBucket()+"/"+ key +" (not copying): "+e);
log.info("Error getting metadata for "+options.getDestinationBucket()+"/"+ keydest +" (not copying): "+e);
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cobbzilla/s3s3mirror/MirrorOptions.java
Expand Up @@ -36,6 +36,11 @@ public class MirrorOptions implements AWSCredentials {
public static final String LONGOPT_PREFIX = "--prefix";
@Option(name=OPT_PREFIX, aliases=LONGOPT_PREFIX, usage=USAGE_PREFIX)
@Getter @Setter private String prefix = null;
public static final String USAGE_ADDPREFIX = "Give the copied object this prefix (replacing the one specified in --prefix, if any)";
public static final String OPT_ADDPREFIX = "-a";
public static final String LONGOPT_ADDPREFIX = "--addprefix";
@Option(name=OPT_ADDPREFIX, aliases=LONGOPT_ADDPREFIX, usage=USAGE_ADDPREFIX)
@Getter @Setter private String addPrefix = null;

public static final String USAGE_MAX_CONNECTIONS = "Maximum number of connections to S3";
public static final String OPT_MAX_CONNECTIONS = "-m";
Expand Down

0 comments on commit 8766708

Please sign in to comment.