Skip to content

Renaming a file to have the same name causes it to be deleted, error raised #277

@spdjudd

Description

@spdjudd

Hi,

Thanks for your work on cloudpathlib, much appreciated!

I came across this issue when running a script to rename a set of files - one of them happened to have the correct name already, but calling CloudPath('s3://bucket/folder/filea.pq').rename(CloudPath('s3://bucket/folder/filea.pq')) causes the file to be lost, and an error raised:

    448 def rename(self, target: "CloudPath") -> "CloudPath":
    449     # for cloud services replace == rename since we don't just rename,
    450     # we actually move files
--> 451     return self.replace(target)

File ~\anaconda3\lib\site-packages\cloudpathlib\cloudpath.py:445, in CloudPath.replace(self, target)
    442 if target.exists():
    443     target.unlink()
--> 445 self.client._move_file(self, target)
    446 return target

File ~\anaconda3\lib\site-packages\cloudpathlib\s3\s3client.py:217, in S3Client._move_file(self, src, dst, remove_src)
    213 if src == dst:
    214     o = self.s3.Object(src.bucket, src.key)
    215     o.copy_from(
    216         CopySource={"Bucket": src.bucket, "Key": src.key},
--> 217         Metadata=self._get_metadata(src).get("extra", {}),
    218         MetadataDirective="REPLACE",
    219     )
    221 else:
    222     target = self.s3.Object(dst.bucket, dst.key)

...
NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The object 'folder/filea.pq' was not found in the bucket 'bucket'

Seems like checking target to see if it's the same path as self before unlinking could prevent this - happy to create a PR if that sounds ok?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions