-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCcStoreS3.java
More file actions
238 lines (221 loc) · 9.5 KB
/
CcStoreS3.java
File metadata and controls
238 lines (221 loc) · 9.5 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package usace.cc.plugin;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.SdkBaseException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.RegionUtils;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.fasterxml.jackson.databind.ObjectMapper;
public class CcStoreS3 implements CcStore {
String localRootPath;
String bucket;
String root;
String manifestId;
String payloadId;
StoreType storeType;
AmazonS3 awsS3;
AWSConfig config;
public CcStoreS3(){
AWSConfig acfg = new AWSConfig();
acfg.aws_access_key_id = System.getenv(EnvironmentVariables.CC_PROFILE + "_" + EnvironmentVariables.AWS_ACCESS_KEY_ID);
acfg.aws_secret_access_key_id = System.getenv(EnvironmentVariables.CC_PROFILE + "_" + EnvironmentVariables.AWS_SECRET_ACCESS_KEY);
acfg.aws_region = System.getenv(EnvironmentVariables.CC_PROFILE + "_" + EnvironmentVariables.AWS_DEFAULT_REGION);
acfg.aws_bucket = System.getenv(EnvironmentVariables.CC_PROFILE + "_" + EnvironmentVariables.AWS_S3_BUCKET);
acfg.aws_endpoint = System.getenv(EnvironmentVariables.CC_PROFILE + "_" +"AWS_ENDPOINT");
config = acfg;
Region clientRegion = RegionUtils.getRegion(config.aws_region);//.toUpperCase().replace("-", "_"));//Regions.valueOf(config.aws_region.toUpperCase().replace("-", "_"));
try {
AmazonS3 s3Client = null;
if(!config.aws_endpoint.equals("")){
System.out.println(String.format("Using alt endpoint: %s",config.aws_endpoint));
config.aws_force_path_style=true;
config.aws_disable_ssl=true;
AWSCredentials credentials = new BasicAWSCredentials(config.aws_access_key_id, config.aws_secret_access_key_id);
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSignerOverride("AWSS3V4SignerType");
clientConfiguration.setProtocol(Protocol.HTTP);
s3Client = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(config.aws_endpoint, clientRegion.getName()))
.withPathStyleAccessEnabled(config.aws_force_path_style)
.withClientConfiguration(clientConfiguration)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}else{
AWSCredentials credentials = new BasicAWSCredentials(config.aws_access_key_id, config.aws_secret_access_key_id);
s3Client = AmazonS3ClientBuilder
.standard()
.withRegion(clientRegion.getName())
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
awsS3 = s3Client;
} catch (AmazonServiceException e) {
// The call was transmitted successfully, but Amazon S3 couldn't process
// it, so it returned an error response.
e.printStackTrace();
} catch (SdkClientException e) {
// Amazon S3 couldn't be contacted for a response, or the client
// couldn't parse the response from Amazon S3.
e.printStackTrace();
}
storeType = StoreType.S3;
manifestId = System.getenv(EnvironmentVariables.CC_MANIFEST_ID);
payloadId = System.getenv(EnvironmentVariables.CC_PAYLOAD_ID);
localRootPath = Constants.LOCAL_ROOT_PATH;
bucket = config.aws_bucket;// + Constants.RemoteRootPath;
root = System.getenv(EnvironmentVariables.CC_ROOT);
}
@Override
public String rootPath() {
return bucket;
}
@Override
public boolean handlesDataStoreType(StoreType storeType){
return this.storeType == storeType;
}
@Override
public boolean putObject(PutObjectInput input) {
String path = root + "/" + manifestId + "/" + input.getFileName() + "." + input.getFileExtension();
byte[] data;
switch(input.getObjectState()){
case LOCAL_DISK:
//read from local
File file = new File(path);
data = new byte[(int) file.length()];
try(FileInputStream fis = new FileInputStream(file)) {
fis.read(data);
}
catch(Exception e){
//@TODOprint?
}
uploadToS3(config.aws_bucket, path, data);
break;
case MEMORY:
data = input.getData();
uploadToS3(config.aws_bucket, path, data);
break;
default:
return false;
}
return true;
}
@Override
public boolean pullObject(PullObjectInput input) {
String path = root + "/" + manifestId + "/" + input.getFileName() + "." + input.getFileExtension();
byte[] data;
String localPath = input.getDestRootPath() + "/" + input.getFileName() + "." + input.getFileExtension();
try {
//get the object from s3
data = downloadBytesFromS3(path);
//create localpath writer
InputStream stream = new ByteArrayInputStream(data);
//write it.
writeInputStreamToDisk(stream, localPath);
} catch (Exception e) {
return false;
}
return false;
}
private void writeInputStreamToDisk(InputStream input, String outputDestination) throws IOException {
String directory = new File(outputDestination).getParent();
File f = new File(directory);
if(!f.exists()){
f.mkdirs();
}
byte[] bytes = input.readAllBytes();
try (OutputStream os = new FileOutputStream(new File(outputDestination))) {
os.write(bytes);
} catch (Exception e) {
e.printStackTrace();
};
}
@Override
public byte[] getObject(GetObjectInput input) throws AmazonS3Exception {
String path = root + "/" + payloadId + "/" + input.getFileName() + "." + input.getFileExtension();
byte[] data;
try {
data = downloadBytesFromS3(path);
} catch (Exception e) {
throw new AmazonS3Exception(e.toString());
}
return data;
}
@Override
public Payload getPayload() throws AmazonS3Exception {
String filepath = root + "/" + payloadId + "/" + Constants.PAYLOAD_FILE_NAME;
try{
byte[] body = downloadBytesFromS3(filepath);
return readJsonModelPayloadFromBytes(body);
} catch (Exception e){
throw new AmazonS3Exception(e.toString());
}
}
private byte[] downloadBytesFromS3(String key) throws Exception{
S3Object fullObject = null;
boolean spaces = key.contains(" ");
if(spaces){
key = "\""+ key + "\"";
}
System.out.println(key);
System.out.println(bucket);
try {
fullObject = awsS3.getObject(new GetObjectRequest(bucket, key));
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
return fullObject.getObjectContent().readAllBytes();
} catch (Exception e) {
throw e;
} finally {
// To ensure that the network connection doesn't remain open, close any open input streams.
if (fullObject != null) {
try {
fullObject.close();
} catch (Exception e) {
throw e;
}
}
}
}
private Payload readJsonModelPayloadFromBytes(byte[] bytes) throws Exception {
final ObjectMapper mapper = new ObjectMapper(); // jackson databind
try {
return mapper.readValue(bytes, Payload.class);
} catch (Exception e) {
throw e;
}
}
private void uploadToS3(String bucketName, String objectKey, byte[] fileBytes) {
try {
//File file = new File(objectPath);
InputStream stream = new ByteArrayInputStream(fileBytes);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(fileBytes.length);
PutObjectRequest putOb = new PutObjectRequest(bucketName, objectKey,stream, meta);
PutObjectResult response = awsS3.putObject(putOb);
System.out.println(response.getETag());
} catch (SdkBaseException e) {
System.out.println(e.getMessage());
}
}
}