Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Download File as Name

Hello.

I'd like to download a file as a name. I've seen this on Stack:

  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.

But there's no working example. In the SDK docs we get an example of how to possibly set metadata, but that doesn't have working examples either:

 $storage = new StorageClient();
    $bucket
= $storage->bucket($bucketName);
    $object
= $bucket->object($objectName);
    $object
->update([
       
'metadata' => [
           
'keyToAddOrUpdate' => 'value',
       
]
   
]);

Can someone tell me how to permanently set the download name of a file? The key there could be content-disposition, contentDisposition. The value could be anything. We need less pages of docs and more code with this platform.

1 8 3,643
8 REPLIES 8

Jeremy, It sounds like you want to do an equivalent of gcloud cp gs://myBucket/myRemoteFile.png myLocalFile.png?  If so, than https://cloud.google.com/storage/docs/downloading-objects#storage-download-object-nodejs is a complete example in node.  I do see that you mention setting metadata.  That sounds like red herring to me, but I'd like what you are trying to do better.

I'm not sure what you mean by red herring. In the message on Stack the user said it does exactly what I'm trying to do. What you're pointing to, is how to download a file. I need to download a file under a different name. See Google gets you to set the name of a file when you upload it. But since many users are uploading it has to be something like upload_filea84899be3c4de405bddf32f0ee035cbd because not every name may be unique. That's not a convenient file name to download as, since I'll be showing the download file name that was attached to the file input. So I need to download that file as the original name, say mySong.mp3 or SomeProject.mp4 

Here's a link:

Google Cloud Storage: download a file with a different name - Stack Overflow

But like I said, it would be nice to get a working example. Google has made some of the most important tasks in their workflow obscure. What's the point of having something so integral locked away in the minds of Google techs? I would think every user is trying to do this.

Hi Jeremy, I totally understand what you are trying to do and I am trying to accomplish the exact same thing right this very moment.  I am going in circles and cannot figure out how to programmatically set the Content-Disposition value to 'attachment; filename="save-as-filename-here";

I can update them manually using the Google Storage interface but that value does not show up if I query the object->info();  so I am completely at a loss.  Did you ever find a solution?

I see you have a destination name in there, so that's pretty good, but wrong approach. I'm allowing users to download these objects to their computers. So I'm not downloading them to the server. .. Something like this:

$bucket = $storage->bucket($jobFilesBucket);
$object = $bucket->object($index['bucketFileName']);
$url = $object->signedUrl(new \DateTime('+ ' . 86400 . ' seconds'));

echo $url;

So you want the user to be able to click on a link that resolves to a GCS signed URL and download a file from GCS bypassing your server. And that file has to have a custom name?

I think that's a good assumption @KirTitievsky, it is indeed hard to find a working example of how to set the metadata of files. In our case we had to set the filename in the DB along with the GCS link. Then generate the download URL and pass it to frontend. However, since the option of setting file metadata is available, it would be great to have an example of how to set it to change the download name of the file, while keeping the unique filename on Storage.

We tried:

  1. Passing the name along with the AJAX request as @JeremyBenson11 showed, or
  2. Embedded within the AJAX request url as below

 

$.ajax({
  url: file_upload_url+'&response-content-disposition=attachment%3B%20filename%3D'+f.name,
  type: 'PUT',
  data: f,
  contentType: 'application/octet-stream',
  processData: false,
})

 

 
However, neither worked unfortunately. And the files were uploaded without the metadata settings in the AJAX request.

Can you say more about why you setting the object name to the downloaded file name you want does not work for you? Trying to understand so we can support this better?

To try to answer your question more directly, I believe you are talking about this metadata field with the "Disposition Paramater: 'Filename'" . You've already found the example of how to set the content metadata field.  In the above example you would set 'Content-Disposition' => 'filename="FILENAME"'.  I haven't tested this so can't guarantee that it works. But this is how you would do it. Would you please report back on how it went if you try this?