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

improve: s3 store custom_domain #3717

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/apidocs.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,8 @@ definitions:
type: string
bucket:
type: string
customDomain:
type: string
title: 'Reference: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-go/'
apiHttpBody:
type: object
Expand Down
1 change: 1 addition & 0 deletions proto/api/v1/workspace_setting_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ message WorkspaceStorageSetting {
string endpoint = 3;
string region = 4;
string bucket = 5;
string custom_domain = 6;
}
// The S3 config.
S3Config s3_config = 4;
Expand Down
154 changes: 82 additions & 72 deletions proto/gen/api/v1/workspace_setting_service.pb.go

Large diffs are not rendered by default.

96 changes: 53 additions & 43 deletions proto/gen/store/workspace_setting.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/store/workspace_setting.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ message StorageS3Config {
string endpoint = 3;
string region = 4;
string bucket = 5;
string custom_domain = 6;
}

message WorkspaceMemoRelatedSetting {
Expand Down
14 changes: 10 additions & 4 deletions server/router/api/v1/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,18 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc
if err != nil {
return errors.Wrap(err, "Failed to upload via s3 client")
}
presignURL, err := s3Client.PresignGetObject(ctx, key)
if err != nil {
return errors.Wrap(err, "Failed to presign via s3 client")

var referenceURL string
if s3Config.CustomDomain != "" {
referenceURL = fmt.Sprintf("%s/%s", s3Config.CustomDomain, key)
} else {
referenceURL, err = s3Client.PresignGetObject(ctx, key)
if err != nil {
return errors.Wrap(err, "Failed to presign via s3 client")
}
}

create.Reference = presignURL
create.Reference = referenceURL
create.Blob = nil
create.StorageType = storepb.ResourceStorageType_S3
create.Payload = &storepb.ResourcePayload{
Expand Down
2 changes: 2 additions & 0 deletions server/router/api/v1/workspace_setting_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func convertWorkspaceStorageSettingFromStore(settingpb *storepb.WorkspaceStorage
Endpoint: settingpb.S3Config.Endpoint,
Region: settingpb.S3Config.Region,
Bucket: settingpb.S3Config.Bucket,
CustomDomain: settingpb.S3Config.CustomDomain,
}
}
return setting
Expand All @@ -204,6 +205,7 @@ func convertWorkspaceStorageSettingToStore(setting *v1pb.WorkspaceStorageSetting
Endpoint: setting.S3Config.Endpoint,
Region: setting.S3Config.Region,
Bucket: setting.S3Config.Bucket,
CustomDomain: setting.S3Config.CustomDomain,
}
}
return settingpb
Expand Down
15 changes: 11 additions & 4 deletions server/service/s3_object_presigner/s3_object_presigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package s3objectpresigner

import (
"context"
"fmt"
"log/slog"
"time"

Expand Down Expand Up @@ -67,15 +68,21 @@ func (p *S3ObjectPresigner) CheckAndPresign(ctx context.Context) {
continue
}

presignURL, err := s3Client.PresignGetObject(ctx, s3ObjectPayload.Key)
if err != nil {
return
var referenceURL string
Copy link
Member

@boojack boojack Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we skip this? This is the scheduler that refreshes the presigned time. For resources that use custom domain, we don't need to refresh its URL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this because when I set/unset custom domains, this scheduler will automatically update and switch to using the custom domain URL/presigned URL after the previous signature expires.

if s3Config.CustomDomain != "" {
referenceURL = fmt.Sprintf("%s/%s", s3Config.CustomDomain, s3ObjectPayload.Key)
} else {
referenceURL, err = s3Client.PresignGetObject(ctx, s3ObjectPayload.Key)
if err != nil {
return
}
}

s3ObjectPayload.S3Config = s3Config
s3ObjectPayload.LastPresignedTime = timestamppb.New(time.Now())
if err := p.Store.UpdateResource(ctx, &store.UpdateResource{
ID: resource.ID,
Reference: &presignURL,
Reference: &referenceURL,
Payload: &storepb.ResourcePayload{
Payload: &storepb.ResourcePayload_S3Object_{
S3Object: s3ObjectPayload,
Expand Down
8 changes: 8 additions & 0 deletions web/src/components/Settings/StorageSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ const StorageSection = () => {
handlePartialS3ConfigChanged({ bucket: event.target.value });
};

const handleS3ConfigCustomDomainChanged = async (event: React.FocusEvent<HTMLInputElement>) => {
handlePartialS3ConfigChanged({ customDomain: event.target.value });
};

const handleStorageTypeChanged = async (storageType: WorkspaceStorageSetting_StorageType) => {
const update: WorkspaceStorageSetting = {
...workspaceStorageSetting,
Expand Down Expand Up @@ -180,6 +184,10 @@ const StorageSection = () => {
<span className="text-gray-700 dark:text-gray-500 mr-1">Bucket</span>
<Input value={workspaceStorageSetting.s3Config?.bucket} placeholder="" onChange={handleS3ConfigBucketChanged} />
</div>
<div className="w-full flex flex-row justify-between items-center">
<span className="text-gray-700 dark:text-gray-500 mr-1">CustomDomain</span>
<Input value={workspaceStorageSetting.s3Config?.customDomain} placeholder="" onChange={handleS3ConfigCustomDomainChanged} />
</div>
</>
)}
<div>
Expand Down