Downloading file with service account using signed

2019-08-09 07:40发布

问题:

My Requirement is to generate signed Url for drive uploaded document which will be shared with clients so that they can able to download files form drive without any authentication.

As newbie to use drive API for first time, I am using .NET libraries to call drive API. I have setup the service account for Google API as mentioned in the document. I am able to make API call using service account and able to upload the files. I reffered to How to sign url in .net for google cloud storage to create the sign url. but after accessing the mentioned url receiving following error:

 <Error> <Code>InvalidBucketName</Code> <Message>The specified bucket
 is not valid.</Message> </Error>

I am not sure about GoogleBucketName and GoogleBucketDir. Can anybody tell me where i would get GoogleBucketName & GoogleBucketDir. Currently i am setting GoogleBucketName to Prject Name i created in google developer console https://console.developers.google.com/ and GoogleBucketDir as empty string. Thanks in advance.

回答1:

After you upload a file you are returned a file resource.

File resource contains a field called alternatelink

alternateLink
string
A link for opening the file in a relevant Google editor or viewer.

You can share this link with other users they will be able to open the file in drive they should also be able to download it from drive.

There is another field called downloadUrl which might let them download it but I haven't tested that one personal. If I remember correctly downloadUrl was null sometimes so I didn't use it.

Console.WriteLine("Alternate link: " + item.AlternateLink);  

Google Drive API Link to file Tutorial