Dropbox v2 API - 大文件上传 [英] Dropbox v2 API - large file uploads

查看:454
本文介绍了Dropbox v2 API - 大文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来自我对相同主题的上一个问题。简单的文件上传是,很简单

This question follows on from my previous question on the same subject. Simple file uploads are, well, simple.

 $headers = array("Authorization: Bearer dropbox_token",
                  'Content-Type: application/octet-stream',
                'Dropbox-API-Arg: {"path":"/path/file.ext",
                "mode":"add"}');

 $data = "I love Stackoverflow";
 $ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
 curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);

 curl_setopt($ch,CURLOPT_POST,true);
 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
 $response = curl_exec($ch);
 curl_close($ch);
 echo $response;

但是,当要保存的文件数据运行到10兆字节时,这不是一个可行的选项。我想我只需要使用新的(ish) PHP curl_file_create

However, this is not a viable option when the file data to be save run into 10s of megabytes. I thought I would simply use the new(ish) PHP curl_file_create.

$headers = array("Authorization: Bearer Dropbox token",
              'Content-Type: application/octet-stream',
              'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
                                 "mode":"add"}');

$ch = curl_init('https://content.dropboxapi.com/2/files/upload/');
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$args['file'] = curl_file_create('/path/to/bigfile.txt');

curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$args);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;

但是,这不起作用。 Dropbox API返回消息

However, this does not work. The Dropbox API returns the message


调用API函数files / upload时出错:Bad HTTPContent-Type应用程序/八位字节流; boundary = --...期望使用应用程序/八位字节流,text / plain; charset = dropbox-cors-hack

Error in call to API function "files/upload": Bad HTTP "Content-Type" header: "application/octet-stream; boundary=--... Expecting one of "application/octet-stream", "text/plain; charset=dropbox-cors-hack

我将非常感谢任何可以告诉我这里可能出错的人。 / p>

I would be most grateful to anyone who could tell me where I might be going wrong here.

推荐答案

看起来像 curl_file_create 这不是你想要的,当谈论到Dropbox API。

It looks like curl_file_create encodes a file as a multipart form upload, which isn't what you want when talking to the Dropbox API.

如果我理解正确,你试图解决的问题是,你不想将整个文件内容加载到内存中。是这样吗?

If I understand correctly, the issue you're trying to address is that you don't want to load the entire file contents into memory. Is that right?

如果是这样,请尝试以下方法。 (非常抱歉,我没有测试它,所以它可能包含错误。)

If so, please give the following a try. (Apologies that I haven't tested it at all, so it may contain mistakes.)

$headers = array('Authorization: Bearer Dropbox token',
                 'Content-Type: application/octet-stream',
                 'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
                                    "mode":"add"}');

$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);

$path = '/path/to/bigfile.txt';
$fp = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

curl_close($ch);
fclose($fp);

echo $response;

请注意,您可能还需要考虑 / files / upload_session_start '重新上传大文件。这可让您以块的形式上传,它支持大于150MB的文件( / files / upload 不支持)。

Note that you may also want to consider /files/upload_session_start, etc. if you're uploading large files. That lets you upload in chunks, and it supports files bigger than 150MB (which /files/upload does not).

这篇关于Dropbox v2 API - 大文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆