From c9298def5b086530594abf330d7e527a47c2acf1 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Thu, 3 Aug 2023 20:33:44 -0700 Subject: [PATCH] Cleanup object storage files on success or failure (#3231) * fix: cleanup object storage files on success or failure. Closes #3230 * fix: clean filepath when deleting for safety --- core/storageproviders/s3Storage.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/storageproviders/s3Storage.go b/core/storageproviders/s3Storage.go index cef381693..72c6aa332 100644 --- a/core/storageproviders/s3Storage.go +++ b/core/storageproviders/s3Storage.go @@ -184,9 +184,15 @@ func (s *S3Storage) Save(filePath string, retryCount int) (string, error) { return s.Save(filePath, retryCount+1) } + // Upload failure. Remove the local file. + s.removeLocalFile(filePath) + return "", fmt.Errorf("Giving up uploading %s to object storage %s", filePath, s.s3Endpoint) } + // Upload success. Remove the local file. + s.removeLocalFile(filePath) + return response.Location, nil } @@ -252,6 +258,14 @@ func (s *S3Storage) getDeletableVideoSegmentsWithOffset(offset int) ([]s3object, return objectsToDelete, nil } +func (s *S3Storage) removeLocalFile(filePath string) { + cleanFilepath := filepath.Clean(filePath) + + if err := os.Remove(cleanFilepath); err != nil { + log.Errorln(err) + } +} + func (s *S3Storage) deleteObjects(objects []s3object) { keys := make([]*s3.ObjectIdentifier, len(objects)) for i, object := range objects {