我希望能够使用gcs-resumable-upload软件包和signed urls一起在node.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来执行可恢复的上传吗?如果有人举个例子,将不胜感激!
答案 0 :(得分:1)
如果您使用的是Java,那么
根据Google文档here 很明显,如何创建一个签名的URL将对象上传到存储桶。
但是您需要为可恢复的上传添加一个额外的标头(“ x-goog-resumable:开始”)。在文档中没有提到。
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 :(得分:0)
根据此post,有可能。
客户端请求签名,以便可以执行PUT
您的服务器确实会创建并返回签名的URL。
您的服务器发出POST请求以启动可恢复的上传
您的服务器将URL和上载ID都返回给客户端
客户端使用提供的URL和上载ID进行一个或多个PUT