0

这是 PHP 中的库代码,用于使用 laravel 框架在 dropbox-sdk(file-Client.php) 中上传文件。

我会在我的保管箱控制器中调用以下函数。

function uploadFile($path, $writeMode, $inStream, $numBytes = null)
{
    Path::checkArgNonRoot("path", $path);
    WriteMode::checkArg("writeMode", $writeMode);
    Checker::argResource("inStream", $inStream);
    Checker::argNatOrNull("numBytes", $numBytes);

    // If we don't know how many bytes are coming, we have to use chunked upload.
    // If $numBytes is large, we elect to use chunked upload.
    // In all other cases, use regular upload.
    if ($numBytes === null || $numBytes > self::$AUTO_CHUNKED_UPLOAD_THRESHOLD) {
        $metadata = $this->_uploadFileChunked($path, $writeMode, $inStream, $numBytes,
                                              self::$DEFAULT_CHUNK_SIZE);
    } else {
        $metadata = $this->_uploadFile($path, $writeMode,
            function(Curl $curl) use ($inStream, $numBytes) {
                $curl->set(CURLOPT_POST, true);
                $curl->set(CURLOPT_INFILE, $inStream);
                $curl->set(CURLOPT_INFILESIZE, $numBytes);
            });
    }

    return $metadata;
}

反过来使用此功能。

 private function _uploadFile($path, $writeMode, $curlConfigClosure)
  {
    Path::checkArg("path", $path);
    WriteMode::checkArg("writeMode", $writeMode);
    Checker::argCallable("curlConfigClosure", $curlConfigClosure);

    $url = $this->buildUrlForGetOrPut(
        $this->contentHost,
        $this->appendFilePath("2/files/upload", $path),
        $writeMode->getExtraParams());

    $curl = $this->mkCurl($url);

    $curlConfigClosure($curl);

    $curl->set(CURLOPT_RETURNTRANSFER, true);
    $response = $curl->exec();

    if ($response->statusCode !== 200) throw RequestUtil::unexpectedStatus($response);

    return RequestUtil::parseResponseJson($response->body);
}

我最近将网址从 V1 升级到 V2。我现在收到此错误,无法找到此错误的根本原因。

在此处输入图像描述

我什至尝试对保管箱 url 进行硬编码并将其放入 curl 请求中,但没有用。任何建议、帮助甚至引导都是救命稻草。

4

1 回答 1

0

Dropbox API v2 不是 API v1 的直接替代品,因此您不能只换入新的 v2 API 端点 URL。如果您尝试更新 API v1 库以与 API v2 一起使用,则需要对代码进行大量更改。

例如,在您共享的示例中,文件上传的所需路径是直接在 v1 端点的 URL 路径上提供的,但这不适用于 API v2,这就是您收到该错误的原因。

在 API v2 中,路径将在Dropbox-API-Arg请求标头中作为 JSON 值提供。您可以在文档中找到有关如何格式化 API v2 请求的信息:

https://www.dropbox.com/developers/documentation/http/documentation#files-upload

使用已经为 Dropbox API v2 构建的新库可能比尝试更新旧库更容易。没有官方的 Dropbox API v2 PHP SDK,但这里列出了一些使用 PHP 的 API v2 的社区库:

https://www.dropbox.com/developers/documentation/communitysdks

于 2018-02-08T17:12:25.743 回答