Hugues
Hugues

Reputation: 653

Serve file using Google Cloud Storage does not retrieve the mime type

I am storing a file in Google App Engine using Google Cloud Storage. The file is loaded fine but the serve returns a file interpreted as a binary not as the original mime type. You'll find hereafter the code. Has anyone an idea of what is happening ?

I. Store file

GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
.setBucket(BUCKET_NAME)
.setKey(objectId)
.setAcl("project-private")
.setMimeType(mimeType)
.setContentDisposition("attachment;filename "+item.getName());

II. Serve file

blobKey = blobstoreService.createGsBlobKey("/gs/"+BUCKET_NAME+"/"+ fileName);

blobstoreService.serve(blobKey, resp);

III. Difference with serving a file in the blobstore

I had a piece of code which used to work. The problem is that apparently the BlobInfo object is only for objects stored in the blobstore and not in the Google Cloud Storage

BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey); 
resp.setContentLength(new Long(blobInfo.getSize()).intValue()); 
resp.setHeader("content-type", blobInfo.getContentType()); 
resp.setHeader("content-disposition", "attachment; filename=" + 
blobInfo.getFilename()); 
        blobstoreService.serve(blobKey, resp);  

Any help is very welcome !

Thanks,

Hugues

Upvotes: 1

Views: 1387

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101139

Because you're serving the blob yourself, using the blob serving service, it's up to you to set all the HTTP headers, including the content type, correctly. If you want to use the content type of the stored object, you should fetch it and set it in the headers yourself.

Alternatively, you could link directly to the object's path in Google Storage, in which case it will be served by the Google Storage infrastructure, with the correct mimetype.

Upvotes: 1

Related Questions