Google Certified Associate Cloud Engineer 2020

Sign Up Free or Log In to participate!

Uploaded files in my bucket will not open in my browser, instead download to my computer. Why?

When I open up the files that are in my bucket (for example, the Readme file from the last lab that I downloaded, and then uploaded into this bucket), instead of seeing the file as a text document in my browser in a new tab, the file downloads again onto my computer. When I open up a pdf that I uploaded to my bucket it is the same situation, it downloads onto my computer instead of opening the file in a new tab on my browser. 

I’m trying to think about the mental model for the data that’s flowing in this situation but I can’t understand it. Can anyone explain why the files might be downloading to my computer from the bucket instead of opening up in my web browser?

1 Answers

In general, browsers will try to detect the MIME type of an object (set by metadata "Content-Type") to decide what do to with it. If an object has an undetermined/unknown MIME type to the browser, it will simply download that object as "regular file", rather than processing it (open, play, etc.).

So depends on your objects, you need to set their MIME accordingly. For example:

  • PDF objects need to have MIME type as application/pdf so that browsers can open them directly.

  • Regular text objects need to have MIME type as text/plain so browsers can display it without downloading.

  • etc.

You can use either the Console or gsutil to set the metadata for objects in bucket. Here’s an example with gsutil for PDF file:

gsutil setmeta -h "Content-Type:application/pdf" gs://my-example-bucket/my-document.pdf
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?