Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add delegate upload example #245

Merged
merged 1 commit into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions examples/delegate_upload_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"time"

"github.com/bnb-chain/greenfield-go-sdk/client"
"github.com/bnb-chain/greenfield-go-sdk/types"
)

// The example demonstrates the use of the Delegate upload functionality, which streamlines the process compared to the
// traditional two-stage method of creating and then uploading. With Delegated upload, the uploader interacts solely with
// the SP without the need to calculate the checksum and create the object meta on-chain initially.
// Instead, the primary SP handles these on behalf of the uploader.
func main() {
account, err := types.NewAccountFromPrivateKey("test", privateKey)
if err != nil {
log.Fatalf("New account from private key error, %v", err)
}
cli, err := client.New(chainId, rpcAddr, client.Option{DefaultAccount: account})
if err != nil {
log.Fatalf("unable to new greenfield client, %v", err)
}
ctx := context.Background()

// get storage providers list
spLists, err := cli.ListStorageProviders(ctx, true)
if err != nil {
log.Fatalf("fail to list in service sps")
}
// choose the first sp to be the primary SP
primarySP := spLists[0].GetOperatorAddress()

// create bucket
_, err = cli.CreateBucket(ctx, bucketName, primarySP, types.CreateBucketOptions{})
handleErr(err, "CreateBucket")
log.Printf("create bucket %s on SP: %s successfully \n", bucketName, spLists[0].Endpoint)

// head bucket
bucketInfo, err := cli.HeadBucket(ctx, bucketName)
handleErr(err, "HeadBucket")
log.Println("bucket info:", bucketInfo.String())

// Create object content
var buffer bytes.Buffer
line := `0123456789`
for i := 0; i < 1024; i++ {
buffer.WriteString(fmt.Sprintf("%s", line))
}

err = cli.DelegatePutObject(ctx, bucketName, objectName, int64(buffer.Len()), bytes.NewReader(buffer.Bytes()), types.PutObjectOptions{})
handleErr(err, "DelegatePutObject")

log.Printf("object: %s has been uploaded to SP\n", objectName)

waitObjectSeal(cli, bucketName, objectName)
// wait for block_syncer to sync up data from chain
time.Sleep(time.Second * 5)

// get object
reader, info, err := cli.GetObject(ctx, bucketName, objectName, types.GetObjectOptions{})
handleErr(err, "GetObject")
log.Printf("get object %s successfully, size %d \n", info.ObjectName, info.Size)
handleErr(err, "GetObject")
objectBytes, err := io.ReadAll(reader)
if !bytes.Equal(objectBytes, buffer.Bytes()) {
handleErr(errors.New("download content not same"), "GetObject")
}

// the updated content of object
var newBuffer bytes.Buffer
for i := 0; i < 2048; i++ {
newBuffer.WriteString(fmt.Sprintf("%s", line))
}

err = cli.DelegateUpdateObjectContent(ctx, bucketName, objectName, int64(buffer.Len()), bytes.NewReader(newBuffer.Bytes()), types.PutObjectOptions{})
handleErr(err, "DelegateUpdateObjectContent")

// get object
reader, info, err = cli.GetObject(ctx, bucketName, objectName, types.GetObjectOptions{})
handleErr(err, "GetObject")
log.Printf("get object %s successfully, size %d \n", info.ObjectName, info.Size)
handleErr(err, "GetObject")
objectBytes, err = io.ReadAll(reader)
if !bytes.Equal(objectBytes, newBuffer.Bytes()) {
handleErr(errors.New("download content not same"), "GetObject")
}

}
Loading