12

I'm wondering if it's possible to download a file from Google Cloud Storage with a different name than the one that has in the bucket.

For example, in Google Cloud Storage I have stored a file named 123-file.txt but when I download it I would like choose a different name, let's say file.txt

I've noticed that the link for download it is like: https://storage.cloud.google.com/bucket_name%2F123-file.txt?response-content-disposition=attachment;%20filename=123-file.txt

So I've tried to change it to: https://storage.cloud.google.com/bucket_name%2F123-file.txt?response-content-disposition=attachment;%20filename=file.txt

But it still keeps downloading as 123-file.txt instead of file.txt

dablak
  • 1,376
  • 1
  • 11
  • 21

3 Answers3

12

The response-content-disposition parameter can only be used by authorized requests. Anonymous links don't work with it. You have a few options:

  1. The content-disposition of a particular object is part of its metadata and can be permanently set. If you always want a specific file to be downloaded with a specific name, you can just permanently set the content-disposition metadata for the object.

  2. You can also generate signed URLs that include the response-content-disposition query parameter. Then the users will be making authorized requests to download the resource.

Brandon Yarbrough
  • 37,021
  • 23
  • 116
  • 145
  • 3
    The signed urls worked for me. Although, I've tried to call generate_url() with the parameter response_headers and the value response-content-disposition but I got malformed signed urls. So my solution has been to concatenate '&response-content-disposition=attachment%3B%20filename%3D"{}"'.format(file_name) to the signed url and it worked. – dablak Feb 19 '14 at 16:02
  • 2
    Where is the response-content-disposition query parameter documented? What are the other valid query parameters? – Robert D Dec 18 '14 at 00:41
  • A complete list of valid XML API parameters is at https://cloud.google.com/storage/docs/reference-headers – Brandon Yarbrough Dec 19 '14 at 08:16
  • @BrandonYarbrough I am using .net client libraries(latest version as of today) to upload files.Could you please let me know where in the code I need to set the the content-disposition which class and which method/property I need to use. I am searching throughout google but couldn't find any example. could you please help on this – umsateesh Jul 22 '18 at 05:20
5

example (first option Brandon Yarbrough) with javascript library:

const storage = new Storage()
const fileBucket = storage.bucket('myBucket')
const file = fileBucket.file('MyOriginalFile.txt')
const newName = "NewName.txt"
await file.save(content, {
    metadata: {
      contentDisposition: `inline; filename="${newName}"`
    }
  })
vitaliytv
  • 694
  • 7
  • 9
1

the following is a part of a python script i've used to remove the forward slashes - added by google cloud buckets when to represent directories - from multiple objects, it's based on this blog post, please keep in mind the double quotes around the content position "file name"

def update_blob_download_name(bucket_name):
    """ update the download name of blobs and remove
    the path. 
    :returns: None
    :rtype: None
    """
    # Storage client, not added to the code for brevity 
    client = initialize_google_storage_client()
    bucket = client.bucket(bucket_name)
    for blob in bucket.list_blobs():
        if "/" in blob.name:
            remove_path = blob.name[blob.name.rfind("/") + 1:] # rfind gives that last occurence of the char
            ext = pathlib.Path(remove_path).suffix
            remove_id = remove_path[:remove_path.rfind("_id_")]
            new_name = remove_id + ext
            blob.content_disposition = f'attachment; filename="{new_name}"'
            blob.patch()
Mohab Khaled
  • 167
  • 1
  • 8