我正在尝试使用预先签名的url将视频文件上传到GCS。我已经设法通过谷歌创建了url,但现在我在使用它时遇到了问题。
在邮递员中上传作品,得到200响应。

从邮递员复制的代码结果为403禁止(SignatureDoesNotMatch):
Future<http.StreamedResponse> uploadVideo(
{required String uploadURL, required String filePath}) async {
var headers = {'Content-Type': 'application/octet-stream'};
var request = http.MultipartRequest('PUT', Uri.parse(uploadURL));
request.files.add(await http.MultipartFile.fromPath('file', filePath));
request.headers.addAll(headers);
http.StreamedResponse response = await request.send();
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
} else {
print(response.reasonPhrase);
}
return response;
}这是我从Google得到的错误:
<?xml version='1.0' encoding='UTF-8'?><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message><StringToSign>GOOG4-RSA-SHA256
20210803T082850Z
20210803/auto/storage/goog4_request
6d513846a3db49f949b0d2eea8f04b90f918b3b94588c3ed55ed3620b7d7e1f6</StringToSign><CanonicalRequest>PUT
/phonedo-interviews/app-test/007/2.mp4
X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=interviews%40interviews-317011.iam.gserviceaccount.com%2F20210803%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20210803T082850Z&X-Goog-Expires=900&X-Goog-SignedHeaders=content-type%3Bhost
content-type:multipart/form-data; boundary=dart-http-boundary-6w1yq6BQN3EkGBrhHZnwidOXZsBecsgSwTT3nBjB9vQCToHt0cg
host:storage.googleapis.com
content-type;host
UNSIGNED-PAYLOAD</CanonicalRequest></Error>注意:我需要Content-Type为application/octet-stream,所以我在Postman的自动标头中禁用了该标头,并手动添加了Content-Type。当我没有这样做的时候,我也得到了403。
发布于 2021-08-08 13:29:59
解决方案是以二进制格式发送文件。
以下是工作代码:
Future<http.Response> uploadVideo(
{required String uploadURL, required String filePath}) async {
var response = await http.put(
Uri.parse(uploadURL),
headers: {'content-type': 'application/octet-stream'},
body: File(filePath).readAsBytesSync(),
);发布于 2021-08-03 08:31:01
在您的邮递员标头中,一个Token被提供给GCS (第一行)。考虑到您需要授权,Postman可能会将此令牌保存在应用程序方面的某个位置。
在这段flutter代码中,您给出的报头不包括Auth令牌,因此您将收到一个403错误。
https://stackoverflow.com/questions/68632515
复制相似问题