Google cloud storage 如何使用带有签名url的gcs可恢复上载
我希望能够在node.js客户机应用程序中使用该软件包,与(因为客户机应用程序由未经身份验证的用户调用)一起进行可恢复的上传到Google云存储 我的服务器通过调用Google cloud storage 如何使用带有签名url的gcs可恢复上载,google-cloud-storage,google-api-nodejs-client,resume-upload,Google Cloud Storage,Google Api Nodejs Client,Resume Upload,我希望能够在node.js客户机应用程序中使用该软件包,与(因为客户机应用程序由未经身份验证的用户调用)一起进行可恢复的上传到Google云存储 我的服务器通过调用{action:'resumable'}生成一个签名url。然后,服务器向签名的url发送一篇文章,标题为{'x-goog-resumable':'start'},正文为空,并接收一个带有位置标题的响应,标题如下所示: https://storage.googleapis.com/<bucket_name/<file_pa
{action:'resumable'}
生成一个签名url。然后,服务器向签名的url发送一篇文章,标题为{'x-goog-resumable':'start'}
,正文为空,并接收一个带有位置
标题的响应,标题如下所示:
https://storage.googleapis.com/<bucket_name/<file_path>?GoogleAccessId=<service_account>&Expires=<expiry_time>&Signature=<signature>&upload_id=<upload_id>
https://storage.googleapis.com/根据这一点,这是可能的
客户端请求签名,以便执行PUT
您的服务器创建并返回一个签名的URL
服务器发出POST请求以启动可恢复上载
服务器将URL和上载ID返回给客户端
客户端使用提供的URL和上载ID执行一个或多个PUT
如果您使用的是java,那么
根据谷歌文档
很明显,如何创建一个签名URL来将对象上传到bucket中
但是您需要为可恢复上传添加一个额外的头(“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");
}
}
import com.google.auth.oauth2.GoogleCredentials;
导入com.google.auth.oauth2.ServiceAccountCredentials;
导入com.google.cloud.storage。*;
导入java.io.*;
导入java.net.URL;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.concurrent.TimeUnit;
公共类GenerateV4PutObjectSignedUrl{
公共静态void generateV4GPutObjectSignedUrl(
字符串projectId、字符串bucketName、字符串objectName)引发StorageException、IOException{
File initialFile=new文件(“src/main/java/credentials.json”);
InputStream serviceAccountJson=新文件InputStream(初始文件);
ServiceAccountCredentials凭据=(ServiceAccountCredentials)
GoogleCredentials.fromStream(serviceAccountJson);
Storage Storage=StorageOptions.newBuilder().setProjectId(projectId).setCredentials(credentials.build().getService();
//定义资源
BlobInfo BlobInfo=BlobInfo.newBuilder(BlobId.of(bucketName,objectName)).build();
//生成签名URL
Map header=newhashmap();
标题。放置(“x-goog-可恢复”、“启动”);
网址=
storage.signUrl(
布卢宾福,
15,
时间单位,分钟,
Storage.signurlpoption.httpMethod(httpMethod.POST),
Storage.SignUrlOption.withExtHeaders(标头),
Storage.signurlpoption.withV4Signature());
System.out.println(“生成的PUT签名URL:”);
System.out.println(url);
}
公共静态void main(字符串[]args)引发IOException{
generateV4GPutObjectSignedUrl(“projectId”、“bucketName”、“objectName”);
}
}
使用生成的URL进行POST调用。我用cURL来表示请求。
答案是这样的。
主持人:storage.googleapis.com
>用户代理:curl/7.54.0
>接受:*/*
>x-goog-可恢复:启动
>
现在使用位置进行PUT调用以上载对象。有关中断时使用哪些标头的更多信息,请参见这不是使用gcs可恢复上传
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