Google Cloud Storage Force Download - google-app-engine

I am storing my files in Google Cloud Storage. I would like to provide downloadable links. For example https://yeketakclub.storage.googleapis.com/audios/yeketak.club-dm7aEYv7R53JRlti3HHn.mp3 one of audio files stored in google cloud storage. But when it is clicked browser tries to open it. Is it possible to force download?

The correct answer is neither of these! (if you don't want to edit a file's metadata) Add this on the end of any signed url:
&response-content-disposition=attachment;
This will make all storage links force a download instead of opening.

You can signal browsers to download the object while still retaining an accurate content type by setting the content disposition to attachment. For, example using gsutil you can do this like so:
gsutil setmeta -h 'Content-Disposition:attachment' gs://yeketakclub/audios/yeketak.club-dm7aEYv7R53JRlti3HHn.mp3
Now your object can still have the correct content type of "audio/mpeg3" (or whatever happens to match the object's content).

I'm not sure if this is necessarily a Google Cloud Storage issue (I might be wrong). The link provided there is downloadable. It just happens to be that your browser "prefers" to play it most probably because it recognises the MIME Type as one that can be handled.
In Chrome for instance, you can force download of the file by using alt + click.
Or you can right click and save link as...

In the Google Console bucket area you can click the menu and edit metadata on the object and set the Content-Disposition to attachment

edit metadata -> set Content-Disposition = attachment
image

Related

Google Storage Image Serving Cache

Using the GoogleStorageTools class's CloudStorageTools::getImageServingUrl and then replacing the storage object of the image with another image of the same name, the old image is still displayed upon subsequent calls of getImageServingUrl
I tried using CloudStorageTools::deleteImageServingUrl and then CloudStorageTools::getImageServingUrl again, but this doesn't work.
Is there any way to interact with Cloud Storage and tell it to refresh the image or the image URL? I'm guessing not, and am going to ensure the filenames are unique, instead, but it feels like there ought to be a way.
If you refresh the image, does the new image show up? It's possible there's a cache-control policy set on the image. Google Cloud Storage allows users to specify what cache-control headers should be sent to browsers, but I'm not sure whether app engine's getImageServingUrl respects that value.
As an experiment, could you try going to console.developers.google.com, heading over to "storage > cloud storage > storage browser", choosing the appropriate object, choosing "edit metadata," and then seeing whether there's a Cache-Control policy on the object? Try changing the cache-control section to "max-age=0,no-cache".

open document in browser without generating file in server side

I have stored user document (ms word) in data store as blob object.
How do I open this in a browser? I can make it downloadable but I want user to view the document in browser before downloading it. how do I do that?
I think, to be able to open the file in browser by setting response headers (inline and content-type), the file should physically exist on a file location.
I don't have space to generate the file and then write to the browser. How do I handle it?
can I store them in google drive? does reading and uploading will be charged? any pointer to examples?
daoIntf=new DAOImpl();
document = daoIntf.getEntity("Document", id);
Blob docBlob=(Blob)document.getProperty("resume");
String fileName=(String)document.getProperty("fileName");
String contentType=(String)document.getProperty("contentType");
response.setContentType(contentType);
response.setHeader("Pragma" , "no-cache");
response.setHeader("Cache-Control" , "no-cache");
response.setDateHeader("Expires" , 0);
response.setHeader("Content-Disposition" , "inline;filename=\""+fileName+"\"");
ServletOutputStream out = response.getOutputStream();
out.write(docBlob.getBytes());
-thanks
Ma
If you have a document in the Blobstore, you simply need to provide a link to it (i.e. .getServingUrl() in Java). When a user clicks on that link, his browser will either offer to download it, or it will open it in the preview mode if this user has a plugin or extension that can do it.
There is an online Google Docs viewer that works on external documents: https://docs.google.com/a/iddiction.com/viewer?pli=1
Expose your documents via servlet using blobstoreService.serve(key, response) and then open them in the viewer.
http://docs.google.com/viewer?url=<url_of_your_document>&embedded=<true|false>

Google Drive MD5 checksum for files

I'm not a programmer, just a regular user of Google Drive. I want to see if the files are uploaded correctly. I go through a whole process in the OAuth 2.0 Playground that lists all files, shows the MD5 checksums but also lots of information per file. If I upload a new file it's hard to search for it and verify its md5 checksum.
Is there an easier way (through an app, maybe?) to show/list MD5 checksums for the uploaded files? I wonder why the Details pane doesn't have it, only lists the file size in bytes.
edit: NB these instructions have changed slightly for the v3 API
I've figured out a quick way to get the MD5 checksums of the files uploaded and decided to share it here, too. Log into your Google Drive account, then:
Visit: https://developers.google.com/drive/v3/reference/files/list
Scroll down to the Try it! section.
Change "Authorize requests using OAuth 2.0" from OFF to ON by clicking on it, then select:
https://www.googleapis.com/auth/drive.metadata.readonly
and click Authorize.
Choose your account then click Accept.
Fill in the fields field with:
for v2 API:
items(md5Checksum,originalFilename)
for v3 API:
open "Show standard parameters" in GUI to see fields than
files(md5Checksum,originalFilename)
to only get a list of filenames and MD5 checksums.
Click Execute and you'll open a list with all the files uploaded to Google Drive and their MD5 checksums.
API instructions
Google Developers - OAuth 2.0 Playground:
https://developers.google.com/oauthplayground/
Step 1: Select & authorize APIs:
Expand "Drive API v3".
Enable "https://www.googleapis.com/auth/drive.metadata.readonly".
Click "Authorize APIs".
Click "Allow".
Step 2: Exchange authorization code for tokens:
Click "Exchange authorization code for tokens".
Step 3: Configure request to API:
Enter the "Request URI".
Click "Send the request".
Request URI instructions
All files in folder
Get specific fields of files in a folder:
https://www.googleapis.com/drive/v3/files?q="folderId"+in+parents&fields=files(md5Checksum,+originalFilename)
//
Replace "folderId" with the folder ID.
You can use &fields=files(*) to get all of the file's fields.
Single file
Get specific fields of a file:
https://www.googleapis.com/drive/v3/files/fileId?fields=md5Checksum,+originalFilename
//
Replace "fileId" with the file ID.
You can use &fields=* to get all of the file's fields.
Parsing the JSON response
Open a JavaScript console.
Save the object into a variable.
Map the object.
Copy the result.
Code
var response = {
"files": [
{
"md5Checksum": "0cc175b9c0f1b6a831c399e269772661",
"originalFilename": "a.txt"
},
{
"md5Checksum": "92eb5ffee6ae2fec3ad71c777531578f",
"originalFilename": "b.txt"
}
]
};
var result = response.files.map(function (file) { return (file.md5Checksum + " *" + file.originalFilename); }).join("\r\n");
console.log(result);
copy(result);
Here are three additional, different ways to list md5 checksums.
Install Google Skicka, a command line tool for Google Drive and run skicka ls -ll /
Although the readme file says it's not an official google product it is hosted on google's github account, so I guess it can be trusted.
There is a plugin that lists all files with their checksums in drive's spreadsheet.
Here's my python3 script that I've created for myself. It's mostly copied from google's official examples. You'll need to obtain client_secret.json file and place it in the same directory with the script - here's the instruction how to do it.
Based on: Alex's above answer!
Click the link : https://developers.google.com/drive/v3/reference/files/list
Click the Try it now link in the middle.
( An active window appears in the middle )
Scroll down the left pane in the active window.
Under fields section on the left pane, fill
files(md5Checksum,originalFilename)
Now we will limit access scopes :
(i) leave the Google OAuth 2.0 selected & clear the box against API key.
(ii) Expand Show scopes under Google OAuth 2.0
(iii) Clear all the scopes but keep this one selected:
**https: //www.googleapis.com/auth/drive.metadata.readonly**
Now click EXECUTE in blue.
(A new Google Sign In Window will open)
Use that window to Sign in with the respective google account & click Allow to permit the Google APIs Explorer access files in your google drive.
It's done! A new window will open with the results in lower right code pane. It will
provide the names & md5Checksums for all the files in the respective google
drive account.
Click outside of the active window to close the window & close the Google Drive
API tab. Now you can sign out of the google account if you want!
Combined with XP1 and Alex guides to work in my scenario, to list MD5 for private folders that shared with me
-includeItemsFromAllDrives
-includeTeamDriveItems
-supportsAllDrives
-supportsTeamDrives
Request URI in OAuth 2.0 Playground
https://www.googleapis.com/drive/v3/files?q="folderID"+in+parents&includeItemsFromAllDrives=true&includeTeamDriveItems=true&supportsAllDrives=true&supportsTeamDrives=true&fields=files(md5Checksum%2CoriginalFilename)

From Drive to Blobstore using Picker

I have the Google picker set up, as well as Blobstore. I'm able to upload files from my local machine to the Blobstore, but now I have the Picker set up, it works, but I don't know know how to use the info (url? fileid?) to then load that selected file into the Blobstore? Any tips on how to do this? I haven't been able to find much of anything on it on Googles resources
There isn't a direct link between the Google Picker and the App Engine Blobstore. They are kind of different tools for different jobs. The Google Picker is designed as an end user tool, to select data from a users Google account. It just so happens that the Picker also provides an upload interface (to Google Drive) as well. The Blobstore on the other hand, is designed as a blob storage mechanism for your App Engine application.
In theory, you could write a script to connect the two, but there are a few considerations:
Your app would need access to the users Google Drive account using OAuth2. This is necessary, as the Picker API is a client side API, whereas the Blobstore API is a server side API. You would need to send the selected document URL to the server, then download the document and finally save it to Blobstore.
Unless you then deleted the data from Drive (very risky due to point 3), your data would be persisted in 2 places
You cannot know for sure if the user selected an existing file, or uploaded a new one
Not a great user experience - the user things they are uploading to Drive
In essence, this sounds like a bad idea! What is your use case?
#Gwyn - I don't have enough reputation to add a comment to your solution, but I had an idea about problem #3: You cannot know for sure if the user selected an existing file, or uploaded a new one
Would it be possible to use Response.VIEW to see what view they were using when the file was selected? If you have one view constructor for Drive files and one for Upload files, something like
var driveView = new google.picker.View(google.picker.ViewId.DOCS);
var uploadView = new google.picker.DocsUploadView();
would that allow you to know whether the file was a new upload (safe to delete) or an existing file (leave it alone)?
Assuming that you want to pick a file from your own Google Drive and move it to the Blobstore.
1)First you have to perform Oauth for Google Drive API
2)Using the picker when you select a file from drive, you need to get it's id
3)Using the id obtained in step 2 you can programmatically download it using Drive API
4)After downloading the file you can use FileService(deprecated though) to upload the file to the
Blobstore.

How long to blob urls served by the app engine remain valid?

I was wondering if anyone knew how long image urls served back from the google app engine blob store remain valid for?
I have been tracking on url that i served an image from the blob store on 1/3/13 and its still there.
I am ask specifically so i can cache the image url instead of attempting to serve it repeatedly. If i did this i would still check if the image is there, but how often would i need to check that
thanks!
They remain valid until either you
a. call delete_serving_url, or
b. delete the underling blob.

Resources