David T.
David T.

Reputation: 23399

Python GCS how to rename file within inner zip file?

Suppose I have a file hosted on GCS on a Python AppEngine project. unfortunately, this file structure is something like:

outer.zip/
  - inner.zip/
    - vid_file
    - png_file

the problem is that the two files inside inner.zip do not have the .mp4 extension on the file, and it's causing all sorts of troubles. How do i rename the files so that it appears like:

outer.zip/
  - inner.zip/
    - vid_file.mp4
    - png_file.png

so that the files inside inner.zip have their extensions?

I keep running into all sorts of limitations since gcs doesn't allow file renaming, unarchiving...etc.

the files aren't terribly large.

P.S. i'm not very familiar with Python, so any code examples would be great appreciated, thanks!

Upvotes: 1

Views: 376

Answers (3)

Joe Bourne
Joe Bourne

Reputation: 1204

"Import zipfile" and you can unzip the file once it's downloaded into gcs storage. I have code doing exactly this on a nightly basis from a cron job. Ive never tried creating a zip file with GAE but the docs say you can do it. https://docs.python.org/2/library/zipfile.html

Upvotes: 0

Mike Schwartz
Mike Schwartz

Reputation: 12145

Alex is right that objects are immutable, i.e., no editing in-place. The only way to accomplish what you're talking about would be to download the current file, unzip it, update the new files, re-zip the files into the same-named file, and upload to GCS. GCS object overwrites are transactional, so the old content will be visible until the instant the upload completes. Doing it this way is obviously not very network efficient but at least it wouldn't leave periods of time when the object is invisible (as deleting and re-uploading would).

Upvotes: 2

Alex Martelli
Alex Martelli

Reputation: 882241

There is absolutely no way to perform any alteration to GCS objects -- full stop. They are exactly the bunch of bytes you decided at their birth (uninterpreted by GCS itself) and thus they will stay.

The best you can do is create a new object which is almost like the original except it fixes little errors and oopses you did when creating the original. Then you can overwrite (i.e completely replace) the original with the new, improved version.

Hopefully it's a one-off terrible mistake you made just once and now want to fix so it's not worth writing a program for that. Just download that GCS object, use normal tools to unzip it and unzip any further zipfiles it may contain, do the fixes on the filesystem with your favorite local filesystem tools, zip things up again, upload/rewrite the final zip to your desired new GCS object -- phew, you're done.

Upvotes: 2

Related Questions