Package upload provides an easy and portable way to interact with CDN, buckets or blobs within any cloud/local storage location. And provides methods to read or write or upload or delete files to blob storage on GCP, AWS, Azure, in-memory, local and more.
It wraps gocloud.dev/blob
(https://github.com/google/go-cloud/tree/master/blob) for further simplicity.
You can store the files in using file system blob and can serve them using http file server. Upload library provides
everything you need to do that in-built. Just use pfsblob
as your file system handler. See the example
in pfsblob/tests.
- URL Scheme:
pfs
- URL Host: Your domain name (without protocol) e.g.
localhost:8080
orexample.com
- URL Path: Your storage directory in which you want to manage your files e.g.
/tmp/files
- Domain Protocol: If your domain uses secure
https
protocol, set a query parameter:?secure=true
- Route: By default, files will be served at the root of the domain, if you want to change it, use query
parameter:
?route=static
Complete Query will look something like these:
-
pfs://localhost:8080/tmp/files?secure=false
This will serve files athttp://localhost:8080/...
e.g. the link for the file/tmp/files/image.png
will behttp://localhost:8080/image.png
-
pfs://example.com/tmp/files?secure=true&route=public
This will serve files athttps://example.com/public/...
e.g. the link for the file/tmp/files/image.png
will behttps://example.com/public/image.png
package main
import (
"context"
"fmt"
"github.com/Shivam010/upload"
)
const content = `Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries.`
func main() {
ctx := context.TODO()
s3Url := "s3://name?region=us-east-2"
// Open or create bucket using name
buck, err := upload.Open(s3Url) // or buck := upload.NewBucket(s3Url)
if err != nil {
panic(err) // handle error
}
fmt.Println("Provider: ", buck.Provider())
// Upload a new file and get its link
link, err := buck.WriteAll(ctx, "loren.ipsum", []byte(content))
if err != nil {
panic(err) // handle error
}
fmt.Println("Link to the file (loren.ipsum)", link)
// Name of the uploaded file from its link
name := buck.GetName(link)
// read content of the uploaded file
cont, err := buck.ReadAll(ctx, name)
if err != nil {
panic(err) // handle error
}
fmt.Println("Content of the file (loren.ipsum)", cont)
// delete the uploaded file
if err = buck.Delete(ctx, name); err != nil {
panic(err) // handle error
}
// Close the bucket
if err = buck.Close(); err != nil {
panic(err) // handle error
}
}
This project is licensed under the MIT License