AWS SDK - Go (Golang)

Please read the following to understand how to use the AWS SDK - Go with the 4EVERLAND Bucket. Combine the AWS SDK API for the Go language with the 4EVERLAND Bucket to achieve file uploads to the IPFS or Arweave storage networks.

Preparation

Development Examples

Install the S3 client

The following code example defines an S3 client and does not return any output. Replace the values in the code below to complete your configuration:

  1. Endpoint:https://endpoint.4everland.co

  2. 4EVERLAND-Bucket-APIKey: Bucket-Access Keys-API key

  3. 4EVERLAND-Bucket-APISecret: Bucket-Access Keys-API Secret

  4. Region: Default filled in with "4everland"

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() {
        s3Config := aws.Config{
        Credentials:      credentials.NewStaticCredentials("4EVERLAND-Bucket-APIKey", "4EVERLAND-Bucket-APISecret", ""),
        Endpoint:         aws.String("https://endpoint.4everland.co"),
        Region:           aws.String("4everland"),
}

Create a bucket

bucket:The desired name for creating the bucket

goSession, err := session.NewSessionWithOptions(session.Options{
        Config:  s3Config,
        Profile: "4everland",
})
 
// 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("bucketname")
 
// 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 a file

bucket: Your bucket name

/path/to/4everland/4ever.png: The file path for upload

Key: The file name for upload

// create a new session using the config above and profile
goSession, err := session.NewSessionWithOptions(session.Options{
        Config:  s3Config,
        Profile: "4everland",
})
 
// 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/4everland/4ever.png")
if err != nil {
        fmt.Println(err.Error())
return
}
 
defer file.Close()
// create put object input
        putObjectInput := &s3.PutObjectInput{
        Body:   file,
        Bucket: aws.String("4everland-bucketname"),
        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
}
}

To retrieve the bucket and file list

listBucketOutPut, err := client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})

if err != nil {
    log.Fatalf("unable ListBuckets: , %v", err)
}
    //listObjectV2
listObjectsV2Output, err := client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
    Bucket: &bucket,
})

if err != nil {
    log.Fatalf("unable listObjectsV2: , %v", err)
}
fmt.Println(listObjectsV2Output)

Requesting IPFS CID and Arweave Hash

bucket:The bucket name where the target file is stored

objectKey: The path of the target file

var (
    bucket = "4everlandbucket"
    objectKey = "/path/to/4everland/4ever.png"
)
object, err := client.HeadObject(context.Background(), &s3.HeadObjectInput{
    Bucket: &bucket,
    Key:    &objectKey,
})
if err != nil {
    return
}
if err != nil {
    panic(err)
}
fmt.Println("ipfs cid:", object.Metadata["ipfs-hash"])
//If it is an Arweave type of bucket.
fmt.Println("arweave hash:", object.Metadata["arweave-hash"])

If you have any questions, please join our Discord server, or send us an email at contact@4everland.org.

Last updated