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.
- 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.
To install the package, use the following go get command:
go get github.com/dmitrymomot/filemanager
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)
}
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
}
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
}
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
This package is licensed under the Apache 2.0 - see the LICENSE
file for details.