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

feat: delegate create folder #235

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
52 changes: 52 additions & 0 deletions client/api_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type IObjectClient interface {
ListObjects(ctx context.Context, bucketName string, opts types.ListObjectsOptions) (types.ListObjectsResult, error)
ComputeHashRoots(reader io.Reader, isSerial bool) ([][]byte, int64, storageTypes.RedundancyType, error)
CreateFolder(ctx context.Context, bucketName, objectName string, opts types.CreateObjectOptions) (string, error)
DelegateCreateFolder(ctx context.Context, bucketName, objectName string, opts types.PutObjectOptions) error
GetObjectUploadProgress(ctx context.Context, bucketName, objectName string) (string, error)
ListObjectsByObjectID(ctx context.Context, objectIds []uint64, opts types.EndPointOptions) (types.ListObjectsByObjectIDResponse, error)
ListObjectPolicies(ctx context.Context, objectName, bucketName string, actionType uint32, opts types.ListObjectPoliciesOptions) (types.ListObjectPoliciesResponse, error)
Expand Down Expand Up @@ -379,6 +380,47 @@ func (c *Client) putObject(ctx context.Context, bucketName, objectName string, o
return nil
}

func (c *Client) delegateCreateFolder(ctx context.Context, bucketName, objectName string, opts types.PutObjectOptions,
) (err error) {

var contentType string
if opts.ContentType != "" {
contentType = opts.ContentType
} else {
contentType = types.ContentDefault
}
urlValues := make(url.Values)

urlValues.Set("create-folder", "")
urlValues.Set("visibility", strconv.FormatInt(int64(opts.Visibility), 10))

reqMeta := requestMeta{
bucketName: bucketName,
objectName: objectName,
contentSHA256: types.EmptyStringSHA256,
contentLength: 0,
contentType: contentType,
urlValues: urlValues,
}

sendOpt := sendOptions{
method: http.MethodPost,
}

endpoint, err := c.getSPUrlByBucket(bucketName)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by bucket: %s failed, err: %s", bucketName, err.Error()))
return err
}

_, err = c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return err
}

return nil
}

// UploadSegmentHook is for testing usage
type uploadSegmentHook func(id int) error

Expand Down Expand Up @@ -1138,6 +1180,16 @@ func (c *Client) CreateFolder(ctx context.Context, bucketName, objectName string
return txHash, err
}

// DelegateCreateFolder send create empty object txn to greenfield chain
func (c *Client) DelegateCreateFolder(ctx context.Context, bucketName, objectName string, opts types.PutObjectOptions) error {
if !strings.HasSuffix(objectName, "/") {
return errors.New("failed to create folder. Folder names must end with a forward slash (/) character")
}
opts.Delegated = true

return c.delegateCreateFolder(ctx, bucketName, objectName, opts)
}

// GetObjectUploadProgress return the status of object including the uploading progress
func (c *Client) GetObjectUploadProgress(ctx context.Context, bucketName, objectName string) (string, error) {
status, err := c.HeadObject(ctx, bucketName, objectName)
Expand Down
27 changes: 27 additions & 0 deletions e2e/e2e_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func (s *StorageTestSuite) Test_Object() {
err = s.Client.DelegateUpdateObjectContent(s.ClientContext, bucketName, objectName2, newObjectSize, bytes.NewReader(newBuffer.Bytes()), types.PutObjectOptions{})
s.Require().NoError(err)
s.WaitSealObject(bucketName, objectName2)

}

func (s *StorageTestSuite) Test_Group() {
Expand Down Expand Up @@ -953,6 +954,32 @@ func (s *StorageTestSuite) Test_Get_Object_With_ForcedSpEndpoint() {
s.Client = origClient
}

func (s *StorageTestSuite) TestCreateFolder() {
bucketName := storageTestUtil.GenRandomBucketName()
objectName := storageTestUtil.GenRandomObjectName()
objectName = fmt.Sprintf("%s/", objectName)

s.T().Logf("BucketName:%s, objectName: %s", bucketName, objectName)

bucketTx, err := s.Client.CreateBucket(s.ClientContext, bucketName, s.PrimarySP.OperatorAddress, types.CreateBucketOptions{Visibility: storageTypes.VISIBILITY_TYPE_PUBLIC_READ})
s.Require().NoError(err)

_, err = s.Client.WaitForTx(s.ClientContext, bucketTx)
s.Require().NoError(err)

bucketInfo, err := s.Client.HeadBucket(s.ClientContext, bucketName)
s.Require().NoError(err)
if err == nil {
s.Require().Equal(bucketInfo.Visibility, storageTypes.VISIBILITY_TYPE_PUBLIC_READ)
}

s.T().Log("---> CreateFolder and HeadObject <---")
err = s.Client.DelegateCreateFolder(s.ClientContext, bucketName, objectName, types.PutObjectOptions{Visibility: storageTypes.VISIBILITY_TYPE_PUBLIC_READ})
s.Require().NoError(err)

s.WaitSealObject(bucketName, objectName)
}

func (s *StorageTestSuite) PutObjectWithRetry(bucketName, objectName string, objectSize int64, buffer bytes.Buffer, option types.PutObjectOptions) error {
var err error
for retry := 0; retry < 5; retry++ {
Expand Down
Loading