11package com .ibm .ml .ilog .v4 ;
22
3- import com .ibm .json .java .JSON ;
43import com .ibm .json .java .JSONObject ;
54import com .ibm .ml .ilog .Credentials ;
65import com .ibm .ml .ilog .utils .HttpUtils ;
76import ilog .concert .IloException ;
87import org .apache .logging .log4j .LogManager ;
98import org .apache .logging .log4j .Logger ;
109
11- import java .io .IOException ;
1210import java .util .Date ;
13- import java .util .HashMap ;
14- import java .util .LinkedHashMap ;
1511import java .util .Map ;
1612
1713public class COSConnector extends HttpUtils implements com .ibm .ml .ilog .COSConnector {
1814
1915 private String _connectionId = null ;
16+ private String _bucketPath = null ;
2017 private static final Logger logger = LogManager .getLogger ();
18+ @ Override
19+ public void setBucketPath (String path ) throws IloException {
20+ if (_bucketPath != null ) throw new IloException ("Path in bucket already exist" );
21+ _bucketPath = path ;
22+ }
2123
2224 @ Override
2325 public void end () {
@@ -27,9 +29,8 @@ public void end() {
2729 Map <String , String > headers = getPlatformHeaders ();
2830
2931 long t1 = new Date ().getTime ();
30- String res = null ;
3132 try {
32- res = doDelete (
33+ doDelete (
3334 wml_credentials .get (Credentials .PLATFORM_HOST ),
3435 V2_CONNECTIONS +"/" +_connectionId ,
3536 params , headers );
@@ -44,6 +45,29 @@ public void end() {
4445 super .end ();
4546 }
4647
48+ public void deleteConnection () {
49+ if (_connectionId != null ) {
50+ Map <String , String > params = getWMLParams ();
51+
52+ Map <String , String > headers = getPlatformHeaders ();
53+
54+ long t1 = new Date ().getTime ();
55+ String res = null ;
56+ try {
57+ res = doDelete (
58+ wml_credentials .get (Credentials .PLATFORM_HOST ),
59+ V2_CONNECTIONS + "/" + _connectionId ,
60+ params , headers );
61+ } catch (IloException e ) {
62+ logger .error (e .getMessage ());
63+ }
64+ long t2 = new Date ().getTime ();
65+ logger .info ("Connection Id = " + _connectionId );
66+ logger .info ("Deleting the connection took " + (t2 - t1 ) / 1000 + " seconds." );
67+ _connectionId = null ;
68+ }
69+ }
70+
4771 private void checkCredentials () throws IloException {
4872 for (String k : Credentials .COSFields ) {
4973 if (!wml_credentials .containsKey (k )) {
@@ -66,25 +90,27 @@ public JSONObject getDataReferences(String id) throws IloException {
6690
6791 String cos_bucket = wml_credentials .get (Credentials .COS_BUCKET );
6892
93+ String filename = "\" file_name\" : \" " ;
94+ if (_bucketPath != null )
95+ filename = filename + _bucketPath + "/" ;
96+ filename = filename + id + "\" \n " ;
6997 String data = "{\n " +
7098 //"\"id\": \"" + id + "\",\n" +
7199 "\" type\" : \" connection_asset\" ,\n " +
72100 "\" id\" : \" " + id + "\" ,\n " +
73101 "\" connection\" : {\n " +
74- "\" id\" : \" " + getConnection () + "\" \n " +
102+ "\" id\" : \" " + getConnectionId () + "\" \n " +
75103 "},\n " +
76104 "\" location\" : {\n " +
77105 "\" bucket\" : \" " + cos_bucket + "\" ,\n " +
78- " \" file_name \" : \" " + id + " \" \n " +
106+ filename +
79107 "},\n " +
80108 "}\n " ;
81109 return parseJson (data );
82110 }
83111
84112 @ Override
85113 public String putFile (String fileName , String filePath ) throws IloException {
86- String cos_bucket = wml_credentials .get (Credentials .COS_BUCKET );
87-
88114 byte [] bytes = getFileContent (filePath );
89115
90116 Map <String , String > params = getPlatformParams ();
@@ -96,13 +122,22 @@ public String putFile(String fileName, String filePath) throws IloException {
96122 long t1 = new Date ().getTime ();
97123 String ret = doPut (
98124 wml_credentials .get (Credentials .COS_ENDPOINT ),
99- "/" + cos_bucket + "/" + fileName ,
125+ getTargetUrl ( fileName ) ,
100126 params , headers , bytes );
101127 long t2 = new Date ().getTime ();
102128 logger .info ("Uploading in COS took " + (t2 - t1 ) / 1000 + " seconds." );
103129 return ret ;
104130 }
105131
132+ private String getTargetUrl (String fileName ){
133+ String cos_bucket = wml_credentials .get (Credentials .COS_BUCKET );
134+ String targetUrl = "/" + cos_bucket + "/" ;
135+ if (_bucketPath != null )
136+ targetUrl = targetUrl + _bucketPath + "/" ;
137+ targetUrl = targetUrl + fileName ;
138+ return targetUrl ;
139+ }
140+
106141 private String getS3Id () throws IloException {
107142 Map <String , String > params = getWMLParams ();
108143 Map <String , String > headers = getPlatformHeaders ();
@@ -117,7 +152,7 @@ private String getS3Id() throws IloException{
117152
118153
119154 @ Override
120- public String getConnection () throws IloException {
155+ public String getConnectionId () throws IloException {
121156 if (_connectionId != null )
122157 return _connectionId ;
123158 long t1 = new Date ().getTime ();
@@ -158,14 +193,12 @@ public String getConnection() throws IloException {
158193
159194 @ Override
160195 public String getFile (String fileName ) throws IloException {
161- String cos_bucket = wml_credentials .get (Credentials .COS_BUCKET );
162-
163196 Map <String , String > params = getPlatformParams ();
164197 Map <String , String > headers = getPlatformHeaders ();
165198
166199 String res = doGet (
167200 wml_credentials .get (Credentials .COS_ENDPOINT ),
168- "/" + cos_bucket + "/" + fileName ,
201+ getTargetUrl ( fileName ) ,
169202 params ,
170203 headers );
171204
0 commit comments