Documentation ¶
Index ¶
- Constants
- Variables
- type Config
- type FileManager
- func (fm *FileManager) Remove(ctx context.Context, fileURL string) error
- func (fm *FileManager) RemoveFilesFromDirectory(ctx context.Context, dir string) error
- func (fm *FileManager) Upload(ctx context.Context, file io.ReadSeeker, filename, contentType string) (string, error)
- func (fm *FileManager) UploadFromMultipartForm(r *http.Request, fieldName string) (string, error)
- func (fm *FileManager) UploadFromURL(ctx context.Context, fileURL string) (string, error)
- type FileManagerOption
- func WithBasePath(basePath string) FileManagerOption
- func WithBucketName(bucketName string) FileManagerOption
- func WithCDNURL(cdnURL string) FileManagerOption
- func WithCustomHTTPClient(client httpClient) FileManagerOption
- func WithMaxFileSize(maxFileSize int64) FileManagerOption
- func WithS3Client(client S3Client) FileManagerOption
- type S3Client
Constants ¶
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 ¶
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 ¶
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 ¶
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 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