Skip to content

Commit bb47849

Browse files
authored
Support S3ConfigStorage of AWS
1 parent bf8537a commit bb47849

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.zeppelin.storage;
19+
20+
import com.amazonaws.AmazonClientException;
21+
import com.amazonaws.ClientConfiguration;
22+
import com.amazonaws.ClientConfigurationFactory;
23+
import com.amazonaws.auth.AWSCredentialsProvider;
24+
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
25+
import com.amazonaws.services.s3.AmazonS3;
26+
import com.amazonaws.services.s3.AmazonS3Client;
27+
import com.amazonaws.services.s3.model.GetObjectRequest;
28+
import com.amazonaws.services.s3.model.PutObjectRequest;
29+
import com.amazonaws.services.s3.model.S3Object;
30+
import com.google.common.annotations.VisibleForTesting;
31+
import org.apache.commons.io.FileUtils;
32+
import org.apache.commons.io.IOUtils;
33+
import org.apache.zeppelin.conf.ZeppelinConfiguration;
34+
import org.apache.zeppelin.interpreter.InterpreterInfoSaving;
35+
import org.apache.zeppelin.notebook.NotebookAuthorizationInfoSaving;
36+
import org.slf4j.Logger;
37+
import org.slf4j.LoggerFactory;
38+
39+
import java.io.*;
40+
41+
/**
42+
* Storing config in aws s3 file system
43+
*/
44+
public class S3ConfigStorage extends ConfigStorage {
45+
46+
47+
private static Logger LOGGER = LoggerFactory.getLogger(S3ConfigStorage.class);
48+
49+
private AmazonS3 s3client;
50+
private String bucketName;
51+
private String user;
52+
private String rootFolder;
53+
private String interpreterSettingPath;
54+
private String authorizationPath;
55+
56+
57+
58+
public S3ConfigStorage(ZeppelinConfiguration zConf) {
59+
super(zConf);
60+
bucketName = zConf.getS3BucketName();
61+
user = zConf.getS3User();
62+
rootFolder = user + "/conf";
63+
this.interpreterSettingPath = rootFolder + "/interpreter.json";
64+
this.authorizationPath = rootFolder + "/notebook-authorization.json";
65+
66+
// always use the default provider chain
67+
AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
68+
69+
ClientConfigurationFactory configFactory = new ClientConfigurationFactory();
70+
ClientConfiguration cliConf = configFactory.getConfig();
71+
72+
// regular S3
73+
this.s3client = new AmazonS3Client(credentialsProvider, cliConf);
74+
75+
// set S3 endpoint to use
76+
s3client.setEndpoint(zConf.getS3Endpoint());
77+
}
78+
79+
@Override
80+
public void save(InterpreterInfoSaving settingInfos) throws IOException {
81+
LOGGER.info("Save Interpreter Setting to s3://{}/{}", this.bucketName, this.interpreterSettingPath);
82+
saveToS3(settingInfos.toJson(), interpreterSettingPath,"zeppelin-interpreter");
83+
}
84+
85+
@Override
86+
public InterpreterInfoSaving loadInterpreterSettings() throws IOException {
87+
LOGGER.info("Load Interpreter Setting from s3 Path: " + interpreterSettingPath);
88+
String json = readFromS3(interpreterSettingPath);
89+
return buildInterpreterInfoSaving(json);
90+
}
91+
92+
@Override
93+
public void save(NotebookAuthorizationInfoSaving authorizationInfoSaving) throws IOException {
94+
LOGGER.info("Save notebook authorization to s3://{}/{} ",this.bucketName,this.authorizationPath);
95+
saveToS3(authorizationInfoSaving.toJson(), authorizationPath,"notebook-authorization");
96+
}
97+
98+
@Override
99+
public NotebookAuthorizationInfoSaving loadNotebookAuthorization() throws IOException {
100+
LOGGER.info("Load notebook authorization from s3 Path: " + interpreterSettingPath);
101+
String json = readFromS3(interpreterSettingPath);
102+
return NotebookAuthorizationInfoSaving.fromJson(json);
103+
}
104+
105+
@Override
106+
public String loadCredentials() throws IOException {
107+
return null;
108+
}
109+
110+
@Override
111+
public void saveCredentials(String credentials) throws IOException {
112+
113+
}
114+
115+
@VisibleForTesting
116+
void saveToS3(String content, String s3Path,String tempFileName) throws IOException {
117+
File file = File.createTempFile(tempFileName, "zpln");
118+
try {
119+
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
120+
writer.write(content);
121+
writer.close();
122+
PutObjectRequest putRequest = new PutObjectRequest(bucketName, s3Path, file);
123+
s3client.putObject(putRequest);
124+
}
125+
catch (AmazonClientException ace) {
126+
throw new IOException("Fail to store " + tempFileName + ": " + s3Path + " in S3", ace);
127+
}
128+
finally {
129+
FileUtils.deleteQuietly(file);
130+
}
131+
}
132+
133+
@VisibleForTesting
134+
String readFromS3( String filePath) throws IOException {
135+
S3Object s3object;
136+
try {
137+
s3object = s3client.getObject(new GetObjectRequest(bucketName,
138+
filePath));
139+
}
140+
catch (AmazonClientException ace) {
141+
throw new IOException("Fail to get file: " + filePath + " from S3", ace);
142+
}
143+
try (InputStream ins = s3object.getObjectContent()) {
144+
return IOUtils.toString(ins, zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_ENCODING));
145+
}
146+
}
147+
148+
}

0 commit comments

Comments
 (0)