Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow MemoryFileStore store copying from different a bucket #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions tests/storage/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,30 @@ def test_download_file(self):
finally:
os.remove(file.name)

def test_copy_file(self):
# Store a file in a different source bucket
source_store = storage.MemoryFileStore("source-bucket")
contents = b"test_download_file"
source_bucket_name, source_key_path = source_store.store_file(
namespace="mem",
filename="test.pdf",
contents=contents,
)
s3_object = storage.S3Object(
bucket_name=source_bucket_name,
key=source_key_path,
)
destination = "a/b/c.pdf"
Copy link
Contributor

Choose a reason for hiding this comment

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

🐼 Since the original key path is called source_key_path, shall we rename this as destination_key_path?


# Copy the file to a different bucket
new_s3_object = self.store.copy(s3_object=s3_object, destination=destination)

# Check the file is copied to the correct bucket and content is as expected
assert new_s3_object.bucket_name == self.store.bucket_name
assert new_s3_object.key == destination
copied_contents = self.store.fetch_file_contents(destination)
assert copied_contents == contents


class TestLocalFileStore:
def test_store_and_fetch(self):
Expand Down
3 changes: 2 additions & 1 deletion xocto/storage/storage.py
Copy link
Contributor

Choose a reason for hiding this comment

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

🐼 There is a nice PR description, shall we copy that as detailed explanatory text into the commit message?
Only truly trivial changes should have a one-line commit message, see: standard commit message format.

Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,9 @@ def list_s3_keys(self, namespace: str = "") -> Iterable[S3Object]:
return [self.get_key(path) for path in self.list_files(namespace=namespace)]

def copy(self, *, s3_object: S3Object, destination: str) -> S3Object:
source_bucket = self.buffers[s3_object.bucket_name]
Copy link
Contributor

Choose a reason for hiding this comment

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

📚📚📚 Shall we add a docstring to the function?

bucket = self.buffers[self.bucket_name]
bucket[destination] = bucket[s3_object.key]
bucket[destination] = source_bucket[s3_object.key]
return S3Object(bucket_name=self.bucket_name, key=destination)

def rename(self, *, s3_object: S3Object, destination: str) -> S3Object:
Expand Down