Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google api 使用Java在google云存储上使用*签名url*进行*可恢复上传*_Google Api_Google Cloud Platform_Google Cloud Storage - Fatal编程技术网

Google api 使用Java在google云存储上使用*签名url*进行*可恢复上传*

Google api 使用Java在google云存储上使用*签名url*进行*可恢复上传*,google-api,google-cloud-platform,google-cloud-storage,Google Api,Google Cloud Platform,Google Cloud Storage,根据关于如何在google cloud storage中创建对象的文档(请参阅中的“创建”方法),我们应该在尝试上载大型文件时使用blob.writer(…)方法,因为它可能会自动处理可恢复的上载。是这样吗 然而,如果我们希望在已签名的URL上进行可恢复的上传,那么在Java中如何实现?(非常感谢任何示例代码或指针;到目前为止,我的研究让我相信,使用精心创建的java库是不可能的,相反,在生成签名url后,需要使用java中的“PUT”和“POST”语句自定义构建自己的逻辑。这是目前为止最好的方

根据关于如何在google cloud storage中创建对象的文档(请参阅中的“创建”方法),我们应该在尝试上载大型文件时使用blob.writer(…)方法,因为它可能会自动处理可恢复的上载。是这样吗


然而,如果我们希望在已签名的URL上进行可恢复的上传,那么在Java中如何实现?(非常感谢任何示例代码或指针;到目前为止,我的研究让我相信,使用精心创建的java库是不可能的,相反,在生成签名url后,需要使用java中的“PUT”和“POST”语句自定义构建自己的逻辑。这是目前为止最好的方法吗?)

关于您的第一点,是的,
blob.writer(…)
方法会自动处理可恢复的上载。不幸的是,此方法不能从签名URL调用,只能直接从字节流上载文件

但是,正如您所提到的,可以使用其他方法从已签名的URL创建可恢复的上载,例如使用
PUT
方法似乎是一个不错的解决方法

我所做的是:

  • 使用“PUT”方法创建一个。您可以通过指定来实现,我在bucket中指定了一个具有所需权限的服务帐户

  • 用于将HTTP请求抛出到此签名URL。例如,我相信您不能直接使用curl命令,而UrlFetch API就可以做到这一点

  • 将uploadType=resumable头添加到
    urlFetch
    HTTP请求。请参见有关其工作原理的信息,以及其他参数和信息

  • 我将
    URLFetch
    配置为对已签名的URL进行异步调用,因为我相信上传大文件时会更方便

  • 要在应用程序引擎处理程序中使用的示例代码:

    package com.example.storage;
    
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
    import java.util.HashMap;
    import java.util.Map;
    import java.nio.charset.StandardCharsets;
    
    import java.net.URL;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    // Cloud Storage Imports
    import com.google.cloud.storage.Bucket;
    import com.google.cloud.storage.BucketInfo;
    import com.google.cloud.storage.Storage;
    import com.google.cloud.storage.StorageOptions;
    import com.google.cloud.storage.Blob;
    import com.google.cloud.storage.BlobId;
    import com.google.cloud.storage.BlobInfo;
    import com.google.cloud.storage.Storage.SignUrlOption;
    import com.google.auth.oauth2.ServiceAccountCredentials;
    import com.google.cloud.storage.HttpMethod;
    
    // Url Fetch imports
    import com.google.appengine.api.urlfetch.HTTPMethod;
    import com.google.appengine.api.urlfetch.HTTPRequest;
    import com.google.appengine.api.urlfetch.URLFetchService;
    import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
    import com.google.appengine.api.urlfetch.HTTPHeader;
    
    @WebServlet(name = "MainStorage", value = "/")
    public class MainStorage extends HttpServlet {
    
            @Override
            public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
                    // Bucket parameters 
                    String bucketName = "MY-BUCKET-NAME";
                    String blobName = "MY-BLOB-NAME";
                    String keyPath = "/PATH-TO-SERVICE-ACCOUNT-KEY/key.json";
    
                    BlobId blobId = BlobId.of(bucketName, blobName);
                    Storage storage = StorageOptions.getDefaultInstance().getService();
    
                    // Create signed URL with SignUrlOptions
                    URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS,
                                                    SignUrlOption.signWith(ServiceAccountCredentials.fromStream(new FileInputStream(keyPath))),
                                                    SignUrlOption.httpMethod(HttpMethod.PUT));
    
                    // Contents to upload to the Blob
                    String content = "My-File-contents";
    
                    // Build UrlFetch request
                    HTTPRequest upload_request = new HTTPRequest(signedUrl, HTTPMethod.PUT);
                    upload_request.setPayload(content.getBytes(StandardCharsets.UTF_8));
    
                    // Set request to have an uploadType=resumable
                    HTTPHeader set_resumable = new HTTPHeader("uploadType", "resumable");
                    upload_request.setHeader(set_resumable);
                    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
    
                    // Do an asynchronous call to the signed URL with the contents
                    fetcher.fetchAsync(upload_request);
    
                    // Return response to App Engine handler call
                    response.setContentType("text/plain");
                    response.getWriter().println("Hello Storage");
            }
    }
    

    这可能是一个更好的方法,但我相信它提供了一个如何制作此类应用程序的想法。

    关于您的第一点,是的,
    blob.writer(…)
    方法会自动处理可恢复的上传。不幸的是,此方法不能从签名URL调用,只能直接从字节流上载文件

    但是,正如您所提到的,可以使用其他方法从已签名的URL创建可恢复的上载,例如使用
    PUT
    方法似乎是一个不错的解决方法

    我所做的是:

  • 使用“PUT”方法创建一个。您可以通过指定来实现,我在bucket中指定了一个具有所需权限的服务帐户

  • 用于将HTTP请求抛出到此签名URL。例如,我相信您不能直接使用curl命令,而UrlFetch API就可以做到这一点

  • 将uploadType=resumable头添加到
    urlFetch
    HTTP请求。请参见有关其工作原理的信息,以及其他参数和信息

  • 我将
    URLFetch
    配置为对已签名的URL进行异步调用,因为我相信上传大文件时会更方便

  • 要在应用程序引擎处理程序中使用的示例代码:

    package com.example.storage;
    
    import java.io.IOException;
    import java.io.FileInputStream;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;
    import java.util.HashMap;
    import java.util.Map;
    import java.nio.charset.StandardCharsets;
    
    import java.net.URL;
    
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    // Cloud Storage Imports
    import com.google.cloud.storage.Bucket;
    import com.google.cloud.storage.BucketInfo;
    import com.google.cloud.storage.Storage;
    import com.google.cloud.storage.StorageOptions;
    import com.google.cloud.storage.Blob;
    import com.google.cloud.storage.BlobId;
    import com.google.cloud.storage.BlobInfo;
    import com.google.cloud.storage.Storage.SignUrlOption;
    import com.google.auth.oauth2.ServiceAccountCredentials;
    import com.google.cloud.storage.HttpMethod;
    
    // Url Fetch imports
    import com.google.appengine.api.urlfetch.HTTPMethod;
    import com.google.appengine.api.urlfetch.HTTPRequest;
    import com.google.appengine.api.urlfetch.URLFetchService;
    import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
    import com.google.appengine.api.urlfetch.HTTPHeader;
    
    @WebServlet(name = "MainStorage", value = "/")
    public class MainStorage extends HttpServlet {
    
            @Override
            public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
                    // Bucket parameters 
                    String bucketName = "MY-BUCKET-NAME";
                    String blobName = "MY-BLOB-NAME";
                    String keyPath = "/PATH-TO-SERVICE-ACCOUNT-KEY/key.json";
    
                    BlobId blobId = BlobId.of(bucketName, blobName);
                    Storage storage = StorageOptions.getDefaultInstance().getService();
    
                    // Create signed URL with SignUrlOptions
                    URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 14, TimeUnit.DAYS,
                                                    SignUrlOption.signWith(ServiceAccountCredentials.fromStream(new FileInputStream(keyPath))),
                                                    SignUrlOption.httpMethod(HttpMethod.PUT));
    
                    // Contents to upload to the Blob
                    String content = "My-File-contents";
    
                    // Build UrlFetch request
                    HTTPRequest upload_request = new HTTPRequest(signedUrl, HTTPMethod.PUT);
                    upload_request.setPayload(content.getBytes(StandardCharsets.UTF_8));
    
                    // Set request to have an uploadType=resumable
                    HTTPHeader set_resumable = new HTTPHeader("uploadType", "resumable");
                    upload_request.setHeader(set_resumable);
                    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();
    
                    // Do an asynchronous call to the signed URL with the contents
                    fetcher.fetchAsync(upload_request);
    
                    // Return response to App Engine handler call
                    response.setContentType("text/plain");
                    response.getWriter().println("Hello Storage");
            }
    }
    

    这可能是一种更好的方法,但我相信它提供了一个如何制作此类应用程序的想法。

    好的,因此如果internet连接暂时中断,上面的代码将“自动”处理它(由于设置了“可恢复性”)(另外,如果我没有使用GAE,我假定我会删除“获取程序”相反,只使用普通的HttpURLConnection?(如果是,HttpURLConnection将如何异步,因为我在javadocs中没有看到HttpURLConnection的任何此类方法)。不,前面的代码只是创建可恢复的会话请求。解释了如何检查会话是否被中断,以及如何恢复它(通过向之前创建的同一会话URL发送请求,200-201响应表示正确结束,308表示不完整)。关于使用哪个库来代替UrlFetch,任何REST java库都可以使用。我只使用了一个异步方法,因为App Engine中的处理程序有超时,这不是必需的。我在java中发现了处理异步HTTP调用的方法,其中可能有更多的示例,如果您希望用户使用符号URL进行curl:使用字符串of signedUrl.toExternalForm()好的,那么如果internet连接暂时中断,上面的代码将“自动”处理它(由于设置了“可恢复的”)(另外,如果我没有使用GAE,我假定我放弃“获取程序”,而只使用正常的HttpURLConnection?(如果是,HttpUrlConnection将如何异步,因为我在javadocs for HttpUrlConnection中没有看到任何这样的方法)。不,前面的代码只是创建了可恢复的会话请求。解释了如何检查会话是否被中断,以及如何恢复它(通过向之前创建的同一会话URL发送请求,200-201响应表示正确结束,308表示不完整)。关于使用哪个库来代替UrlFetch,任何REST java库都可以使用。我只使用了一个异步方法,因为App Engine中的处理程序有超时,这不是必需的。我在java中发现了处理异步HTTP调用的方法,其中可能有更多的示例,如果您希望用户使用符号URL进行curl:使用字符串of signedUrl.toExternalForm()