gpt4 book ai didi

rest - 通过HTTP将Flutter可恢复上传到Google云端硬盘

转载 作者:行者123 更新时间:2023-12-03 04:24:30 29 4
gpt4 key购买 nike

根据Google Drive API上的文档,我正在尝试将文件上传到Google云端硬盘的根文件夹。我已经通过Google登录对用户进行了身份验证,这不是问题。我不断从服务器返回411错误,提示“

"POST requests require a Content-length header. That’s all we know.".



我有一个Content-length header ,但似乎不被接受。这是我的代码,
Uri uri = Uri.parse('https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable');

http.MultipartRequest request = new http.MultipartRequest('POST', uri);
request.headers["Authorization"] = header['Authorization'];
request.headers['content-type'] = "application/json; charset=UTF-8";
request.headers['X-Upload-Content-Type'] ='video/mp4';
request.headers['X-Upload-Content-Length'] = lengthInBytes.toString();
request.headers['name'] = fileName;
request.headers['content-length'] = (request.contentLength).toString();
//request.files.add(await http.MultipartFile.fromPath('$fileName', file.path,));
print("request.toString: " + request.toString());
http.StreamedResponse response = await request.send();
print('ok: ' + response.statusCode.toString());
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});

我所知道的唯一对我来说是fileName,因为API站点上的文档略有不同,并且我不确定是否对其进行了正确编码。这是Google网站上的API示例,
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
"name": "myObject"
}

我可以对大约5MB的文件进行分段上传,但是我需要能够上传较大的文件,并且只能使用可恢复文件。如果需要,我可以发布多部分代码。

最佳答案

我通过使用http StreamedRequest类解决了该问题。以下发布的代码可与Google Drive V3一起上传mp4视频。

Future handleUploadData(Map headers, String filename, String path) async {
final file = new File(path);
final fileLength = file.lengthSync().toString();
String sessionUri;

Uri uri = Uri.parse('https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable');

String body = json.encode({ 'name' : filename });

final initialStreamedRequest =
new http.StreamedRequest('POST', uri)
..headers.addAll({
'Authorization': headers['Authorization'],
'Content-Length' : utf8.encode(body).length.toString(),
'Content-Type' : 'application/json; charset=UTF-8',
'X-Upload-Content-Type' : 'video/mp4',
'X-Upload-Content-Length' : fileLength
});

initialStreamedRequest.sink.add(utf8.encode(body));
initialStreamedRequest.sink.close();

http.StreamedResponse response = await initialStreamedRequest.send();
print("response: " + response.statusCode.toString());
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});

if (response.statusCode == 200) {
sessionUri = response.headers['location'];
print(sessionUri);
}

Uri sessionURI = Uri.parse(sessionUri);
final fileStreamedRequest =
new http.StreamedRequest('PUT', sessionURI)
..headers.addAll({
'Content-Length' : fileLength,
'Content-Type' : 'video/mp4',
});
fileStreamedRequest.sink.add(file.readAsBytesSync());
fileStreamedRequest.sink.close();

http.StreamedResponse fileResponse = await fileStreamedRequest.send();
print("file response: " + fileResponse.statusCode.toString());
fileResponse.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}

最初的StreamRequest向GDrive发送请求,其中包含有关将要上传的文件的元数据,并接收位置URI,该位置URI在第二个文件StreamRequest中用于上载实际的文件数据。目前,这是通过一个上载操作完成的,但可以分为多个块。

关于rest - 通过HTTP将Flutter可恢复上传到Google云端硬盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60423620/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com