如何使用rest api在Google云端存储中下载文件

问题描述 投票:1回答:2

场景:存储桶中存在多个与dcm API相关的文件夹和许多文件(点击,展示,每日聚合文件等)。 https://console.cloud.google.com/storage/browser/dcmaccountno

是否可以使用rest api下载文件,目前我有服务帐户和私钥。我们没有太多暴露于goog云存储,因此任何小的帮助将是非常可观的。

感谢您的任何帮助!

google-cloud-storage google-apps
2个回答
2
投票

使用rest API,您可以按照我在下面提到的链接中执行的以下方式从Google存储中下载/上传文件:

参考:https://stackoverflow.com/a/53955058/4345389

您可以通过以下方式使用DownloadData方法代替WebClient的UploadData方法:

// 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
}

根据您的要求做一些调整。


1
投票

您可以调用两个REST API中的任何一个:JSON或XML。在任何情况下,您都需要从documentation中详细说明的OAuth 2.0获取授权访问令牌,然后将cURL与GET对象请求一起使用:

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]"

请注意,对于多个文件,您必须对请求进行编程,因此如果您想轻松下载存储桶或子目录中的所有对象,最好使用gsutil

© www.soinside.com 2019 - 2024. All rights reserved.