3

Scenario: there are multiple folders and many files stored in storage bucket related to dcm API.(click,impression,daily aggregate files etc). https://console.cloud.google.com/storage/browser/dcmaccountno

Is it possible to download files using rest api and currently i have service account and private key. we dont have much exposure towards goog cloud storage hence any small help would be really appreciable.

Thank you for any help!

Naveen
  • 115
  • 3
  • 13

2 Answers2

2

You can do calls to whichever of the two REST APIs: JSON or XML. In any case, you will need to get an authorization access token from OAuth 2.0 as detailed in the documentation and then use cURL with a GET Object Request:

JSON API:

curl -X GET \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -o "[SAVE_TO_LOCATION]" \
    "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]/o/[OBJECT_NAME]?alt=media"

XML API:

curl -X GET \
    -H "Authorization: Bearer [OAUTH2_TOKEN]" \
    -o "[SAVE_TO_LOCATION]" \
    "https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]"

Note that for multiple files, you will have to program the requests, so if you want to easily download all the objects in a bucket or subdirectory, it's better to use gsutil instead.

Héctor Neri
  • 1,384
  • 9
  • 13
  • Thanks Neri i went through this doc however have a doubt on [OAUTH2_TOKEN]. – Naveen Aug 20 '18 at 15:13
  • The OAuth2_token is the one you get following the [documentation to authorize requests](https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing) and if you want to understand better the process to OAuth 2.0 and adjust it to your use case, then check the [documentation about using OAuth 2.0 fo access Google APIs](https://developers.google.com/identity/protocols/OAuth2). OAuth 2.0 is recommended, but if you want to handle the security of the keys by yourself, you can use API keys. There are different options of [authentication](https://cloud.google.com/storage/docs/authentication). – Héctor Neri Aug 20 '18 at 15:35
  • This helped me! Thanks. I got the [OUTH2_TOKEN] by running "gcloud auth print-access-token" on my ubuntu. – Chethan May 09 '19 at 21:23
2

Using rest API you can download/upload files from google storage in the following way that I already did in the below-mentioned link:

Reference: https://stackoverflow.com/a/53955058/4345389

Instead of UploadData method of WebClient, you can use DownloadData method in the following way:

// Create a new WebClient instance.
using (WebClient client= new WebClient())
{
  client.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + bearerToken);
  client.Headers.Add(HttpRequestHeader.ContentType, "application/octet-stream");
// Download the Web resource and save it into a data buffer.
byte[] bytes = client.DownloadData(body.SourceUrl);
MemoryStream memoryStream = new MemoryStream(bytes);
// TODO write further funcitonality to write the memorystream
}

Do some tweaks as per your requirements.

Jayoti Parkash
  • 868
  • 11
  • 26