44import com .baidubce .services .bos .BosClient ;
55import com .baidubce .services .bos .BosClientConfiguration ;
66import com .baidubce .services .bos .model .BosObject ;
7+ import com .baidubce .services .bos .model .ObjectMetadata ;
8+ import com .baidubce .services .bos .model .PutObjectResponse ;
79import com .robin .core .base .exception .MissingConfigException ;
10+ import com .robin .core .base .util .Const ;
811import com .robin .core .base .util .ResourceConst ;
9- import com .robin .core .fileaccess .fs .AbstractFileSystemAccessor ;
1012import com .robin .core .fileaccess .meta .DataCollectionMeta ;
13+ import com .robin .core .fileaccess .util .ResourceUtil ;
1114import lombok .Getter ;
12- import lombok .extern .slf4j .Slf4j ;
13- import org .apache .commons .lang3 .tuple .Pair ;
15+ import org .apache .commons .io .FileUtils ;
1416import org .springframework .util .Assert ;
1517import org .springframework .util .CollectionUtils ;
1618import org .springframework .util .ObjectUtils ;
1719
1820import java .io .*;
21+ import java .nio .file .Files ;
22+ import java .nio .file .Paths ;
1923
20- @ Slf4j
2124@ Getter
2225@ SuppressWarnings ("unused" )
23- public class BOSFileSystemAccessor extends AbstractFileSystemAccessor {
26+ public class BOSFileSystemAccessor extends AbstractCloudStorageFileSystemAccessor {
2427 private String endpoint ;
2528 private String accessKeyId ;
2629 private String securityAccessKey ;
27- private String bucketName ;
2830 private BosClient client ;
31+ private BOSFileSystemAccessor (){
32+ this .identifier = Const .FILESYSTEM .BAIDU_BOS .getValue ();
33+ }
2934
3035 @ Override
3136 public void init (DataCollectionMeta meta ) {
@@ -52,63 +57,21 @@ public void init(){
5257 client =new BosClient (config );
5358 }
5459
55- @ Override
56- public Pair <BufferedReader , InputStream > getInResourceByReader (DataCollectionMeta meta , String resourcePath ) throws IOException {
57- InputStream inputStream = getInputStreamByConfig (meta );
58- return Pair .of (getReaderByPath (resourcePath , inputStream , meta .getEncode ()),inputStream );
59- }
60-
61- @ Override
62- public Pair <BufferedWriter , OutputStream > getOutResourceByWriter (DataCollectionMeta meta , String resourcePath ) throws IOException {
63- OutputStream outputStream = getOutputStream (meta );
64- return Pair .of (getWriterByPath (meta .getPath (), outputStream , meta .getEncode ()),outputStream );
65- }
66-
67- @ Override
68- public OutputStream getRawOutputStream (DataCollectionMeta meta , String resourcePath ) throws IOException {
69- return getOutputStream (meta );
70- }
71-
72- @ Override
73- public OutputStream getOutResourceByStream (DataCollectionMeta meta , String resourcePath ) throws IOException {
74- return getOutputStreamByPath (resourcePath , getOutputStream (meta ));
75- }
76-
77- @ Override
78- public InputStream getInResourceByStream (DataCollectionMeta meta , String resourcePath ) throws IOException {
79- InputStream inputStream = getInputStreamByConfig (meta );
80- return getInputStreamByPath (resourcePath , inputStream );
81- }
82-
83- @ Override
84- public InputStream getRawInputStream (DataCollectionMeta meta , String resourcePath ) throws IOException {
85- return getInputStreamByConfig (meta );
86- }
87-
8860 @ Override
8961 public boolean exists (DataCollectionMeta meta , String resourcePath ) throws IOException {
90- String bucketName = getBucketName (meta );
91- return client .doesObjectExist (bucketName ,resourcePath );
62+ return client .doesObjectExist (getBucketName (meta ),resourcePath );
9263 }
9364
9465 @ Override
9566 public long getInputStreamSize (DataCollectionMeta meta , String resourcePath ) throws IOException {
96- String bucketName = getBucketName (meta );
9767 if (exists (meta ,resourcePath )){
98- BosObject object =client .getObject (bucketName ,resourcePath );
68+ BosObject object =client .getObject (getBucketName ( meta ) ,resourcePath );
9969 return object .getObjectMetadata ().getContentLength ();
10070 }
10171 return 0 ;
10272 }
103- private InputStream getInputStreamByConfig (DataCollectionMeta meta ) {
104- String bucketName = getBucketName (meta );
105- String objectName = meta .getPath ();
106- return getObject (bucketName ,objectName );
107- }
108- private String getBucketName (DataCollectionMeta meta ) {
109- return !ObjectUtils .isEmpty (bucketName )?bucketName :meta .getResourceCfgMap ().get (ResourceConst .BUCKETNAME ).toString ();
110- }
111- private InputStream getObject (String bucketName ,String objectName ){
73+
74+ protected InputStream getObject (String bucketName ,String objectName ){
11275 if (client .doesObjectExist (bucketName ,objectName )) {
11376 BosObject object = client .getObject (bucketName , objectName );
11477 if (!ObjectUtils .isEmpty (object )) {
@@ -120,6 +83,32 @@ private InputStream getObject(String bucketName,String objectName){
12083 throw new MissingConfigException (" key " +objectName +" not in OSS bucket " +bucketName );
12184 }
12285 }
86+
87+ @ Override
88+ protected boolean putObject (String bucketName , DataCollectionMeta meta , OutputStream outputStream ) throws IOException {
89+ PutObjectResponse result ;
90+ ObjectMetadata metadata =new ObjectMetadata ();
91+ String tmpFilePath =null ;
92+ if (!ObjectUtils .isEmpty (meta .getContent ())){
93+ metadata .setContentType (meta .getContent ().getContentType ());
94+ }
95+ if (ByteArrayOutputStream .class .isAssignableFrom (outputStream .getClass ())) {
96+ ByteArrayOutputStream byteArrayOutputStream =(ByteArrayOutputStream )outputStream ;
97+ metadata .setContentLength (byteArrayOutputStream .size ());
98+ result = client .putObject (bucketName , meta .getPath (), new ByteArrayInputStream (byteArrayOutputStream .toByteArray ()),metadata );
99+ }else {
100+ outputStream .close ();
101+ String tmpPath = com .robin .core .base .util .FileUtils .getWorkingPath (meta );
102+ tmpFilePath = tmpPath + ResourceUtil .getProcessFileName (meta .getPath ());
103+ metadata .setContentLength (Files .size (Paths .get (tmpFilePath )));
104+ result =client .putObject (bucketName ,meta .getPath (), Files .newInputStream (Paths .get (tmpFilePath )),metadata );
105+ }
106+ if (ObjectUtils .isEmpty (tmpFilePath )){
107+ FileUtils .deleteQuietly (new File (tmpFilePath ));
108+ }
109+ return !ObjectUtils .isEmpty (result ) && !ObjectUtils .isEmpty (result .getETag ());
110+ }
111+
123112 public static class Builder {
124113 private BOSFileSystemAccessor accessor ;
125114 public Builder (){
0 commit comments