You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/advanced-guide/handling-file/page.md
+92-18Lines changed: 92 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,15 @@ GoFr simplifies the complexity of working with different file stores by offering
6
6
7
7
By default, local file-store is initialized and user can access it from the context.
8
8
9
-
GoFr also supports FTP/SFTP file-store. Developers can also connect and use their AWS S3 bucket as a file-store. The file-store can be initialized as follows:
9
+
GoFr also supports FTP/SFTP file-store. Developers can also connect and use their cloud storage bucket as a file-store. Following cloud storage options are currently supported:
10
+
11
+
-**AWS S3**
12
+
-**Google Cloud Storage (GCS)**
13
+
14
+
The file-store can be initialized as follows:
10
15
11
16
### FTP file-store
17
+
12
18
```go
13
19
package main
14
20
@@ -34,6 +40,7 @@ func main() {
34
40
```
35
41
36
42
### SFTP file-store
43
+
37
44
```go
38
45
package main
39
46
@@ -60,8 +67,7 @@ func main() {
60
67
### AWS S3 Bucket as File-Store
61
68
62
69
To run S3 File-Store locally we can use localstack,
63
-
``docker run --rm -it -p 4566:4566 -p 4510-4559:4510-4559 localstack/localstack``
64
-
70
+
`docker run --rm -it -p 4566:4566 -p 4510-4559:4510-4559 localstack/localstack`
65
71
66
72
```go
67
73
package main
@@ -90,17 +96,68 @@ func main() {
90
96
app.Run()
91
97
}
92
98
```
93
-
> Note: The current implementation supports handling only one bucket at a time,
94
-
> as shown in the example with `gofr-bucket-2`. Bucket switching mid-operation is not supported.
99
+
100
+
> Note: The current implementation supports handling only one bucket at a time,
101
+
> as shown in the example with `gofr-bucket-2`. Bucket switching mid-operation is not supported.
102
+
103
+
### Google Cloud Storage (GCS) Bucket as File-Store
104
+
105
+
To run GCS File-Store locally we can use fake-gcs-server:
106
+
`docker run -it --rm -p 4443:4443 -e STORAGE_EMULATOR_HOST=0.0.0.0:4443 fsouza/fake-gcs-server:latest`
log.Fatalf("Failed to read credentials file: %v", err)
141
+
}
142
+
return data
143
+
}
144
+
145
+
```
146
+
147
+
> **Note:** When connecting to the actual GCS service, authentication can be provided via CredentialsJSON or the GOOGLE_APPLICATION_CREDENTIALS environment variable.
148
+
> When using fake-gcs-server, authentication is not required.
149
+
> Currently supports one bucket per file-store instance.
To switch to another directory in same parent directory
180
+
122
181
```go
123
182
currentDir, err:= ctx.File.Chdir("../my_dir2")
124
183
```
125
184
126
185
To switch to a subfolder of the current directory
186
+
127
187
```go
128
188
currentDir, err:= ctx.File.Chdir("sub_dir")
129
189
```
190
+
130
191
> Note: This method attempts to change the directory, but S3's flat structure and fixed bucket
131
-
> make this operation inapplicable.
192
+
> make this operation inapplicable. Similarly, GCS uses a flat structure where directories are simulated through object prefixes.
132
193
133
194
### Read a Directory
134
195
135
-
The ReadDir function reads the specified directory and returns a sorted list of its entries as FileInfo objects. Each FileInfo object provides access to its associated methods, eliminating the need for additional stat calls.
196
+
The ReadDir function reads the specified directory and returns a sorted list of its entries as FileInfo objects. Each FileInfo object provides access to its associated methods, eliminating the need for additional stat calls.
136
197
137
198
If an error occurs during the read operation, ReadDir returns the successfully read entries up to the point of the error along with the error itself. Passing "." as the directory argument returns the entries for the current directory.
199
+
138
200
```go
139
201
entries, err:= ctx.File.ReadDir("../testdir")
140
202
@@ -143,12 +205,13 @@ for _, entry := range entries {
143
205
144
206
if entry.IsDir() {
145
207
entryType = "Dir"
146
-
}
208
+
}
147
209
148
210
fmt.Printf("%v: %v Size: %v Last Modified Time : %v\n", entryType, entry.Name(), entry.Size(), entry.ModTime())
149
211
}
150
212
```
151
-
> Note: In S3, directories are represented as prefixes of file keys. This method retrieves file
213
+
214
+
> Note: In S3 and GCS, directories are represented as prefixes of file keys/object names. This method retrieves file
152
215
> entries only from the immediate level within the specified directory.
GoFr support reading CSV/JSON/TEXT files line by line.
167
231
168
232
```go
169
233
reader, err:= file.ReadAll()
170
234
171
235
for reader.Next() {
172
236
varbstring
173
-
237
+
174
238
// For reading CSV/TEXT files user need to pass pointer to string to SCAN.
175
239
// In case of JSON user should pass structs with JSON tags as defined in encoding/json.
176
240
err = reader.Scan(&b)
@@ -179,10 +243,12 @@ for reader.Next() {
179
243
}
180
244
```
181
245
182
-
183
246
### Opening and Reading Content from a File
247
+
184
248
To open a file with default settings, use the `Open` command, which provides read and seek permissions only. For write permissions, use `OpenFile` with the appropriate file modes.
249
+
185
250
> Note: In FTP, file permissions are not differentiated; both `Open` and `OpenFile` allow all file operations regardless of specified permissions.
251
+
186
252
```go
187
253
csvFile, _:= ctx.File.Open("my_file.csv")
188
254
@@ -205,6 +271,7 @@ if err != nil {
205
271
### Getting Information of the file/directory
206
272
207
273
Stat retrieves details of a file or directory, including its name, size, last modified time, and type (such as whether it is a file or folder)
274
+
208
275
```go
209
276
file, _:= ctx.File.Stat("my_file.text")
210
277
entryType:="File"
@@ -215,10 +282,12 @@ if entry.IsDir() {
215
282
216
283
fmt.Printf("%v: %v Size: %v Last Modified Time : %v\n", entryType, entry.Name(), entry.Size(), entry.ModTime())
217
284
```
218
-
>Note: In S3:
285
+
286
+
> Note: In S3 and GCS:
287
+
>
219
288
> - Names without a file extension are treated as directories by default.
220
-
> - Names starting with "0" are interpreted as binary files, with the "0" prefix removed.
221
-
>
289
+
> - Names starting with "0" are interpreted as binary files, with the "0" prefix removed (S3 specific behavior).
290
+
>
222
291
> For directories, the method calculates the total size of all contained objects and returns the most recent modification time. For files, it directly returns the file's size and last modified time.
> Note: Currently, the S3 package supports the deletion of unversioned files from general-purpose buckets only. Directory buckets and versioned files are not supported for deletion by this method.
306
+
307
+
> Note: Currently, the S3 package supports the deletion of unversioned files from general-purpose buckets only. Directory buckets and versioned files are not supported for deletion by this method. GCS supports deletion of both files and empty directories.
308
+
238
309
```go
239
310
err:= ctx.File.Remove("my_dir")
240
311
```
241
312
242
313
The `RemoveAll` command deletes all subdirectories as well. If you delete the current working directory, such as "../currentDir", the working directory will be reset to its parent directory.
243
-
> Note: In S3, RemoveAll only supports deleting directories and will return an error if a file path (as indicated by a file extension) is provided.
314
+
315
+
> Note: In S3, RemoveAll only supports deleting directories and will return an error if a file path (as indicated by a file extension) is provided for S3.
316
+
> GCS handles both files and directories.
317
+
244
318
```go
245
319
err:= ctx.File.RemoveAll("my_dir/my_text")
246
320
```
247
321
248
-
> GoFr supports relative paths, allowing locations to be referenced relative to the current working directory. However, since S3 uses
249
-
> a flat file structure, all methods require a full path relative to the S3 bucket.
322
+
> GoFr supports relative paths, allowing locations to be referenced relative to the current working directory. However, since S3 and GCS use
323
+
> a flat file structure, all methods require a full path relative to the bucket.
250
324
251
325
> Errors have been skipped in the example to focus on the core logic, it is recommended to handle all the errors.
0 commit comments