Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

feat: Implement CreateLink and setup linker test in go-service-oss #48

Merged
merged 4 commits into from
Aug 4, 2021
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
coverage.*
bin/
Makefile.env
Makefile.env

# Jetbrain IDE
.idea
*.iml
72 changes: 72 additions & 0 deletions generated.go

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

6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ go 1.14
require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/beyondstorage/go-endpoint v1.0.1
github.com/beyondstorage/go-integration-test/v4 v4.2.0
github.com/beyondstorage/go-storage/v4 v4.4.0
github.com/beyondstorage/go-endpoint v1.1.0
github.com/beyondstorage/go-integration-test/v4 v4.3.0
github.com/beyondstorage/go-storage/v4 v4.4.1-0.20210730075750-6e541b87ea46
github.com/google/uuid v1.3.0
github.com/satori/go.uuid v1.2.0 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
Expand Down
15 changes: 6 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible h1:mO8fA9l5cQ7r0D2v3Wrib
github.com/aliyun/aliyun-oss-go-sdk v2.1.9+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA=
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
github.com/beyondstorage/go-endpoint v1.0.1 h1:F8x2dGLMu9je6g7zPbKoxCXDlug97K26SeCx7KEHgyg=
github.com/beyondstorage/go-endpoint v1.0.1/go.mod h1:P2hknaGrziOJJKySv/XnAiVw/d3v12/LZu2gSxEx4nM=
github.com/beyondstorage/go-integration-test/v4 v4.2.0 h1:h2+SLmlDqjfBg+NzVcDr6VCmcD7I2xG+mqMzDlaCG+0=
github.com/beyondstorage/go-integration-test/v4 v4.2.0/go.mod h1:jLyYWSGUjQRH7U1HdaLbXE5sxBgqrtK73q+Q7PGIuSs=
github.com/beyondstorage/go-storage/v4 v4.3.0/go.mod h1:0fdcRCzLKMQe7Ve4zPlyTGgoPYwuINiV79Gx9tCt9tQ=
github.com/beyondstorage/go-storage/v4 v4.4.0 h1:sWURraKFjNR4qpwthr45cAGOIx6EOLrrJcz6su4Je30=
github.com/beyondstorage/go-endpoint v1.1.0 h1:cpjmQdrAMyaLoT161NIFU/eXcsuMI3xViycid5/mBZg=
github.com/beyondstorage/go-endpoint v1.1.0/go.mod h1:P2hknaGrziOJJKySv/XnAiVw/d3v12/LZu2gSxEx4nM=
github.com/beyondstorage/go-integration-test/v4 v4.3.0 h1:WZ95f78RKlHpvft8zHcMaoa2aaTF/jzlzINhMD0EMHY=
github.com/beyondstorage/go-integration-test/v4 v4.3.0/go.mod h1:HKgzemQZpxoHBL49JYEUnLTb5eteUhzcvmmPL7EDT/Y=
github.com/beyondstorage/go-storage/v4 v4.4.0/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw=
github.com/beyondstorage/go-storage/v4 v4.4.1-0.20210730075750-6e541b87ea46 h1:sUmtn3cWgpjetIv/lSzZ6AA9GnzvxPjalTvRY1ZDzOg=
github.com/beyondstorage/go-storage/v4 v4.4.1-0.20210730075750-6e541b87ea46/go.mod h1:mc9VzBImjXDg1/1sLfta2MJH79elfM6m47ZZvZ+q/Uw=
github.com/dave/dst v0.26.2 h1:lnxLAKI3tx7MgLNVDirFCsDTlTG9nKTk7GcptKcWSwY=
github.com/dave/dst v0.26.2/go.mod h1:UMDJuIRPfyUCC78eFuB+SV/WI8oDeyFDvM/JR6NI3IU=
github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ=
Expand All @@ -20,11 +20,9 @@ github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWE
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/google/pprof v0.0.0-20181127221834-b4f47329b966/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand Down Expand Up @@ -63,7 +61,6 @@ golang.org/x/arch v0.0.0-20180920145803-b19384d3c130/go.mod h1:cYlCBUl1MsqxdiKgm
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
2 changes: 1 addition & 1 deletion service.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ optional = ["service_features", "default_service_pairs", "endpoint", "http_clien

[namespace.storage]
features = ["virtual_dir"]
implement = ["appender", "direr", "multiparter"]
implement = ["appender", "direr", "multiparter", "linker"]

[namespace.storage.new]
required = ["name"]
Expand Down
35 changes: 35 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@ func (s *Storage) createDir(ctx context.Context, path string, opt pairStorageCre
return
}

func (s *Storage) createLink(ctx context.Context, path string, target string, opt pairStorageCreateLink) (o *Object, err error) {
rt := s.getAbsPath(target)
rp := s.getAbsPath(path)

// oss `symlink` supports `overwrite`, so we don't need to check if path exists.
err = s.bucket.PutSymlink(rp, rt)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to call API first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it!

if err != nil {
return nil, err
}

o = s.newObject(true)
o.ID = rp
o.Path = path
// oss does not have an absolute path, so when we call `getAbsPath`, it will remove the prefix `/`.
// To ensure that the path matches the one the user gets, we should re-add `/` here.
o.SetLinkTarget("/" + rt)
o.Mode |= ModeLink

return
}

func (s *Storage) createMultipart(ctx context.Context, path string, opt pairStorageCreateMultipart) (o *Object, err error) {
rp := s.getAbsPath(path)

Expand Down Expand Up @@ -454,6 +475,20 @@ func (s *Storage) read(ctx context.Context, path string, w io.Writer, opt pairSt
func (s *Storage) stat(ctx context.Context, path string, opt pairStorageStat) (o *Object, err error) {
rp := s.getAbsPath(path)

if symlink, err := s.bucket.GetSymlink(rp); err == nil {
// The path is a symlink.
o = s.newObject(true)
o.ID = rp
o.Path = path

target := symlink.Get(oss.HTTPHeaderOssSymlinkTarget)
o.SetLinkTarget("/" + target)

o.Mode |= ModeLink

return o, nil
}

if opt.HasMultipartID {
_, err = s.bucket.ListUploadedParts(oss.InitiateMultipartUploadResult{
Bucket: s.bucket.BucketName,
Expand Down
7 changes: 7 additions & 0 deletions tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ func TestDirer(t *testing.T) {
}
tests.TestDirer(t, setupTest(t))
}

func TestLinker(t *testing.T) {
if os.Getenv("STORAGE_OSS_INTEGRATION_TEST") != "on" {
t.Skipf("STORAGE_OSS_INTEGRATION_TEST is not 'on', skipped")
}
tests.TestLinker(t, setupTest(t))
}
11 changes: 6 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Storage struct {
typ.UnimplementedAppender
typ.UnimplementedMultiparter
typ.UnimplementedDirer
typ.UnimplementedLinker
}

// String implements Storager.String
Expand Down Expand Up @@ -254,15 +255,15 @@ func (s *Storage) formatFileObject(v oss.ObjectProperties) (o *typ.Object, err e
o = s.newObject(false)
o.ID = v.Key
o.Path = s.getRelPath(v.Key)
o.Mode |= typ.ModeRead
if v.Type == "Symlink" {
abyss-w marked this conversation as resolved.
Show resolved Hide resolved
o.Mode |= typ.ModeLink
} else {
o.Mode |= typ.ModeRead
}

o.SetContentLength(v.Size)
o.SetLastModified(v.LastModified)

if v.Type != "" {
o.SetContentType(v.Type)
}

// OSS advise us don't use Etag as Content-MD5.
//
// ref: https://help.aliyun.com/document_detail/31965.html
Expand Down