【问题标题】:Getting 404 when trying to get an object from GCS using service account尝试使用服务帐户从 GCS 获取对象时出现 404
【发布时间】:2020-03-08 03:52:05
【问题描述】:

我正在编写 Google Apps 脚本代码以从 Google Cloud Storage 下载对象并返回 Blob。但是,在以下调用中运行 getGcsObjectBlobTest() 函数会给我一个带有“未找到”正文的 404 响应代码。

我正在使用带有 JSON 密钥的服务帐户进行身份验证。服务帐户具有项目的存储对象查看者角色和存储桶的存储对象查看者角色/创建者角色。我没有设置任何相信 the official documentation 的 ACL,它声明“用户只需要来自 Cloud IAM 或 ACL 的权限即可访问存储桶或对象”。

有趣的是,如果我在从 Google Cloud Console 上的对象详细信息页面获得的代码中将 URL 切换为“Link #2”,我会收到带有 html 正文的 200 响应。所以我想我的 IAM 至少部分工作。

谁能告诉我这是怎么回事?

function getGcsObjectBlobTest(){
  var bucketName = 'vision_api_head_count';
  var objectName = 'Sample/Sample_01.jpg';

  getGcsObjectBlob(bucketName, objectName);
}


/*
 * Get Blob for a GCS object
 *
 * @param {string} bucketName - bucket name for the object
 * @param {string} objectName - name for the object
 * @returns Blob
 */
function getGcsObjectBlob(bucketName, objectName){
  var googleAppCredentials = getGoogleCredentials();
  var scope = 'https://www.googleapis.com/auth/devstorage.read_only';
  var token = getOAuth2TokenForGCP('annotatePrivateGcsImage', googleAppCredentials, scope);

  // Link #1: This URL is compliant with the official documentation. (https://cloud.google.com/storage/docs/json_api/v1/objects/get)
  var url = "https://storage.googleapis.com/storage/v1/b/" + bucketName + "/o/" + objectName + "?alt=media";

  // Link #2: This is "Link URL" from Google Cloud Console.
  // var url = "https://storage.cloud.google.com/" + bucketName + "/" + objectName + "?alt=media&orgonly=true&supportedpurview=organizationId";

  console.log(url);

  var options = {
    'method' : 'GET',
    'headers' : {
      'Authorization': 'Bearer ' + token,
    }
  }

  try{
    var blob = urlfetch(url, options, true).getBlob();
    console.log(blob.getName());
    console.log(blob.getContentType());
    console.log(blob.getBytes().length + " Bytes");

    return blob;
  }catch(e){
    throw new Error(e);
  }
}


/* Get OAuth2 token for specified credentials and scope
 * 
 * @requires {@link https://github.com/gsuitedevs/apps-script-oauth2|OAuth2 for Apps Script}
 * @param {string} serviceNamePrefix - prefix for the service name
 * @param {string} googleAppCredentials - JSON secret key for an account
 * @param {string} scope - {@link https://developers.google.com/identity/protocols/googlescopes|OAuth 2.0 Scopes for Google APIs}
 * @returns {string} OAuth 2.0 access token
 */
function getOAuth2TokenForGCP(serviceNamePrefix, googleAppCredentials, scope){
  var creds = JSON.parse(googleAppCredentials);

  var service = OAuth2.createService(serviceNamePrefix + "_" + creds.client_email)
      .setTokenUrl(creds.token_uri)
      .setPrivateKey(creds.private_key)
      .setIssuer(creds.client_email)
      .setSubject(creds.client_email)
      .setPropertyStore(PropertiesService.getScriptProperties())
      .setCache(CacheService.getScriptCache())
      .setLock(LockService.getScriptLock())
      .setScope(scope);

  if(service.hasAccess()){
    return service.getAccessToken();
  }else{
    console.error(service.getLastError());
    throw new Error('Could not get OAuth 2.0 access token!!');
  }
}

【问题讨论】:

    标签: google-apps-script google-cloud-platform google-cloud-storage


    【解决方案1】:

    您使用了错误的端点,这就是您收到 404(未找到错误)的原因。您正在使用名为“Authenticated Browser Downloads”的方法,但您调用的端点是 JSON API。

    在 Google Cloud Console 中查找网址。

    网址看起来像这样,但有几个变化:

    https://storage.cloud.google.com/BUCKET_NAME/OBJECT_NAME
    

    Google Cloud Storage Request Endpoints

    Authenticated Browser Downloads

    【讨论】:

    • 我试图构建一个服务器端应用程序来下载一个对象,所以 JSON API 是我想要的。感谢您为回答我的问题所做的努力!
    • @t.toda - 但是,我回答了你为什么会得到 404 的问题。
    【解决方案2】:

    我刚刚通过将encodeURIComponent() 应用于其中包含“/”的对象名称来解决了这个问题。他们应该在他们的文件中提到这一点

    var url = "https://storage.googleapis.com/storage/v1/b/" + bucketName + "/o/" + encodeURIComponent(objectName) + "?alt=media";
    

    编辑:错字

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2021-01-04
      • 2017-07-29
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多