Search code examples
gsutil

gsutil setmeta: quotes and spaces within "Content-Disposition" metadata


I'm using gsutil setmeta to set a custom filename for downloading, by means of Content-Disposition property (Content-Disposition: attachment; filename="filename.jpg"; filename*="filename.jpg").

Filenames must be enclosed in quotes. And also the argument of setmeta has its own quotes.

If I do

gsutil setmeta -h "Content-Disposition: attachment; filename="Myname.jpg"; filename*="Myname.jpg"" gs://<path-to-file>"

is ok, unless the file name I specify in the metadata contains spaces (e.g. filename="My name.jpg"): in this case, I get an error. Is there a way to escape the quotes, or else a way to have spaces within the download filename?

Single quotes seems not to be an option (they are shown in the file name when I download it).

EDIT: I tried replacing spaces with %20 (cf. How to set filename containing spaces in Content-Disposition header). It's a step forward: gsutil does the job, and if I download the file with Chrome it's ok. But if I use Safari to download the file, the %20 are literaly there.


Solution

  • You can surround the filename in double quotes and escape any inner quotes within the filename to set a custom filename for downloading using gsutil and the Content-Disposition metadata. Replace any spaces in the filename with %20 to comply with URL encoding.

    Here's an illustration:

    gsutil setmeta -h "Content-Disposition: attachment; filename=\"My name.jpg\"; filename*=utf-8''My%20name.jpg" gs://<path-to-file>
    
    

    This eliminates issues when setting a filename that contains spaces. The correct filename, "My name.jpg," without any visible quotations, will show in the downloaded image.