Categorygithub.com/dmitrymomot/filemanager
repositorypackage
0.1.3
Repository: https://github.com/dmitrymomot/filemanager.git
Documentation: pkg.go.dev

# README

FileManager Package for S3-Compatible Storage

GitHub tag (latest SemVer) Go Reference License

Tests CodeQL Analysis GolangCI Lint Go Report Card

The filemanager package provides a convenient and efficient way to interact with AWS S3 or S3-compatible storage services in Go. It simplifies common operations such as uploading files from various sources, removing files, and managing content within S3 buckets.

Features

  • File Uploads: Upload files directly from byte slices, multipart forms, or URLs.
  • File Removal: Remove individual files or all files within a directory.
  • S3 Integration: Seamlessly integrates with AWS S3 and other S3-compatible services.
  • Content-Type Detection: Automatically detects and sets the MIME type for uploaded files.
  • Customizable Settings: Configurable for different bucket names, paths, and size limits.

Installation

To install the package, use the following go get command:

go get github.com/dmitrymomot/filemanager

Configuration

Before using the FileManager, set up your configuration with AWS credentials, bucket details, and other preferences:

import "github.com/dmitrymomot/filemanager"

config := filemanager.Config{
    FileManagerKey:    "your-access-key-id",
    FileManagerSecret: "your-secret-access-key",
    CDNURL:            "your-cdn-url",
    BasePath:          "your-base-path",
    Endpoint:          "your-s3-endpoint",
    Region:            "your-region",
    Bucket:            "your-bucket-name",
    MaxFileSize:       64 << 20, // e.g., 64MB
}

fm, err := filemanager.New(config)
if err != nil {
    log.Fatal(err)
}

Usage

Uploading Files

Upload a file from an HTTP request:

http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
    url, err := fm.UploadFromMultipartForm(r, "fileFieldName")
    if err != nil {
        // handle error
    }

    fmt.Fprintf(w, "File uploaded successfully: %s", url)
})

Upload a file from a URL:

url, err := fm.UploadFromURL(context.Background(), "https://example.com/path/to/file")
if err != nil {
    // handle error
}

Removing Files

Remove a specific file:

err := fm.Remove(context.Background(), "fileURL")
if err != nil {
    // handle error
}

Remove all files in a directory:

err := fm.RemoveFilesFromDirectory(context.Background(), "directoryPath")
if err != nil {
    // handle error
}

Contributing

Contributions to the filemanager package are welcome! Here are some ways you can contribute:

  • Reporting bugs
  • Additional tests cases
  • Suggesting enhancements
  • Submitting pull requests
  • Sharing the love by telling others about this project

License

This package is licensed under the Apache 2.0 - see the LICENSE file for details.