Filebase
Search…
AWS SDK - Go (Golang)
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.
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.

Prerequisites:

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 Storj network.
1
package main
2
3
import (
4
"fmt"
5
"github.com/aws/aws-sdk-go/aws"
6
"github.com/aws/aws-sdk-go/aws/session"
7
"github.com/aws/aws-sdk-go/aws/credentials"
8
"github.com/aws/aws-sdk-go/service/s3"
9
)
10
11
func main() {
12
//// create a configuration
13
s3Config := aws.Config{
14
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
15
Endpoint: aws.String("https://s3.filebase.com"),
16
Region: aws.String("us-east-1"),
17
S3ForcePathStyle: aws.Bool(true),
18
}
19
20
goSession, err := session.NewSessionWithOptions(session.Options{
21
Config: s3Config,
22
Profile: "filebase",
23
})
24
25
// check if the session was created correctly.
26
27
if err != nil {
28
fmt.Println(err)
29
}
30
31
// create a s3 client session
32
s3Client := s3.New(goSession)
33
34
// set parameter for bucket name
35
bucket := aws.String("new-filebase-bucket")
36
37
// create a bucket
38
_, err = s3Client.CreateBucket(&s3.CreateBucketInput{
39
Bucket: bucket,
40
})
41
42
// print if there is an error
43
if err != nil {
44
fmt.Println(err.Error())
45
return
46
}
47
}
Copied!

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
1
package main
2
3
import (
4
"fmt"
5
"github.com/aws/aws-sdk-go/aws"
6
"github.com/aws/aws-sdk-go/aws/session"
7
"github.com/aws/aws-sdk-go/aws/credentials"
8
"github.com/aws/aws-sdk-go/service/s3"
9
"os"
10
)
11
12
func main() {
13
//// create a configuration
14
s3Config := aws.Config{
15
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
16
Endpoint: aws.String("https://s3.filebase.com"),
17
Region: aws.String("us-east-1"),
18
S3ForcePathStyle: aws.Bool(true),
19
}
20
21
// create a new session using the config above and profile
22
goSession, err := session.NewSessionWithOptions(session.Options{
23
Config: s3Config,
24
Profile: "filebase",
25
})
26
27
// check if the session was created correctly.
28
29
if err != nil {
30
fmt.Println(err)
31
}
32
33
// create a s3 client session
34
s3Client := s3.New(goSession)
35
36
//set the file path to upload
37
file, err := os.Open("/path/to/object/to/upload")
38
if err != nil {
39
fmt.Println(err.Error())
40
return
41
}
42
43
defer file.Close()
44
// create put object input
45
putObjectInput := &s3.PutObjectInput{
46
Body: file,
47
Bucket: aws.String("bucket-name"),
48
Key: aws.String("object-name"),
49
}
50
51
// upload file
52
_, err = s3Client.PutObject(putObjectInput)
53
54
// print if there is an error
55
if err != nil {
56
fmt.Println(err.Error())
57
return
58
}
59
}
Copied!

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
1
package main
2
3
import (
4
"fmt"
5
"github.com/aws/aws-sdk-go/aws"
6
"github.com/aws/aws-sdk-go/aws/session"
7
"github.com/aws/aws-sdk-go/aws/credentials"
8
"github.com/aws/aws-sdk-go/service/s3"
9
)
10
11
func main() {
12
//// create a configuration
13
s3Config := aws.Config{
14
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
15
Endpoint: aws.String("https://s3.filebase.com"),
16
Region: aws.String("us-east-1"),
17
S3ForcePathStyle: aws.Bool(true),
18
}
19
20
// create a new session using the config above and profile
21
goSession, err := session.NewSessionWithOptions(session.Options{
22
Config: s3Config,
23
Profile: "filebase",
24
})
25
26
// check if the session was created correctly.
27
if err != nil {
28
fmt.Println(err)
29
}
30
31
// create a s3 client session
32
s3Client := s3.New(goSession)
33
34
// create put object input
35
getObjectInput := &s3.GetObjectInput{
36
Bucket: aws.String("bucket-name"),
37
Key: aws.String("object-name"),
38
}
39
40
// get file
41
_, err = s3Client.GetObject(getObjectInput)
42
43
// print if there is an error
44
if err != nil
45
fmt.Println(err.Error())
46
return
47
}
48
}
Copied!

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
1
package main
2
3
import (
4
"fmt"
5
"github.com/aws/aws-sdk-go/aws"
6
"github.com/aws/aws-sdk-go/aws/session"
7
"github.com/aws/aws-sdk-go/aws/credentials"
8
"github.com/aws/aws-sdk-go/service/s3"
9
)
10
11
func main() {
12
//// create a configuration
13
s3Config := aws.Config{
14
Credentials: credentials.NewStaticCredentials("filebase-access-key", "filebase-secret-key", ""),
15
Endpoint: aws.String("https://s3.filebase.com"),
16
Region: aws.String("us-east-1"),
17
S3ForcePathStyle: aws.Bool(true),
18
}
19
20
// create a new session using the config above and profile
21
goSession, err := session.NewSessionWithOptions(session.Options{
22
Config: s3Config,
23
Profile: "filebase",
24
})
25
26
// check if the session was created correctly.
27
if err != nil {
28
fmt.Println(err)
29
}
30
31
// create a s3 client session
32
s3Client := s3.New(goSession)
33
34
// create put object input
35
deleteObjectInput := &s3.DeleteObjectInput{
36
Bucket: aws.String("bucket-name"),
37
Key: aws.String("object-name"),
38
}
39
40
// get file
41
_, err = s3Client.DeleteObject(deleteObjectInput)
42
43
// print if there is an error
44
if err != nil {
45
fmt.Println(err.Error())
46
return
47
}
48
}
Copied!
For more information on the AWS SDK for Go, check out the documentation here.
If you have any questions, please send us an email at [email protected].
Last modified 1mo ago