11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34
45namespace TileDB . CSharp . Examples
56{
67 public class ExampleFile
78 {
89 private Context _ctx ;
910 private string _nameSpace ;
10-
11+
1112 // Local access
1213 public ExampleFile ( )
1314 {
1415 _ctx = Context . GetDefault ( ) ;
1516 _nameSpace = String . Empty ;
1617 }
17-
18+
1819 // S3 access
1920 public ExampleFile ( string key , string secret )
2021 {
@@ -24,7 +25,7 @@ public ExampleFile(string key, string secret)
2425 _ctx = new Context ( config ) ;
2526 _nameSpace = String . Empty ;
2627 }
27-
28+
2829 // TileDB Cloud access with token
2930 public ExampleFile ( string host , string token , string nameSpace )
3031 {
@@ -49,8 +50,21 @@ public ExampleFile(string host, string username, string password, string nameSpa
4950 public void SaveFileToArray ( string pathOrBucket , string arrayName , string fileToSave )
5051 {
5152 var arrayURI = string . Join ( "/" , new List < string > ( new string [ ] { pathOrBucket , arrayName } ) ) ;
53+
54+ // If no fileToSave exists, create one with some text data
55+ if ( ! System . IO . File . Exists ( fileToSave ) )
56+ {
57+ System . IO . File . WriteAllText ( fileToSave , "Some text data" ) ;
58+ }
59+
5260 if ( _nameSpace == String . Empty )
5361 {
62+ // If exported array exists, recreate it
63+ if ( Directory . Exists ( arrayURI ) )
64+ {
65+ Directory . Delete ( arrayURI , true ) ;
66+ }
67+
5468 FileStoreUtil . SaveFileToArray ( _ctx , arrayURI , fileToSave ) ;
5569 return ;
5670 }
@@ -80,24 +94,33 @@ public static void RunLocal()
8094 var localArrayName = "array_name" ;
8195 var localOutputFile = "local_out_file" ;
8296
97+ if ( ! Directory . Exists ( localPath ) )
98+ {
99+ Directory . CreateDirectory ( localPath ) ;
100+ }
101+
83102 var exampleFile = new ExampleFile ( ) ;
103+ // Import localFile to new TileDB Array
84104 exampleFile . SaveFileToArray ( localPath , localArrayName , localFile ) ;
105+ // Export TileDB Array to new localOutputFile
85106 exampleFile . ExportArrayToFile ( localOutputFile , localArrayName , localPath ) ;
86107 }
87108
88- public static void RunCloud ( )
109+ public static void RunCloud ( string token , string nameSpace , string cloudArrayName , string s3Bucket ,
110+ string host = "https://api.tiledb.com" )
89111 {
112+ // Local file to import to new TileDB Cloud Array
90113 var localFile = "local_file" ;
91- var bucket = "bucket" ;
92- var cloudArrayName = "cloud_array_name " ;
93- var localOutputFileFromCloud = "local_out" ;
94-
95- var exampleFile = new ExampleFile (
96- "http://localhost:8181" ,
97- " token" ,
98- "userNameOrOrganization"
99- ) ;
100- exampleFile . SaveFileToArray ( bucket , cloudArrayName , localFile ) ;
114+ // Local file to store exported TileDB Cloud Array
115+ var localOutputFileFromCloud = "local_out_cloud_file " ;
116+
117+ // Convert a local file to new TileDB Cloud Array
118+ // + Creates TileDB Array: tiledb://<nameSpace>/<cloudArrayName>
119+ // + Stored on S3: <s3bucket>/<cloudArrayName>
120+ var exampleFile = new ExampleFile ( host , token , nameSpace ) ;
121+ // Import localFile to new TileDB Cloud Array
122+ exampleFile . SaveFileToArray ( s3Bucket , cloudArrayName , localFile ) ;
123+ // Export TileDB Cloud Array to new local file
101124 exampleFile . ExportArrayToFile ( localOutputFileFromCloud , cloudArrayName ) ;
102125 }
103126 }
0 commit comments