filemanager

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

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.

Documentation

Index

Constants

View Source
const (
	// default access control list for new objects
	DefaultACL = "public-read"
	// default max file size for multipart form upload
	// 64MB
	DefaultMaxFileSize = 64 << 20 // 64MB
)

Variables

View Source
var (
	ErrInvalidS3ClientConfig               = errors.New("invalid S3 client config")
	ErrFailedToUploadFile                  = errors.New("failed to upload file")
	ErrFailedToRemoveFile                  = errors.New("failed to remove file")
	ErrFailedToCheckIfFileExists           = errors.New("failed to check if file exists")
	ErrFailedToRemoveFiles                 = errors.New("failed to remove files")
	ErrMissedBucketName                    = errors.New("missed bucket name")
	ErrMissedS3Client                      = errors.New("missed S3 client")
	ErrMissedCDNURL                        = errors.New("missed CDN URL")
	ErrInvalidCDNURL                       = errors.New("invalid CDN URL")
	ErrFailedToUploadFileFromURL           = errors.New("failed to upload file from URL")
	ErrFailedToUploadFileFromMultipartForm = errors.New("failed to upload file from multipart form")
	ErrNotFound                            = errors.New("not found")
	ErrUnexpected                          = errors.New("unexpected error")
	ErrMissedHTTPClient                    = errors.New("missed HTTP client")
)

Predefined errors.

Functions

This section is empty.

Types

type Config

type Config struct {
	// FileManagerKey is the access key for the S3 client.
	FileManagerKey string

	// FileManagerSecret is the secret key for the S3 client.
	FileManagerSecret string

	// CDNURL is the URL of the content delivery network (CDN) for the S3 client.
	CDNURL string

	// BasePath is the base path for the S3 client.
	BasePath string

	// Endpoint is the endpoint URL for the S3 client.
	Endpoint string

	// Region is the AWS region for the S3 client.
	Region string

	// Bucket is the name of the S3 bucket for the client.
	Bucket string

	// MaxFileSize is the maximum allowed file size for the S3 client.
	MaxFileSize int64
}

Config represents a storage client config

type FileManager

type FileManager struct {
	// contains filtered or unexported fields
}

FileManager represents a file manager that interacts with an S3 bucket.

func New

func New(cnf Config) (*FileManager, error)

New creates a new instance of FileManager with the provided configuration. It initializes a storage session using the AWS SDK and returns a FileManager object. The FileManager object is used to interact with the specified S3 bucket.

func NewWithOptions

func NewWithOptions(opt ...FileManagerOption) (*FileManager, error)

NewWithS3Client creates a new instance of FileManager with the provided S3 client. It returns a FileManager object that is used to interact with the specified S3 bucket.

func (*FileManager) Remove

func (fm *FileManager) Remove(ctx context.Context, fileURL string) error

Remove removes a file from the storage. It takes the fileURL as a parameter and returns an error if any. The fileURL is the URL of the file to be removed.

func (*FileManager) RemoveFilesFromDirectory

func (fm *FileManager) RemoveFilesFromDirectory(ctx context.Context, dir string) error

RemoveFilesFromDirectory removes all files from the specified directory in the storage. It retrieves all files from the storage, and then removes each file individually in parallel. If the directory does not exist or there are no files in the directory, it returns nil. If any error occurs during the removal process, it returns an error indicating the failure.

func (*FileManager) Upload

func (fm *FileManager) Upload(ctx context.Context, file io.ReadSeeker, filename, contentType string) (string, error)

Upload uploads a file to the S3 bucket. It takes the file content as a byte slice, the filename, and the content type as input parameters. It returns the URL of the uploaded file and any error encountered during the upload process.

func (*FileManager) UploadFromMultipartForm

func (fm *FileManager) UploadFromMultipartForm(r *http.Request, fieldName string) (string, error)

UploadFromMultipartForm uploads a file from a multipart form to the S3 bucket. It parses the multipart form, retrieves the file from the form data, and then uploads it to the S3 bucket. The file size is limited to 64MB.

Parameters: - r: The HTTP request containing the multipart form data. - fieldName: The name of the field in the multipart form that contains the file.

Returns: - string: The URL of the uploaded file in the S3 bucket. - error: An error if any occurred during the upload process.

func (*FileManager) UploadFromURL

func (fm *FileManager) UploadFromURL(ctx context.Context, fileURL string) (string, error)

UploadFromURL uploads a file from a URL to the S3 bucket. It takes the URL of the file as input and returns the URL of the uploaded file and any error encountered during the upload process.

type FileManagerOption

type FileManagerOption func(*FileManager) error

FileManagerOption represents a file manager option function.

func WithBasePath

func WithBasePath(basePath string) FileManagerOption

WithBasePath sets the base path.

func WithBucketName

func WithBucketName(bucketName string) FileManagerOption

WithBucketName sets the bucket name.

func WithCDNURL

func WithCDNURL(cdnURL string) FileManagerOption

WithCDNURL sets the CDN URL.

func WithCustomHTTPClient

func WithCustomHTTPClient(client httpClient) FileManagerOption

WithCustomHTTPClient sets the custom HTTP client.

func WithMaxFileSize

func WithMaxFileSize(maxFileSize int64) FileManagerOption

WithMaxFileSize sets the max file size.

func WithS3Client

func WithS3Client(client S3Client) FileManagerOption

WithS3Client sets the S3 client.

type S3Client

type S3Client interface {
	PutObjectWithContext(ctx aws.Context, input *s3.PutObjectInput, opts ...request.Option) (*s3.PutObjectOutput, error)
	ListObjectsV2WithContext(ctx aws.Context, input *s3.ListObjectsV2Input, opts ...request.Option) (*s3.ListObjectsV2Output, error)
	HeadObjectWithContext(ctx aws.Context, input *s3.HeadObjectInput, opts ...request.Option) (*s3.HeadObjectOutput, error)
	DeleteObjectWithContext(ctx aws.Context, input *s3.DeleteObjectInput, opts ...request.Option) (*s3.DeleteObjectOutput, error)
}

S3 client interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL