|
| 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.aliyun.oss.OSS; |
| 21 | +import com.aliyun.oss.OSSClientBuilder; |
| 22 | +import com.aliyun.oss.model.OSSObject; |
| 23 | +import com.aliyun.oss.model.PutObjectRequest; |
| 24 | +import com.amazonaws.AmazonClientException; |
| 25 | +import com.google.common.annotations.VisibleForTesting; |
| 26 | +import org.apache.commons.io.IOUtils; |
| 27 | +import org.apache.zeppelin.conf.ZeppelinConfiguration; |
| 28 | +import org.apache.zeppelin.interpreter.InterpreterInfoSaving; |
| 29 | +import org.apache.zeppelin.notebook.NotebookAuthorizationInfoSaving; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.io.*; |
| 34 | + |
| 35 | +/** |
| 36 | + * Storing config in Aliyun OSS file system |
| 37 | + */ |
| 38 | +public class OSSConfigStorage extends ConfigStorage { |
| 39 | + |
| 40 | + |
| 41 | + private static Logger LOGGER = LoggerFactory.getLogger(OSSConfigStorage.class); |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + private OSS ossClient; |
| 46 | + private String bucketName; |
| 47 | + private String interpreterSettingPath; |
| 48 | + private String authorizationPath; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + public OSSConfigStorage(ZeppelinConfiguration zConf) { |
| 53 | + super(zConf); |
| 54 | + String endpoint = zConf.getOSSEndpoint(); |
| 55 | + bucketName = zConf.getOSSBucketName(); |
| 56 | + String rootFolder = zConf.getNotebookDir(); |
| 57 | + if (rootFolder.startsWith("/")) { |
| 58 | + rootFolder = rootFolder.substring(1); |
| 59 | + } |
| 60 | + this.interpreterSettingPath = rootFolder + "/interpreter.json"; |
| 61 | + this.authorizationPath = rootFolder + "/notebook-authorization.json"; |
| 62 | + String accessKeyId = zConf.getOSSAccessKeyId(); |
| 63 | + String accessKeySecret = zConf.getOSSAccessKeySecret(); |
| 64 | + this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void save(InterpreterInfoSaving settingInfos) throws IOException { |
| 69 | + LOGGER.info("Save Interpreter Setting to oss://{}/{}", this.bucketName, this.interpreterSettingPath); |
| 70 | + saveToOSS(settingInfos.toJson(), interpreterSettingPath); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public InterpreterInfoSaving loadInterpreterSettings() throws IOException { |
| 75 | + LOGGER.info("Load Interpreter Setting from oss Path: " + interpreterSettingPath); |
| 76 | + String json = readFromOSS(interpreterSettingPath); |
| 77 | + return buildInterpreterInfoSaving(json); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void save(NotebookAuthorizationInfoSaving authorizationInfoSaving) throws IOException { |
| 82 | + LOGGER.info("Save notebook authorization to oss://{}/{} ",this.bucketName,this.authorizationPath); |
| 83 | + saveToOSS(authorizationInfoSaving.toJson(), authorizationPath); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public NotebookAuthorizationInfoSaving loadNotebookAuthorization() throws IOException { |
| 88 | + LOGGER.info("Load notebook authorization from oss Path: " + interpreterSettingPath); |
| 89 | + String json = readFromOSS(interpreterSettingPath); |
| 90 | + return NotebookAuthorizationInfoSaving.fromJson(json); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String loadCredentials() throws IOException { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void saveCredentials(String credentials) throws IOException { |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @VisibleForTesting |
| 104 | + void saveToOSS(String content, String ossPath) throws IOException { |
| 105 | + try { |
| 106 | + PutObjectRequest putObjectRequest = new com.aliyun.oss.model.PutObjectRequest(bucketName, |
| 107 | + ossPath, new ByteArrayInputStream(content.getBytes())); |
| 108 | + ossClient.putObject(putObjectRequest); |
| 109 | + } |
| 110 | + catch (AmazonClientException ace) { |
| 111 | + throw new IOException("Fail to store " + ossPath + " in OSS", ace); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + @VisibleForTesting |
| 117 | + String readFromOSS( String filePath) throws IOException { |
| 118 | + |
| 119 | + OSSObject ossObject; |
| 120 | + try { |
| 121 | + ossObject = ossClient.getObject(bucketName, filePath); |
| 122 | + } |
| 123 | + catch (Exception e){ |
| 124 | + throw new IOException("Fail to get file: " + filePath + " from OSS", e); |
| 125 | + } |
| 126 | + |
| 127 | + try (InputStream in = ossObject.getObjectContent()){ |
| 128 | + return IOUtils.toString(in,zConf.getString(ZeppelinConfiguration.ConfVars.ZEPPELIN_ENCODING)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments