Learn how to use the AWS SDK for Go (Golang) with Filebase.
What is AWS SDK - Go?
AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - Go.
Read below to learn how to use the AWS SDK for Go with Filebase.
Prerequisites:
This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.
Create a Bucket
The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:
filebase-access-key: Your Filebase Access Key
filebase-secret-key: Your Filebase Secret Key
new-filebase-bucket: The Intended New Bucket Name
Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.
Buckets created through this method will be automatically created on the IPFS network.
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
//// create a configuration
s3Config := aws.Config{
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
Endpoint: aws.String("https://s3.filebase.com"),
Region: aws.String("us-east-1"),
S3ForcePathStyle: aws.Bool(true),
}
goSession, err := session.NewSessionWithOptions(session.Options{
Config: s3Config,
Profile: "filebase",
})
// check if the session was created correctly.
if err != nil {
fmt.Println(err)
}
// create a s3 client session
s3Client := s3.New(goSession)
// set parameter for bucket name
bucket := aws.String("new-filebase-bucket")
// create a bucket
_, err = s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: bucket,
})
// print if there is an error
if err != nil {
fmt.Println(err.Error())
return
}
}
Upload an Object
The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:
filebase-access-key: Your Filebase Access Key
filebase-secret-key: Your Filebase Secret Key
bucket-name: Your Filebase Bucket Name
/path/to/object/to/upload: The path to the file to upload
object-name: The name of the object to upload
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"os"
)
func main() {
//// create a configuration
s3Config := aws.Config{
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
Endpoint: aws.String("https://s3.filebase.com"),
Region: aws.String("us-east-1"),
S3ForcePathStyle: aws.Bool(true),
}
// create a new session using the config above and profile
goSession, err := session.NewSessionWithOptions(session.Options{
Config: s3Config,
Profile: "filebase",
})
// check if the session was created correctly.
if err != nil {
fmt.Println(err)
}
// create a s3 client session
s3Client := s3.New(goSession)
//set the file path to upload
file, err := os.Open("/path/to/object/to/upload")
if err != nil {
fmt.Println(err.Error())
return
}
defer file.Close()
// create put object input
putObjectInput := &s3.PutObjectInput{
Body: file,
Bucket: aws.String("bucket-name"),
Key: aws.String("object-name"),
}
// upload file
_, err = s3Client.PutObject(putObjectInput)
// print if there is an error
if err != nil {
fmt.Println(err.Error())
return
}
}
Download an Object
The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:
filebase-access-key: Your Filebase Access Key
filebase-secret-key: Your Filebase Secret Key
bucket-name: Your Filebase Bucket Name
object-name: The name of the object to download
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
//// create a configuration
s3Config := aws.Config{
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
Endpoint: aws.String("https://s3.filebase.com"),
Region: aws.String("us-east-1"),
S3ForcePathStyle: aws.Bool(true),
}
// create a new session using the config above and profile
goSession, err := session.NewSessionWithOptions(session.Options{
Config: s3Config,
Profile: "filebase",
})
// check if the session was created correctly.
if err != nil {
fmt.Println(err)
}
// create a s3 client session
s3Client := s3.New(goSession)
// create put object input
getObjectInput := &s3.GetObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String("object-name"),
}
// get file
_, err = s3Client.GetObject(getObjectInput)
// print if there is an error
if err != nil
fmt.Println(err.Error())
return
}
}
Delete an Object
The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:
filebase-access-key: Your Filebase Access Key
filebase-secret-key: Your Filebase Secret Key
bucket-name: Your Filebase Bucket Name
object-name: The name of the object to be deleted
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
//// create a configuration
s3Config := aws.Config{
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
Endpoint: aws.String("https://s3.filebase.com"),
Region: aws.String("us-east-1"),
S3ForcePathStyle: aws.Bool(true),
}
// create a new session using the config above and profile
goSession, err := session.NewSessionWithOptions(session.Options{
Config: s3Config,
Profile: "filebase",
})
// check if the session was created correctly.
if err != nil {
fmt.Println(err)
}
// create a s3 client session
s3Client := s3.New(goSession)
// create put object input
deleteObjectInput := &s3.DeleteObjectInput{
Bucket: aws.String("bucket-name"),
Key: aws.String("object-name"),
}
// get file
_, err = s3Client.DeleteObject(deleteObjectInput)
// print if there is an error
if err != nil {
fmt.Println(err.Error())
return
}
}
For more information on the AWS SDK for Go, check out the documentation here.