Skip to content

Commit f55dda1

Browse files
committed
fix(uploadservice): preserve path for S3 upload with subdirs
`path.basename(filename)` strips as well the relative path from the filename, which breaks the ability to upload to S3 buckets with subdirectories included via slashes in the filename input. This commit takes the logic from the base upload_file method to preserve the correct relative path if subdirectories shall be included, stripping away possible leading slashes, which was another issue that could appear. Additionally, the leading slashes are now `lstrip`ped from the path, instead of using a while loop, simplifying code, and the debug message now shows the path the file is uploaded to. Signed-off-by: MichaIng <micha@dietpi.com>
1 parent 5c0b7da commit f55dda1

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

motioneye/uploadservices.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ def upload_file(self, target_dir, filename, camera_name):
6161
if target_dir:
6262
target_dir = os.path.realpath(target_dir)
6363
rel_filename = os.path.realpath(filename)
64-
rel_filename = rel_filename[len(target_dir) :]
65-
66-
while rel_filename.startswith('/'):
67-
rel_filename = rel_filename[1:]
64+
rel_filename = rel_filename[len(target_dir) :].lstrip('/')
6865

6966
self.debug(f'uploading file "{target_dir}/{rel_filename}" to {self}')
7067

@@ -1221,10 +1218,20 @@ def upload_file(self, target_dir, filename, camera_name):
12211218
aws_secret_access_key=self._secret_key,
12221219
)
12231220

1221+
if target_dir:
1222+
target_dir = os.path.realpath(target_dir)
1223+
rel_filename = os.path.realpath(filename)
1224+
rel_filename = rel_filename[len(target_dir) :].lstrip('/')
1225+
1226+
else:
1227+
rel_filename = os.path.basename(filename)
1228+
12241229
# Uploads the given file using a managed uploader, which will split up
12251230
# large files automatically and upload parts in parallel.
1226-
self.debug(f'uploading file "{filename}" to S3 bucket "{self._bucket}"')
1227-
s3.upload_file(filename, self._bucket, os.path.basename(filename))
1231+
self.debug(
1232+
f'uploading file "{filename}" to S3 bucket "{self._bucket}" path "{rel_filename}"'
1233+
)
1234+
s3.upload_file(filename, self._bucket, rel_filename)
12281235

12291236
def test_access(self):
12301237
try:

0 commit comments

Comments
 (0)