> For the complete documentation index, see [llms.txt](https://docs.4everland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible/coding-examples/aws-sdk-go-golang.md).

# 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

* In your development environment, [download and install](https://go.dev/doc/install) Go.
* [Download and install ](https://github.com/aws/aws-sdk-go#getting-started)the AWS SDK for Go.
* [Register](https://www.4everland.org/) a free 4EVERLAND account.
* If you need to use Arweave storage, you need to create an Arweave bucket. Click to[ ](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible)[learn more](https://docs.4everland.org/storage/bucket/guides).
* To obtain the corresponding API key in the bucket, click to[ learn more](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible).

## 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"

```go
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

```go
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

```go
// 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

```go
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

```go
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"])
```

{% hint style="info" %}
If you have any questions, please join our [Discord server](https://discord.com/invite/Cun2VpsdjF), or send us an email at <contact@4everland.org>.
{% endhint %}
