|
| 1 | +package com.robin.comm.fileaccess.fs; |
| 2 | + |
| 3 | +import com.google.auth.oauth2.GoogleCredentials; |
| 4 | +import com.google.cloud.storage.*; |
| 5 | +import com.google.common.collect.Lists; |
| 6 | +import com.robin.core.base.exception.MissingConfigException; |
| 7 | +import com.robin.core.base.util.ResourceConst; |
| 8 | +import com.robin.core.fileaccess.meta.DataCollectionMeta; |
| 9 | +import lombok.NonNull; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.util.Assert; |
| 12 | +import org.springframework.util.CollectionUtils; |
| 13 | +import org.springframework.util.ObjectUtils; |
| 14 | + |
| 15 | +import java.io.*; |
| 16 | +import java.nio.channels.Channels; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.List; |
| 19 | +/** |
| 20 | + * Google Cloud Storage FileSystemAccessor,must init individual |
| 21 | + */ |
| 22 | +@Slf4j |
| 23 | +public class GCSFileSystemAccessor extends AbstractCloudStorageFileSystemAccessor{ |
| 24 | + private GoogleCredentials credentials; |
| 25 | + private String credentialsFile; |
| 26 | + private List<String> scopes; |
| 27 | + private Storage storage; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void init(DataCollectionMeta meta) { |
| 31 | + Assert.notNull(meta.getResourceCfgMap().get(ResourceConst.GCSPARAM.CREDENTIALSFILE.getValue()), "must provide credentialsFile"); |
| 32 | + credentialsFile=meta.getResourceCfgMap().get(ResourceConst.GCSPARAM.CREDENTIALSFILE.getValue()).toString(); |
| 33 | + if(!ObjectUtils.isEmpty(meta.getResourceCfgMap().get(ResourceConst.GCSPARAM.SCOPES.getValue()))){ |
| 34 | + scopes= Lists.newArrayList(meta.getResourceCfgMap().get(ResourceConst.GCSPARAM.SCOPES.getValue()).toString().split(",")); |
| 35 | + } |
| 36 | + try{ |
| 37 | + credentials=GoogleCredentials.fromStream(new FileInputStream(credentialsFile)); |
| 38 | + if(!CollectionUtils.isEmpty(scopes)){ |
| 39 | + credentials.createScoped(scopes); |
| 40 | + } |
| 41 | + storage= StorageOptions.newBuilder().setCredentials(credentials).build().getService(); |
| 42 | + }catch (IOException ex){ |
| 43 | + log.error("{}",ex.getMessage()); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + public void init() { |
| 48 | + Assert.notNull(credentialsFile, "must provide credentialsFile"); |
| 49 | + try{ |
| 50 | + credentials=GoogleCredentials.fromStream(new FileInputStream(credentialsFile)); |
| 51 | + if(!CollectionUtils.isEmpty(scopes)){ |
| 52 | + credentials.createScoped(scopes); |
| 53 | + } |
| 54 | + storage= StorageOptions.newBuilder().setCredentials(credentials).build().getService(); |
| 55 | + }catch (IOException ex){ |
| 56 | + log.error("{}",ex.getMessage()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean exists(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 62 | + checkStorage(meta); |
| 63 | + BlobId blobId=BlobId.of(getBucketName(meta),meta.getPath()); |
| 64 | + Blob blob=storage.get(blobId); |
| 65 | + return blob.exists(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public long getInputStreamSize(DataCollectionMeta meta, String resourcePath) throws IOException { |
| 70 | + checkStorage(meta); |
| 71 | + BlobId blobId=BlobId.of(getBucketName(meta),meta.getPath()); |
| 72 | + Blob blob=storage.get(blobId); |
| 73 | + if(blob.exists()){ |
| 74 | + return blob.getSize(); |
| 75 | + } |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected boolean putObject(String bucketName, DataCollectionMeta meta, OutputStream outputStream) throws IOException { |
| 81 | + checkStorage(meta); |
| 82 | + BlobId blobId=BlobId.of(getBucketName(meta),meta.getPath()); |
| 83 | + String contentType=!ObjectUtils.isEmpty(meta.getContent().getContentType())?meta.getContent().getContentType():"application/octet-stream"; |
| 84 | + BlobInfo blobInfo= BlobInfo.newBuilder(blobId).setContentType(contentType).build(); |
| 85 | + Blob blob; |
| 86 | + if(ByteArrayOutputStream.class.isAssignableFrom(outputStream.getClass())) { |
| 87 | + ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outputStream; |
| 88 | + blob = storage.create(blobInfo, byteArrayOutputStream.toByteArray()); |
| 89 | + }else{ |
| 90 | + outputStream.close(); |
| 91 | + blob=storage.createFrom(blobInfo,Paths.get(tmpFilePath)); |
| 92 | + } |
| 93 | + meta.getResourceCfgMap().put(ResourceConst.GCSPARAM.SELFLINK.getValue(),blob.getSelfLink()); |
| 94 | + return !ObjectUtils.isEmpty(blob.getEtag()); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + @Override |
| 100 | + protected InputStream getObject(String bucketName, String objectName) { |
| 101 | + if(ObjectUtils.isEmpty(bucketName)){ |
| 102 | + throw new MissingConfigException("bucketName "+bucketName+" does not exists!"); |
| 103 | + } |
| 104 | + BlobId blobId=BlobId.of(bucketName,objectName); |
| 105 | + Blob blob=storage.get(blobId); |
| 106 | + if(blob.exists()){ |
| 107 | + return Channels.newInputStream(blob.reader()); |
| 108 | + }else { |
| 109 | + throw new MissingConfigException("objectName " + objectName + " can not get!"); |
| 110 | + } |
| 111 | + } |
| 112 | + private void checkStorage(DataCollectionMeta meta) { |
| 113 | + Assert.notNull(storage,"storage not initialized"); |
| 114 | + Bucket bucket=storage.get(getBucketName(meta)); |
| 115 | + if(ObjectUtils.isEmpty(bucket)){ |
| 116 | + throw new MissingConfigException("bucketName "+getBucketName(meta)+" does not exists!"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static class Builder{ |
| 121 | + private GCSFileSystemAccessor accessor; |
| 122 | + public static S3FileSystemAccessor.Builder builder(){ |
| 123 | + return new S3FileSystemAccessor.Builder(); |
| 124 | + } |
| 125 | + public Builder(){ |
| 126 | + accessor=new GCSFileSystemAccessor(); |
| 127 | + } |
| 128 | + public GCSFileSystemAccessor.Builder credentialsFile(String credentialsFile){ |
| 129 | + accessor.credentialsFile=credentialsFile; |
| 130 | + return this; |
| 131 | + } |
| 132 | + public GCSFileSystemAccessor.Builder scopes(@NonNull String scopes){ |
| 133 | + Assert.notNull(scopes,"must provided scopes"); |
| 134 | + accessor.scopes=Lists.newArrayList(scopes.split(",")); |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public GCSFileSystemAccessor.Builder bucket(String bucketName){ |
| 139 | + accessor.bucketName=bucketName; |
| 140 | + return this; |
| 141 | + } |
| 142 | + public GCSFileSystemAccessor.Builder withMetaConfig(DataCollectionMeta meta){ |
| 143 | + accessor.init(meta); |
| 144 | + return this; |
| 145 | + } |
| 146 | + public GCSFileSystemAccessor build(){ |
| 147 | + if(!ObjectUtils.isEmpty(accessor.credentials)){ |
| 148 | + accessor.init(); |
| 149 | + } |
| 150 | + return accessor; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments