How to download files in Google Cloud Storage using rest api

允我心安 提交于 2019-12-12 16:59:07

问题


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!


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/51932534/how-to-download-files-in-google-cloud-storage-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!