Google Cloud Storage - 从带有代号的签名网址获取下载链接

问题描述

我使用 Google Cloud Storage API。我像这样创建签名网址;

 var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer("username@iam.gserviceaccount.com").FromPrivateKey(PrivateKey));
        var urlSigner = UrlSigner.FromServiceAccountCredential(credential);
        string url = urlSigner.Sign(bucketName,"Sample.txt",TimeSpan.FromDays(7));

        var httpClient = new System.Net.Http.HttpClient();

        System.Net.Http.HttpResponseMessage response  = await httpClient.GetAsync(url);
        var content = await response.Content.ReadAsByteArrayAsync();

没关系。但我不能为版本使用第 1 代。

这是没有签名网址的正常下载; 我在签名网址上寻找“IfGenerationMatch =”

 await client.DownloadobjectAsync(
             bucket: bucketName,objectName: sourcePath,destination: destinationPath,progress: progress,options: new DownloadobjectOptions()
             { ChunkSize = 1048576,Range = rangeHeaderValue,IfGenerationMatch = data.GenerationNo }
         );

解决方法

请注意,正如前面提到的 here,签名网址只能通过 XML API 端点访问 Cloud Storage 中的资源。

IfGenerationMatch 前提条件仅出现在 JSON API 中,因此您不会使用签名 URL 找到它。您需要使用 x-goog-if-generation-match 前置条件,因为它与 XML API 中的等价。