-
Notifications
You must be signed in to change notification settings - Fork 343
Expand file tree
/
Copy pathS3TestUtil.java
More file actions
183 lines (163 loc) · 7.27 KB
/
S3TestUtil.java
File metadata and controls
183 lines (163 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.amazonaws.samples;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import java.io.*;
import java.util.Arrays;
import java.util.Iterator;
/**
* Requires that you created a file ~/.aws/credentials
*/
public class S3TestUtil
{
private static final AmazonS3 s3;
private static final Region usWest2;
private static final DefaultAWSCredentialsProviderChain credentialProviderChain;
private static TransferManager tx;
static {
s3 = new AmazonS3Client();
usWest2 = Region.getRegion(Regions.US_WEST_2);
s3.setRegion(usWest2);
credentialProviderChain = new DefaultAWSCredentialsProviderChain();
}
public static AmazonS3 getS3() {
return s3;
}
public static void deleteEntireBucket(String bucket_name) {
System.out.println("Deleting S3 bucket: " + bucket_name);
try {
System.out.println(" - removing objects from bucket");
ObjectListing object_listing = s3.listObjects(bucket_name);
while (true) {
for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext(); ) {
S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
s3.deleteObject(bucket_name, summary.getKey());
}
if (object_listing.isTruncated()) {
object_listing = s3.listNextBatchOfObjects(object_listing);
} else {
break;
}
}
System.out.println(" - removing versions from bucket");
VersionListing version_listing = s3.listVersions(new ListVersionsRequest().withBucketName(bucket_name));
while (true) {
for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext(); ) {
S3VersionSummary vs = (S3VersionSummary) iterator.next();
s3.deleteVersion(bucket_name, vs.getKey(), vs.getVersionId());
}
if (version_listing.isTruncated()) {
version_listing = s3.listNextBatchOfVersions(version_listing);
} else {
break;
}
}
s3.deleteBucket(bucket_name);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done removing bucket: " + bucket_name);
}
public static void copyEntireBucket(String src_bucket, String dest_bucket) {
System.out.println("Copying S3 bucket '" + src_bucket + "' to destination '" + dest_bucket + "' ...");
try {
System.out.println(" - copying objects from bucket");
ObjectListing object_listing = s3.listObjects(src_bucket);
while (true) {
for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext(); ) {
S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
s3.copyObject(src_bucket, summary.getKey(), dest_bucket, summary.getKey());
}
if (object_listing.isTruncated()) {
object_listing = s3.listNextBatchOfObjects(object_listing);
} else {
break;
}
}
//System.out.println(" - copying versions from bucket");
//VersionListing version_listing = s3.listVersions(new ListVersionsRequest().withBucketName(src_bucket));
//while (true) {
// for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext(); ) {
// S3VersionSummary vs = (S3VersionSummary) iterator.next();
// String key = vs.getKey();
// String ver = vs.getVersionId();
// // how??
// }
// if (version_listing.isTruncated()) {
// version_listing = s3.listNextBatchOfVersions(version_listing);
// } else {
// break;
// }
//}
//s3.deleteBucket(src_bucket);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done copying bucket '" + src_bucket + "' to new bucket '" + dest_bucket + "'");
}
public static File createTmpFile() {
File tempTestFile = null;
try {
tempTestFile = File.createTempFile("aws-java-sdk-copy-test-", ".txt");
tempTestFile.deleteOnExit();
Writer writer = new OutputStreamWriter(new FileOutputStream(tempTestFile));
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.write("01234567890112345678901234\n");
writer.write("!@#$%^&*()-=[]{};':',.<>/?\n");
writer.write("01234567890112345678901234\n");
writer.write("abcdefghijklmnopqrstuvwxyz\n");
writer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return tempTestFile;
}
public static void createExpiringBucket(String name) {
BucketLifecycleConfiguration.Rule bucketExpirationRule =
new BucketLifecycleConfiguration.Rule()
.withId("RULE: Delete bucket after 1 day")
.withExpirationInDays(1) // expiration date must be midnight GMT and is effectively GMT-8, or around 4pm
.withStatus(BucketLifecycleConfiguration.ENABLED.toString());
BucketLifecycleConfiguration configuration =
new BucketLifecycleConfiguration()
.withRules(Arrays.asList(bucketExpirationRule));
boolean bucketMissing = !bucketExists(name);
if (bucketMissing) {
System.out.println("Creating 10 minute bucket " + name + "\n");
s3.createBucket(name);
s3.setBucketLifecycleConfiguration(name, configuration);
}
}
public static boolean bucketExists(String name) {
for (Bucket bucket : s3.listBuckets()) {
if (bucket.getName().equals(name)) {
return true;
}
}
return false;
}
public static void showAllBuckets() {
for (Bucket bucket : s3.listBuckets()) {
System.out.println("bucket: " + bucket.getName());
}
}
public static void uploadTmpFileToBucket(String bucketName, File fileKey) {
System.out.println("Uploading a file to bucket " + bucketName);
tx = new TransferManager(credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(bucketName, fileKey.getName(), fileKey);
try {
myUpload.waitForCompletion();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tx != null) tx.shutdownNow();
}
}