|
| 1 | +package com.robin.comm.fileaccess.fs; |
| 2 | + |
| 3 | +import com.baidubce.auth.DefaultBceCredentials; |
| 4 | +import com.baidubce.services.bos.BosClient; |
| 5 | +import com.baidubce.services.bos.BosClientConfiguration; |
| 6 | +import com.baidubce.services.bos.model.BosObject; |
| 7 | +import com.robin.core.base.exception.MissingConfigException; |
| 8 | +import com.robin.core.base.util.ResourceConst; |
| 9 | +import com.robin.core.fileaccess.fs.AbstractFileSystemAccessor; |
| 10 | +import com.robin.core.fileaccess.meta.DataCollectionMeta; |
| 11 | +import lombok.Getter; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.apache.commons.lang3.tuple.Pair; |
| 14 | +import org.springframework.util.Assert; |
| 15 | +import org.springframework.util.CollectionUtils; |
| 16 | +import org.springframework.util.ObjectUtils; |
| 17 | + |
| 18 | +import java.io.*; |
| 19 | + |
| 20 | +@Slf4j |
| 21 | +@Getter |
| 22 | +public class BOSFileSystemAccessor extends AbstractFileSystemAccessor { |
| 23 | + private String endpoint; |
| 24 | + private String accessKeyId; |
| 25 | + private String securityAccessKey; |
| 26 | + private String bucketName; |
| 27 | + private BosClient client; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void init(DataCollectionMeta meta) { |
| 31 | + Assert.isTrue(!CollectionUtils.isEmpty(meta.getResourceCfgMap()),"config map is empty!"); |
| 32 | + Assert.notNull(meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.ENDPOIN.getValue()),"must provide endpoint"); |
| 33 | + Assert.notNull(meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.ACESSSKEYID.getValue()),"must provide accessKey"); |
| 34 | + Assert.notNull(meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.SECURITYACCESSKEY.getValue()),"must provide securityAccessKey"); |
| 35 | + |
| 36 | + endpoint=meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.ENDPOIN.getValue()).toString(); |
| 37 | + accessKeyId=meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.ACESSSKEYID.getValue()).toString(); |
| 38 | + securityAccessKey=meta.getResourceCfgMap().get(ResourceConst.BOSPARAM.SECURITYACCESSKEY.getValue()).toString(); |
| 39 | + BosClientConfiguration config=new BosClientConfiguration(); |
| 40 | + config.setCredentials(new DefaultBceCredentials(accessKeyId,securityAccessKey)); |
| 41 | + config.setEndpoint(endpoint); |
| 42 | + client=new BosClient(config); |
| 43 | + } |
| 44 | + public void init(){ |
| 45 | + Assert.notNull(endpoint,"must provide region"); |
| 46 | + Assert.notNull(accessKeyId,"must provide accessKey"); |
| 47 | + Assert.notNull(securityAccessKey,"must provide securityAccessKey"); |
| 48 | + BosClientConfiguration config=new BosClientConfiguration(); |
| 49 | + config.setCredentials(new DefaultBceCredentials(accessKeyId,securityAccessKey)); |
| 50 | + config.setEndpoint(endpoint); |
| 51 | + client=new BosClient(config); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Pair<BufferedReader, InputStream> getInResourceByReader(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 56 | + InputStream inputStream = getInputStreamByConfig(meta); |
| 57 | + return Pair.of(getReaderByPath(resourcePath, inputStream, meta.getEncode()),inputStream); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Pair<BufferedWriter, OutputStream> getOutResourceByWriter(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 62 | + OutputStream outputStream = getOutputStream(meta); |
| 63 | + return Pair.of(getWriterByPath(meta.getPath(), outputStream, meta.getEncode()),outputStream); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public OutputStream getRawOutputStream(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 68 | + return getOutputStream(meta); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public OutputStream getOutResourceByStream(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 73 | + return getOutputStreamByPath(resourcePath, getOutputStream(meta)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public InputStream getInResourceByStream(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 78 | + InputStream inputStream = getInputStreamByConfig(meta); |
| 79 | + return getInputStreamByPath(resourcePath, inputStream); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public InputStream getRawInputStream(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 84 | + return getInputStreamByConfig(meta); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean exists(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 89 | + String bucketName= getBucketName(meta); |
| 90 | + return client.doesObjectExist(bucketName,resourcePath); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public long getInputStreamSize(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 95 | + String bucketName= getBucketName(meta); |
| 96 | + if(exists(meta,resourcePath)){ |
| 97 | + BosObject object=client.getObject(bucketName,resourcePath); |
| 98 | + return object.getObjectMetadata().getContentLength(); |
| 99 | + } |
| 100 | + return 0; |
| 101 | + } |
| 102 | + private InputStream getInputStreamByConfig(DataCollectionMeta meta) { |
| 103 | + String bucketName= getBucketName(meta); |
| 104 | + String objectName= meta.getPath(); |
| 105 | + return getObject(bucketName,objectName); |
| 106 | + } |
| 107 | + private String getBucketName(DataCollectionMeta meta) { |
| 108 | + return !ObjectUtils.isEmpty(bucketName)?bucketName:meta.getResourceCfgMap().get(ResourceConst.BUCKETNAME).toString(); |
| 109 | + } |
| 110 | + private InputStream getObject(String bucketName,String objectName){ |
| 111 | + if(client.doesObjectExist(bucketName,objectName)) { |
| 112 | + BosObject object = client.getObject(bucketName, objectName); |
| 113 | + if (!ObjectUtils.isEmpty(object)) { |
| 114 | + return object.getObjectContent(); |
| 115 | + } else { |
| 116 | + throw new RuntimeException("objectName " + objectName + " can not get!"); |
| 117 | + } |
| 118 | + }else{ |
| 119 | + throw new MissingConfigException(" key "+objectName+" not in OSS bucket "+bucketName); |
| 120 | + } |
| 121 | + } |
| 122 | + public static class Builder{ |
| 123 | + private BOSFileSystemAccessor accessor; |
| 124 | + public Builder(){ |
| 125 | + accessor=new BOSFileSystemAccessor(); |
| 126 | + } |
| 127 | + public static Builder builder(){ |
| 128 | + return new Builder(); |
| 129 | + } |
| 130 | + public Builder accessKeyId(String accessKeyId){ |
| 131 | + accessor.accessKeyId=accessKeyId; |
| 132 | + return this; |
| 133 | + } |
| 134 | + public Builder endpoint(String endPoint){ |
| 135 | + accessor.endpoint=endPoint; |
| 136 | + return this; |
| 137 | + } |
| 138 | + public Builder securityAccessKey(String securityAccessKey){ |
| 139 | + accessor.securityAccessKey=securityAccessKey; |
| 140 | + return this; |
| 141 | + } |
| 142 | + public Builder withMetaConfig(DataCollectionMeta meta){ |
| 143 | + accessor.init(meta); |
| 144 | + return this; |
| 145 | + } |
| 146 | + public Builder bucket(String bucketName){ |
| 147 | + accessor.bucketName=bucketName; |
| 148 | + return this; |
| 149 | + } |
| 150 | + public BOSFileSystemAccessor build(){ |
| 151 | + if(ObjectUtils.isEmpty(accessor.getClient())){ |
| 152 | + accessor.init(); |
| 153 | + } |
| 154 | + return accessor; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments