Go (aws-sdk-go-v2)
Fil One works with aws-sdk-go-v2 — it is S3-API compatible. Configure the client to point at the Fil One endpoint with UsePathStyle: true and use the standard S3 API. A few S3 operations are not supported; see S3 Compatibility for the exceptions.
Examples here use eu-west-1, the default region. For a bucket in us-east-1, use https://s3.us-east-1.filonecontent.com and Region: "us-east-1". The endpoint, the region, and your access key must all refer to the same region.
Installation
In a new directory, create a module and add the SDK:
go mod init example.com/filone
go get github.com/aws/aws-sdk-go-v2/aws
go get github.com/aws/aws-sdk-go-v2/credentials
go get github.com/aws/aws-sdk-go-v2/service/s3
Client configuration
package main
import (
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func newClient() *s3.Client {
return s3.New(s3.Options{
BaseEndpoint: aws.String("https://s3.eu-west-1.filonecontent.com"),
Region: "eu-west-1",
Credentials: credentials.NewStaticCredentialsProvider(
os.Getenv("AWS_ACCESS_KEY_ID"),
os.Getenv("AWS_SECRET_ACCESS_KEY"),
"",
),
UsePathStyle: true,
})
}
Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables, the names the console's Download .env writes. Never hardcode credentials in source code.
The operation snippets below are fragments: each assumes the client returned by newClient() above, a ctx (e.g. ctx := context.TODO()), and "log" imported, and shows only the other imports specific to that snippet.
Core operations
Create a bucket
CreateBucket is region-dependent. It is not available in eu-west-1: create the bucket in the Fil One console there, with New bucket. In us-east-1, use a client built with the us-east-1 endpoint and region, and a us-east-1 key that has the CreateBucket permission:
// usClient: like newClient(), with BaseEndpoint https://s3.us-east-1.filonecontent.com and Region "us-east-1"
_, err := usClient.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: aws.String("my-bucket"),
})
if err != nil {
log.Fatal(err)
}
Versioning and Object Lock can only be set when the bucket is created, and only in the console. See Buckets.
Upload an object
import (
"bytes"
"os"
)
// Upload bytes
_, err := client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("data/file.json"),
Body: bytes.NewReader([]byte(`{"key":"value"}`)),
ContentType: aws.String("application/json"),
})
if err != nil {
log.Fatal(err)
}
// Upload a local file (report.pdf must exist)
f, err := os.Open("report.pdf")
if err != nil {
log.Fatal(err)
}
defer f.Close()
_, err = client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("reports/report.pdf"),
Body: f,
ContentType: aws.String("application/pdf"),
})
if err != nil {
log.Fatal(err)
}
Download an object
import (
"io"
"os"
)
result, err := client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("reports/report.pdf"),
})
if err != nil {
log.Fatal(err)
}
defer result.Body.Close()
out, err := os.Create("local-report.pdf")
if err != nil {
log.Fatal(err)
}
defer out.Close()
if _, err := io.Copy(out, result.Body); err != nil {
log.Fatal(err)
}
List objects
import "fmt"
paginator := s3.NewListObjectsV2Paginator(client, &s3.ListObjectsV2Input{
Bucket: aws.String("my-bucket"),
})
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
log.Fatal(err)
}
for _, obj := range page.Contents {
// obj.Size is a *int64; dereference it with aws.ToInt64 to print the number
fmt.Println(*obj.Key, aws.ToInt64(obj.Size))
}
}
Delete an object
_, err := client.DeleteObject(ctx, &s3.DeleteObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("reports/report.pdf"),
})
if err != nil {
log.Fatal(err)
}
Presigned URLs
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
presigner := s3.NewPresignClient(client)
// Download URL valid for 1 hour
downloadReq, err := presigner.PresignGetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("reports/report.pdf"),
}, s3.WithPresignExpires(time.Hour))
if err != nil {
log.Fatal(err)
}
fmt.Println(downloadReq.URL)
// Upload URL valid for 15 minutes
uploadReq, err := presigner.PresignPutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("uploads/new-file.txt"),
}, s3.WithPresignExpires(15*time.Minute))
if err != nil {
log.Fatal(err)
}
fmt.Println(uploadReq.URL)
Error handling
import (
"errors"
"fmt"
"github.com/aws/smithy-go"
)
_, err := client.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String("my-bucket"),
Key: aws.String("missing.txt"),
})
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
fmt.Println(apiErr.ErrorCode(), apiErr.ErrorMessage())
}
}
See the Error Reference for a full list of error codes.