Skip to content

Commit 631e6ab

Browse files
authored
Merge pull request #49 from filecoin-project/feat/support-r2-service
feat: support cloudflare r2 upload backend
2 parents 6f106b2 + bf7fad0 commit 631e6ab

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:buster as builder
1+
FROM golang:1.19.2-buster as builder
22

33
RUN apt-get update && apt-get install -y ca-certificates
44

@@ -15,7 +15,7 @@ RUN go mod download
1515

1616
COPY . .
1717

18-
RUN go build -o filecoin-chain-archiver ./cmd/filecoin-chain-archiver
18+
RUN make all
1919

2020
FROM debian:buster
2121

cmd/filecoin-chain-archiver/cmds/create.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ var cmdCreate = &cli.Command{
7272
Usage: "bucket host and port for upload",
7373
EnvVars: []string{"FCA_CREATE_BUCKET_ENDPOINT"},
7474
},
75+
&cli.StringFlag{
76+
Name: "retrieval-endpoint-prefix",
77+
Usage: "URL prefix where uploaded object can be retrieved from",
78+
EnvVars: []string{"FCA_CREATE_RETRIEVAL_ENDPOINT_PREFIX"},
79+
},
7580
&cli.StringFlag{
7681
Name: "access-key",
7782
Usage: "access key for upload",
@@ -138,6 +143,7 @@ var cmdCreate = &cli.Command{
138143
flagBucketAccessKey := cctx.String("access-key")
139144
flagBucketSecretKey := cctx.String("secret-key")
140145
flagNamePrefix := cctx.String("name-prefix")
146+
flagRetrievalEndpointPrefix := cctx.String("retrieval-endpoint-prefix")
141147
flagBucket := cctx.String("bucket")
142148
flagDiscard := cctx.Bool("discard")
143149
flagProgressUpdate := cctx.Duration("progress-update")
@@ -380,10 +386,10 @@ var cmdCreate = &cli.Command{
380386
return err
381387
}
382388

383-
latestLocation, err := url.QueryUnescape(info.Location)
389+
latestLocation, err := url.JoinPath(flagRetrievalEndpointPrefix, info.Key)
384390
if err != nil {
385-
logger.Errorw("failed to decode location url", "location", info.Location, "err", err)
386-
latestLocation = info.Location
391+
logger.Errorw("failed to join request path", "request_prefix", flagRetrievalEndpointPrefix, "key", info.Key)
392+
return fmt.Errorf("failed to join request path: %w", err)
387393
}
388394

389395
sha256sum := fmt.Sprintf("%x *%s.car\n", h.Sum(nil), name)
@@ -397,8 +403,7 @@ var cmdCreate = &cli.Command{
397403
}
398404

399405
info, err = minioClient.PutObject(ctx, flagBucket, fmt.Sprintf("%slatest", flagNamePrefix), strings.NewReader(latestLocation), -1, minio.PutObjectOptions{
400-
WebsiteRedirectLocation: latestLocation,
401-
ContentType: "text/plain",
406+
ContentType: "text/plain",
402407
})
403408
if err != nil {
404409
return fmt.Errorf("failed to write latest", "object", fmt.Sprintf("%slatest", flagNamePrefix), "err", err)

pkg/index/index.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package index
22

33
import (
44
"context"
5-
"fmt"
5+
"io"
6+
"strings"
67
"sync"
78
"time"
89

9-
"github.com/minio/minio-go/v7"
10-
1110
"github.com/ipfs/go-log/v2"
11+
"github.com/minio/minio-go/v7"
12+
"golang.org/x/xerrors"
1213
)
1314

1415
var (
@@ -23,7 +24,7 @@ type IndexS3Resolver struct {
2324
}
2425

2526
type s3ClientInterface interface {
26-
StatObject(ctx context.Context, bucketName, objectName string, opts minio.StatObjectOptions) (minio.ObjectInfo, error)
27+
GetObject(ctx context.Context, bucketName, objectName string, opts minio.GetObjectOptions) (*minio.Object, error)
2728
}
2829

2930
type Resolver interface {
@@ -38,16 +39,19 @@ func NewIndexS3Resolver(client s3ClientInterface, bucket string) *IndexS3Resolve
3839
}
3940

4041
func (i *IndexS3Resolver) Resolve(ctx context.Context, obj string) (string, error) {
41-
objInfo, err := i.client.StatObject(ctx, i.bucket, obj, minio.StatObjectOptions{})
42+
object, err := i.client.GetObject(ctx, i.bucket, obj, minio.GetObjectOptions{})
4243
if err != nil {
43-
return "", err
44+
return "", xerrors.Errorf("failed to resolve link: %w", err)
4445
}
4546

46-
if v, ok := objInfo.Metadata["X-Amz-Website-Redirect-Location"]; ok {
47-
return v[0], nil
47+
data, err := io.ReadAll(object)
48+
if err != nil {
49+
return "", xerrors.Errorf("failed to resolve link: %w", err)
4850
}
4951

50-
return "", fmt.Errorf("failed to resolve link")
52+
logger.Infow("resolved", "link", string(data))
53+
54+
return strings.TrimSpace(string(data)), nil
5155
}
5256

5357
type cacheMetadata struct {

0 commit comments

Comments
 (0)