3
votes

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!

2

2 Answers

2
votes

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.

2
votes

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.