2

我希望能够在 node.js 客户端应用程序中使用gcs-resumable-upload包以及签名 url进行可恢复上传到谷歌云存储(因为客户端应用程序是由未经身份验证的用户调用的)。

我的服务器通过调用getSignedUrl生成一个签名的 url {action: 'resumable'}。然后,服务器向带有标头{ 'x-goog-resumable': 'start' }和空正文的签名 url 发送 POST,并接收带有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执行可恢复上传,如果可以,具体如何?如果有人有一个例子,那将不胜感激!

4

2 回答 2

2

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

根据 google doc here 很清楚如何创建签名 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 调用以上传对象。有关在中断时使用哪些标头的更多信息,请参阅链接

于 2020-09-08T07:43:20.387 回答
-1

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

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

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

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

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

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

于 2019-08-14T22:26:21.930 回答