如果您使用的是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 调用以上传对象。有关在中断时使用哪些标头的更多信息,请参阅链接