Skip to content

Commit

Permalink
feat: delegate create folder
Browse files Browse the repository at this point in the history
  • Loading branch information
constwz committed Mar 28, 2024
1 parent a21d3b5 commit c88fd8b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
53 changes: 53 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,48 @@ 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,
}

var sendOpt sendOptions

Check failure on line 406 in client/api_object.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

S1021: should merge variable declaration with assignment on next line (gosimple)
sendOpt = sendOptions{
method: http.MethodGet,
}

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 +1181,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

0 comments on commit c88fd8b

Please sign in to comment.