AWS SDK for Go

Learn how to pin files and folders to IPFS using the AWS SDK for Golang.

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.

What is a .car file?

A .car file is a type of compressed archive file that contains multiple files, similar to a ZIP file. .car files are used by the FileCoin and IPFS networks, which utilize the metadata fields to include the IPFS CIDs of each file within the archive.

Read below to learn how to pin files and folders to IPFS using the AWS SDK for Golang.

Prerequisites:

Pinning a Single File to IPFS

1. Click on the ‘Buckets’ option from the menu to open the Buckets dashboard.

2. Once at the Buckets dashboard, create a new bucket by clicking the ‘Create Bucket’ option in the top right corner.

3. Enter a bucket name and choose your storage network.

4. Create a new file with the following AWS SDK for Golang code:

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 to IPFS

  • 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
}
}

Pinning a Folder to IPFS using the AWS SDK for Golang

1. Download the ipfs-car package with the following command:

git clone https://github.com/web3-storage/ipfs-car

2. Install the ipfs-car package:

npx ipfs-car

3. Next, use ipfs-car to package a folder of files into a .car file.

This has been tested with .car archives containing 10,000 or more files. Use any of the following commands, depending on your desired workflow:

Write a .car file to the current working directory

ipfs-car --pack path/to/file/or/dir

Write a .car file to a specific directory with a specified name

ipfs-car --pack path/to/files --output path/to/write/ipfs-car.car

4. Create a new file with the following AWS SDK for Golang code:

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 to IPFS

  • Object-name: The name of the .car file 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("ipfs-car.car"),
}

// upload file
_, err = s3Client.PutObject(putObjectInput)

// print if there is an error
if err != nil {
	fmt.Println(err.Error())
return
}
}

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated