Documentation ¶
Index ¶
- Constants
- Variables
- func HexEscape(s string, shouldEscape func(s []rune, i int) bool) string
- func HexUnescape(s string) string
- func NewDefaultV2Config(ctx context.Context) (awsv2.Config, error)
- func OpenBucketV2(ctx context.Context, client *s3v2.Client, bucketName string, opts *Options) (*blob.Bucket, error)
- func URLEscape(s string) string
- func URLUnescape(s string) string
- func UseV2(q url.Values) bool
- func V2ConfigFromURLParams(ctx context.Context, q url.Values) (awsv2.Config, error)
- type BytesReader
- type File
- type FileReader
- type MultipartReader
- type Options
- type PathReader
- type System
- func (s *System) Attributes(fileKey string) (*blob.Attributes, error)
- func (s *System) Close() error
- func (s *System) Copy(srcKey, dstKey string) error
- func (s *System) CreateThumb(originalKey string, thumbKey, thumbSize string) error
- func (s *System) Delete(fileKey string) error
- func (s *System) DeletePrefix(prefix string) []error
- func (s *System) Exists(fileKey string) (bool, error)
- func (s *System) GetFile(fileKey string) (*blob.Reader, error)
- func (s *System) List(prefix string) ([]*blob.ListObject, error)
- func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error
- func (s *System) SetContext(ctx context.Context)
- func (s *System) Upload(content []byte, fileKey string) error
- func (s *System) UploadFile(file *File, fileKey string) error
- func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
- type URLOpener
Constants ¶
const Scheme = "s3"
Scheme is the URL scheme s3blob registers its URLOpener under on blob.DefaultMux.
Variables ¶
var ThumbSizeRegex = regexp.MustCompile(`^(\d+)x(\d+)(t|b|f)?$`)
Functions ¶
func HexEscape ¶
HexEscape returns s, with all runes for which shouldEscape returns true escaped to "__0xXXX__", where XXX is the hex representation of the rune value. For example, " " would escape to "__0x20__".
Non-UTF-8 strings will have their non-UTF-8 characters escaped to unicode.ReplacementChar; the original value is lost. Please file an issue if you need non-UTF8 support.
Note: shouldEscape takes the whole string as a slice of runes and an index. Passing it a single byte or a single rune doesn't provide enough context for some escape decisions; for example, the caller might want to escape the second "/" in "//" but not the first one. We pass a slice of runes instead of the string or a slice of bytes because some decisions will be made on a rune basis (e.g., encode all non-ASCII runes).
func NewDefaultV2Config ¶
NewDefaultV2Config returns a aws.Config for AWS SDK v2, using the default options.
func OpenBucketV2 ¶
func OpenBucketV2(ctx context.Context, client *s3v2.Client, bucketName string, opts *Options) (*blob.Bucket, error)
OpenBucketV2 returns a *blob.Bucket backed by S3, using AWS SDK v2.
func URLUnescape ¶
URLUnescape reverses URLEscape using url.PathUnescape. If the unescape returns an error, it returns s.
func UseV2 ¶
UseV2 returns true iff the URL parameters indicate that the provider should use the AWS SDK v2.
"awssdk=v1" will force V1. "awssdk=v2" will force V2. No "awssdk" parameter (or any other value) will return the default, currently V1. Note that the default may change in the future.
func V2ConfigFromURLParams ¶
V2ConfigFromURLParams returns an aws.Config for AWS SDK v2 initialized based on the URL parameters in q. It is intended to be used by URLOpeners for AWS services if UseV2 returns true.
https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws#Config
It returns an error if q contains any unknown query parameters; callers should remove any query parameters they know about from q before calling V2ConfigFromURLParams.
The following query options are supported:
- region: The AWS region for requests; sets WithRegion.
- profile: The shared config profile to use; sets SharedConfigProfile.
- endpoint: The AWS service endpoint to send HTTP request.
Types ¶
type BytesReader ¶
type BytesReader struct {
Bytes []byte
}
BytesReader defines a FileReader from bytes content.
func (*BytesReader) Open ¶
func (r *BytesReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type File ¶
type File struct { Reader FileReader Name string OriginalName string Size int64 }
File defines a single file io.ReadSeekCloser resource.
The file could be from a local path, multipart/form-data header, etc.
func NewFileFromBytes ¶
NewFileFromBytes creates a new File instance from the provided byte slice.
func NewFileFromMultipart ¶
func NewFileFromMultipart(mh *multipart.FileHeader) (*File, error)
NewFileFromMultipart creates a new File from the provided multipart header.
func NewFileFromPath ¶
NewFileFromPath creates a new File instance from the provided local file path.
func NewFileFromUrl ¶
NewFileFromUrl creates a new File from the provided url by downloading the resource and load it as BytesReader.
Example
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() file, err := filesystem.NewFileFromUrl(ctx, "https://example.com/image.png")
type FileReader ¶
type FileReader interface {
Open() (io.ReadSeekCloser, error)
}
FileReader defines an interface for a file resource reader.
type MultipartReader ¶
type MultipartReader struct {
Header *multipart.FileHeader
}
MultipartReader defines a FileReader from multipart.FileHeader.
func (*MultipartReader) Open ¶
func (r *MultipartReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type Options ¶
type Options struct { // UseLegacyList forces the use of ListObjects instead of ListObjectsV2. // Some S3-compatible services (like CEPH) do not currently support // ListObjectsV2. UseLegacyList bool }
Options sets options for constructing a *blob.Bucket backed by fileblob.
type PathReader ¶
type PathReader struct {
Path string
}
PathReader defines a FileReader from a local file path.
func (*PathReader) Open ¶
func (r *PathReader) Open() (io.ReadSeekCloser, error)
Open implements the filesystem.FileReader interface.
type System ¶
type System struct {
// contains filtered or unexported fields
}
func NewLocal ¶
NewLocal initializes a new local filesystem instance.
NB! Make sure to call `Close()` after you are done working with it.
func NewS3 ¶
func NewS3( bucketName string, region string, endpoint string, accessKey string, secretKey string, s3ForcePathStyle bool, ) (*System, error)
NewS3 initializes an S3 filesystem instance.
NB! Make sure to call `Close()` after you are done working with it.
func (*System) Attributes ¶
func (s *System) Attributes(fileKey string) (*blob.Attributes, error)
Attributes returns the attributes for the file with fileKey path.
func (*System) Copy ¶
Copy copies the file stored at srcKey to dstKey.
If dstKey file already exists, it is overwritten.
func (*System) CreateThumb ¶
CreateThumb creates a new thumb image for the file at originalKey location. The new thumb file is stored at thumbKey location.
thumbSize is in the format: - 0xH (eg. 0x100) - resize to H height preserving the aspect ratio - Wx0 (eg. 300x0) - resize to W width preserving the aspect ratio - WxH (eg. 300x100) - resize and crop to WxH viewbox (from center) - WxHt (eg. 300x100t) - resize and crop to WxH viewbox (from top) - WxHb (eg. 300x100b) - resize and crop to WxH viewbox (from bottom) - WxHf (eg. 300x100f) - fit inside a WxH viewbox (without cropping)
func (*System) DeletePrefix ¶
DeletePrefix deletes everything starting with the specified prefix.
func (*System) GetFile ¶
GetFile returns a file content reader for the given fileKey.
NB! Make sure to call `Close()` after you are done working with it.
func (*System) List ¶
func (s *System) List(prefix string) ([]*blob.ListObject, error)
List returns a flat list with info for all files under the specified prefix.
func (*System) Serve ¶
func (s *System) Serve(res http.ResponseWriter, req *http.Request, fileKey string, name string) error
Serve serves the file at fileKey location to an HTTP response.
If the `download` query parameter is used the file will be always served for download no matter of its type (aka. with "Content-Disposition: attachment").
func (*System) SetContext ¶
SetContext assigns the specified context to the current filesystem.
func (*System) UploadFile ¶
UploadFile uploads the provided multipart file to the fileKey location.
func (*System) UploadMultipart ¶
func (s *System) UploadMultipart(fh *multipart.FileHeader, fileKey string) error
UploadMultipart uploads the provided multipart file to the fileKey location.
type URLOpener ¶
type URLOpener struct { // UseV2 indicates whether the AWS SDK V2 should be used. UseV2 bool // Options specifies the options to pass to OpenBucket. Options Options }
URLOpener opens S3 URLs like "s3://mybucket".
The URL host is used as the bucket name.
Use "awssdk=v1" to force using AWS SDK v1, "awssdk=v2" to force using AWS SDK v2, or anything else to accept the default.
For V1, see gocloud.dev/aws/ConfigFromURLParams for supported query parameters for overriding the aws.Session from the URL. For V2, see gocloud.dev/aws/V2ConfigFromURLParams.