-1

How do you move files within the same storage bucket to different folders while keeping their version history? I couldn't find any related option for gsutil mv/cp.

Andi
  • 650
  • 2
  • 8
  • 22
  • 1
    You cannot move Cloud Storage objects. The process is to copy and delete. That process does not retain versions as you are only copying a version of an object. – John Hanley May 10 '23 at 08:55
  • @JohnHanley "You cannot move cloud storage objects" is wrong, see the `gsutil mv`[command](https://cloud.google.com/storage/docs/gsutil/commands/mv). – Andi May 10 '23 at 10:05
  • 1
    Thank you for the clarification. Please post your comment as the answer. A few minor suggestions about Google Cloud Storage: Cloud Storage objects cannot be modified except for metadata and storage class. Cloud Storage Objects are **immutable**. Move operations are a copy and delete. The `gcloud mv` command is a combination of copy and delete. – John Hanley May 10 '23 at 10:16
  • My comment isn't the answer. I was trying to find a way to move files with their version history and still don't know how to do it with out of the box tooling. I would be really surprised if that cannot be done. It's a very basic feature/use case I would say. – Andi May 10 '23 at 13:33

2 Answers2

4

Moving objects (e.g., using gsutil mv) is accomplished using a copy followed by a delete, so the moved object is really a new object. There's no way to re-associate the version history of the old object to the new object.

gsutil cp supports copying noncurrent versions of files and since a move is just a copy followed by a delete, you could first copy with the metadata preservation option and then delete.

gsutil cp -A gs://bucket-name/oldDir/file gs://bucket-name/newDir/file
gsutil rm gs://bucket-name/oldDir/file
Mike Schwartz
  • 11,511
  • 1
  • 33
  • 36
  • 1
    gsutil is simply a CLI that makes API calls for you. The GCS service and its APIs do not support "moving" objects in the manner you're wanting. An object is not a file; it does not have the same semantics as a traditional file, and it cannot be moved in the same way you can move a file on a filesystem. An object exists at a given name (and version, if you have versioning enabled). Its name cannot be changed; however, you can create a new object (with a different name) with the same contents and metadata, and delete the old one, which is similar to (but not 100% the same as) "moving" it. – mhouglum May 10 '23 at 21:28
0

Answering as a Community Wiki, so that it may help others too.

Using gsutil mv command will allow you to perform a copy from source to destination followed by removing the source for each object.

Also as @John Henley stated in the comment and as per the Documentation.

Note that while some tools in Cloud Storage make an object move or rename appear to be a unique operation, they are always a copy operation followed by a delete operation of the original object, because objects are immutable.

  • 1
    This wasn't my question. The point is about **keeping** the version history of a file. If a file has 11 versions they should exist after the move as well. I don't care whether the utility internally copies version by versions and adopting metadata or does it in some other way... – Andi May 10 '23 at 13:38