From 144ba0e1e4722fba77d6babb9864af0d1a79d47c Mon Sep 17 00:00:00 2001 From: Zherphy Date: Fri, 22 Nov 2024 09:09:30 +0800 Subject: [PATCH 1/2] add: add-repeat-upload-check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 校验上传文件是否已存在 --- server/server.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/server/server.go b/server/server.go index aa78bd5..81c0cbc 100644 --- a/server/server.go +++ b/server/server.go @@ -194,11 +194,8 @@ func (s *server) dealWithAuthError(userInRepo auth.UserInRepo, w http.ResponseWr } func (s *server) downloadObject(in *batch.RequestObject, out *batch.Object) { - getObjectMetadataInput := &obs.GetObjectMetadataInput{ - Bucket: s.bucket, - Key: s.key(in.OID), - } - if metadata, err := s.client.GetObjectMetadata(getObjectMetadataInput); err != nil { + getObjectMetadataInput := s.getObjectMetadataInput(s.bucket, s.key(in.OID)) + if metadata, err := s.client.GetObjectMetadata(&getObjectMetadataInput); err != nil { out.Error = &batch.ObjectError{ Code: 404, Message: err.Error(), @@ -237,6 +234,12 @@ func (s *server) uploadObject(in *batch.RequestObject, out *batch.Object) { return } + checkInput := s.getObjectMetadataInput(s.bucket, s.key(in.OID)) + _, err := s.client.GetObjectMetadata(&checkInput) + if err == nil { + return + } + putObjectInput := &obs.CreateSignedUrlInput{} putObjectInput.Method = obs.HttpMethodPut putObjectInput.Bucket = s.bucket @@ -257,6 +260,13 @@ func (s *server) uploadObject(in *batch.RequestObject, out *batch.Object) { } } +func (s *server) getObjectMetadataInput(bucket string, key string) obs.GetObjectMetadataInput { + return obs.GetObjectMetadataInput{ + Bucket: bucket, + Key: key, + } +} + // 生成下载对象的带授权信息的URL func (s *server) generateDownloadUrl(getObjectInput *obs.CreateSignedUrlInput) *url.URL { // 生成下载对象的带授权信息的URL From 4bcf8c370df772829a20d77537ed92974e7cbbd9 Mon Sep 17 00:00:00 2001 From: Zherphy Date: Fri, 22 Nov 2024 10:08:08 +0800 Subject: [PATCH 2/2] add: add-repeat-upload-check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加日志 --- server/server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/server.go b/server/server.go index 81c0cbc..26d4548 100644 --- a/server/server.go +++ b/server/server.go @@ -237,6 +237,7 @@ func (s *server) uploadObject(in *batch.RequestObject, out *batch.Object) { checkInput := s.getObjectMetadataInput(s.bucket, s.key(in.OID)) _, err := s.client.GetObjectMetadata(&checkInput) if err == nil { + logrus.Infof("object already exists: %s", in.OID) return }