【问题标题】:How to use gcs-resumable-upload with signed url如何使用带有签名网址的 gcs-resumable-upload
【发布时间】:2019-08-17 07:09:10
【问题描述】:

我希望能够在 node.js 客户端应用程序中使用gcs-resumable-upload 包和signed urls 一起执行可恢复上传到 Google Cloud Storage(因为客户端应用程序是由未经身份验证的用户调用的)。

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

【问题讨论】:

    标签: google-cloud-storage google-api-nodejs-client resume-upload


    【解决方案1】:

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

    根据谷歌文档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 调用以上传对象。有关在中断时使用哪些标头的更多信息,请参阅link

    【讨论】:

      【解决方案2】:

      根据这个post,是可以的。

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

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

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

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

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

      【讨论】:

      • 这没有使用gcs-resumable-upload
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 2020-09-18
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多