File Storage

File Storage #

There are two implementations for below IStorage interface: local_storage and s3_storage

// IStorage Storage interface
type IStorage interface {
    // -- Main actions
    
    // Put Create a file with content string
    Put(path, contents string, options ...interface{}) bool
    // PutFile Create a file from another file source
    PutFile(path string, fileSource *os.File, options ...interface{}) bool
    // Delete Remove a file
    Delete(path string) bool
    // Copy Clone file to another location
    Copy(from, to string) bool
    // Move Switch file location to new place
    Move(from, to string) bool
    
    //-- File manipulation
    
    // Exists Check existed file
    Exists(path string) bool
    // Get Receive a file content
    Get(path string) ([]byte, error)
    // Size Get file size
    Size(path string) int64
    // LastModified Obtains last modified of file
    LastModified(path string) time.Time
    
    //-- Directory
    
    // MakeDir Create new directory
    MakeDir(dir string) bool
    // DeleteDir Remove empty directory
    DeleteDir(dir string) bool
    
    //-- Data manipulation
    
    // Append Add string content to bottom file
    Append(path, data string) bool
}

Parameters in .env

# NOTE: Storage file system settings:
# FILESYSTEM_TYPE:
#   - "local" Local storage (Default).
#   - "s3" S3 storage.
FILESYSTEM_TYPE=local
STORAGE_DIR=./storage
...
# NOTE: AWS settings:
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-west-1
AWS_BUCKET=902-local

By default system will setting FILESYSTEM_TYPE=local to use local storage. And root storage folder is STORAGE_DIR=./storage.

IMPORTANT!
This parameter should not be changed STORAGE_DIR=./storage

Storage instance #

Import file system package

import (
    "app/core/filesystem"
)

Get default storage instance

fs filesystem.IStorage

fs = fs.New()

For some cases you want to create specific Storage

// S3 storage
fsS3 := fs.NewS3Storage()

// Local storage
fsLocal := fs.NewLocalStorage()

Usage #

Create and delete test folder #

fs.MakeDir("test")
fs.DeleteDir("test")

Put content to file #

filePath := "logs/logs.log"

fs.Put(filePath, "Hello\n")

Append content to file #

filePath := "logs/logs.log"

fs.Append(filePath, "Hello world new line\n")

Put a file content to file #

path := "storage/temp/old.png"
file, err := os.Open(filepath.Clean(path))
if err != nil {
    log.Infof("Couldn't open file %v. Here's why: %v\n", path, err)
}
defer func(file *os.File) {
    err := file.Close()
    if err != nil {
        log.Errorf("Unable to close file %q, %v", path, err)
    }
}(file)

// Put file
fs.PutFile("storage/temp/new.png", file)

Copy file #

from := "logs/logs.log"
to := "logs/logs-file.log"

// Copy file
fs.Copy(from, to)

Move file #

from := "logs/logs.log"
to := "logs/logs-file.log"

// Copy file
fs.Move(from, to)

Check exists #

path := "logs/logs.log"

// Copy file
if fs.Exists(path) {
    log.Infof("Do something file %s", path)
}

Read file #

path := "logs/logs.log"

// Get file
byteData, err := fs.Get(path)
if err != nil {
    log.Infof("Read file %s error", path)
}

File information #

path := "logs/logs.log"
var size int64
var lastModified time.Time

// Get file size
size = fs.Size(path)
// Get file modified
lastModified = fs.LastModified(path)

File information #

path := "logs/logs.log"

if fs.Delete(path) {
    log.Infof("Delete file %s", path)
}