如何使用带有签名网址的 gcs-resumable-upload



我希望能够使用 gcs-resumable-upload 包以及签名网址,在节点.js客户端应用程序中对 Google Cloud Storage 进行断点续传上传(因为客户端应用是由未经身份验证的用户调用的(。

我的服务器通过使用 {action: 'resumable'} 调用 getSignedUrl 来生成签名 URL。然后,服务器将 POST 发送到带有标头 { 'x-goog-resumable': 'start' } 和空正文的签名 URL,并接收带有如下所示的 location 标头的响应:

https://storage.googleapis.com/<bucket_name/<file_path>?GoogleAccessId=<service_account>&Expires=<expiry_time>&Signature=<signature>&upload_id=<upload_id>

我的问题是:如果我将上述location标头返回给我的客户端,客户端是否可以使用它使用 gcs-resumable-upload 执行可恢复上传,如果是这样,究竟如何?如果有人有例子,将不胜感激!

如果你使用的是java,那么。

根据谷歌文档 这里如何创建签名 URL 以将对象上传到存储桶一目了然。

但是你需要为可恢复上传添加一个额外的标头("x-goog-resumable:start"(。 文档中没有提到。

import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.*;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class GenerateV4PutObjectSignedUrl {
    public static void generateV4GPutObjectSignedUrl(
            String projectId, String bucketName, String objectName) throws StorageException, IOException {
        File initialFile = new File("src/main/java/credentials.json");
        InputStream serviceAccountJson = new FileInputStream(initialFile);
        ServiceAccountCredentials credentials = (ServiceAccountCredentials)
                GoogleCredentials.fromStream(serviceAccountJson);
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).setCredentials(credentials).build().getService();
        // Define Resource
        BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();
        // Generate Signed URL
        Map<String, String> header = new HashMap<>();
        header.put("x-goog-resumable", "start");
        URL url =
                storage.signUrl(
                        blobInfo,
                        15,
                        TimeUnit.MINUTES,
                        Storage.SignUrlOption.httpMethod(HttpMethod.POST),
                        Storage.SignUrlOption.withExtHeaders(header),
                        Storage.SignUrlOption.withV4Signature());

        System.out.println("Generated PUT signed URL:");
        System.out.println(url);
    }
    public static void main(String[] args) throws IOException {
        generateV4GPutObjectSignedUrl("projectId", "bucketName", "objectName");
    }
}
Use the generated URL to make a POST call. I used cURL for the request.
The response would something like this.
Host: storage.googleapis.com
> User-Agent: curl/7.54.0
> Accept: */*
> x-goog-resumable: start
>
< HTTP/2 201
< content-type: text/plain; charset=utf-8
< x-guploader-uploadid: some-id
< location: session URL for actual resumable upload
< content-length: 0
< date: Mon, 07 Sep 2020 12:18:00 GMT
< server: UploadServer

现在使用该位置进行 PUT 调用以上传对象。有关中断时使用哪些标头的详细信息,请参阅 link

根据这篇文章,这是可能的。

  1. 客户端请求签名,以便它可以执行 PUT

  2. 您的服务器会创建并返回签名的 URL。

  3. 您的服务器发出 POST 请求以启动可恢复上传

  4. 您的服务器将 URL 和上传 ID 同时返回给客户端

  5. 客户端使用提供的 URL 和上传 ID 执行一个或多个 PUT。

最新更新