Skip to content

Commit 11ead52

Browse files
Add Azure file Support (#2605)
1 parent 2a12605 commit 11ead52

File tree

12 files changed

+4571
-8
lines changed

12 files changed

+4571
-8
lines changed

docs/advanced-guide/handling-file/page.md

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GoFr also supports FTP/SFTP file-store. Developers can also connect and use thei
1010

1111
- **AWS S3**
1212
- **Google Cloud Storage (GCS)**
13+
- **Azure File Storage**
1314

1415
The file-store can be initialized as follows:
1516

@@ -163,6 +164,49 @@ func readFile(filename string) []byte {
163164
> When using fake-gcs-server, authentication is not required.
164165
> Currently supports one bucket per file-store instance.
165166
167+
### Azure File Storage as File-Store
168+
169+
Azure File Storage provides fully managed file shares in the cloud. To use Azure File Storage with GoFr:
170+
171+
```go
172+
package main
173+
174+
import (
175+
"gofr.dev/pkg/gofr"
176+
"gofr.dev/pkg/gofr/datasource/file/azure"
177+
)
178+
179+
func main() {
180+
app := gofr.New()
181+
182+
// Create Azure File Storage filesystem
183+
fs, err := azure.New(&azure.Config{
184+
AccountName: "mystorageaccount",
185+
AccountKey: "myaccountkey",
186+
ShareName: "myshare",
187+
// Endpoint is optional, defaults to https://{AccountName}.file.core.windows.net
188+
// Endpoint: "https://custom-endpoint.file.core.windows.net",
189+
}, app.Logger(), app.Metrics())
190+
191+
if err != nil {
192+
app.Logger().Fatalf("Failed to initialize Azure File Storage: %v", err)
193+
}
194+
195+
app.AddFileStore(fs)
196+
197+
app.Run()
198+
}
199+
```
200+
201+
> **Note:**
202+
> - Azure File Storage uses file shares (similar to S3 buckets or GCS buckets)
203+
> - Authentication requires both `AccountName` and `AccountKey`
204+
> - The `Endpoint` field is optional and defaults to `https://{AccountName}.file.core.windows.net`
205+
> - Currently supports one file share per file-store instance
206+
> - The implementation automatically retries connection if the initial connection fails
207+
> - **Automatic parent directory creation**: When creating files in nested paths (e.g., `dir1/subdir/file.txt`), parent directories are automatically created, matching local filesystem behavior
208+
> - **Content type detection**: Content types are automatically detected based on file extensions (e.g., `.json``application/json`, `.txt``text/plain`)
209+
166210
### Creating Directory
167211

168212
To create a single directory
@@ -205,6 +249,7 @@ currentDir, err := ctx.File.Chdir("sub_dir")
205249

206250
> Note: This method attempts to change the directory, but S3's flat structure and fixed bucket
207251
> make this operation inapplicable. Similarly, GCS uses a flat structure where directories are simulated through object prefixes.
252+
> Azure File Storage supports directory operations natively, so `Chdir` works as expected.
208253
209254
### Read a Directory
210255

@@ -227,7 +272,8 @@ for _, entry := range entries {
227272
```
228273

229274
> Note: In S3 and GCS, directories are represented as prefixes of file keys/object names. This method retrieves file
230-
> entries only from the immediate level within the specified directory.
275+
> entries only from the immediate level within the specified directory. Azure File Storage supports native directory
276+
> structures, so `ReadDir` works with actual directories.
231277
232278
### Creating and Save a File with Content
233279

@@ -237,9 +283,14 @@ file, _ := ctx.File.Create("my_file.text")
237283
_, _ = file.Write([]byte("Hello World!"))
238284

239285
// Closes and saves the file.
240-
file.Close()
286+
file.Close()
241287
```
242288

289+
> **Note for Azure File Storage:**
290+
> - Files can be created in nested directories (e.g., `dir1/subdir/file.txt`). Parent directories are automatically created if they don't exist
291+
> - Content types are automatically detected based on file extensions (e.g., `.json`, `.txt`, `.csv`, `.xml`, `.html`, `.pdf`)
292+
> - This behavior matches local filesystem operations for consistency
293+
243294
### Reading file as CSV/JSON/TEXT
244295

245296
GoFr support reading CSV/JSON/TEXT files line by line.
@@ -304,6 +355,8 @@ fmt.Printf("%v: %v Size: %v Last Modified Time : %v\n", entryType, entry.Name(),
304355
> - Names starting with "0" are interpreted as binary files, with the "0" prefix removed (S3 specific behavior).
305356
>
306357
> 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.
358+
>
359+
> Azure File Storage supports native file and directory structures, so `Stat` returns accurate metadata for both files and directories.
307360
308361
### Rename/Move a File
309362

@@ -319,7 +372,7 @@ err := ctx.File.Rename("old_name.text", "new_name.text")
319372

320373
`Remove` deletes a single file
321374

322-
> 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.
375+
> 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. Azure File Storage supports deletion of both files and empty directories.
323376
324377
```go
325378
err := ctx.File.Remove("my_dir")
@@ -328,13 +381,14 @@ err := ctx.File.Remove("my_dir")
328381
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.
329382

330383
> 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.
331-
> GCS handles both files and directories.
384+
> GCS and Azure File Storage handle both files and directories.
332385
333386
```go
334387
err := ctx.File.RemoveAll("my_dir/my_text")
335388
```
336389

337390
> GoFr supports relative paths, allowing locations to be referenced relative to the current working directory. However, since S3 and GCS use
338-
> a flat file structure, all methods require a full path relative to the bucket.
391+
> a flat file structure, all methods require a full path relative to the bucket. Azure File Storage supports native directory structures,
392+
> so relative paths work as expected with directory navigation.
339393
340394
> Errors have been skipped in the example to focus on the core logic, it is recommended to handle all the errors.

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use (
1010
./pkg/gofr/datasource/dbresolver
1111
./pkg/gofr/datasource/dgraph
1212
./pkg/gofr/datasource/elasticsearch
13+
./pkg/gofr/datasource/file/azure
1314
./pkg/gofr/datasource/file/ftp
1415
./pkg/gofr/datasource/file/s3
1516
./pkg/gofr/datasource/file/gcs

0 commit comments

Comments
 (0)