2016-09-26 37 views
0

我試圖編寫一個函數來下載存儲在持久存儲桶中的文件,並且我在解碼結果時遇到了一些問題。從桶中下載對象

我下面的指導here嘗試下載這裏顯示的對象:

(int) 3 => object(stdClass) { 
     bucketKey => 'my-persistent-bucket' 
     objectKey => '11--test.dwg' 
     objectId => 'urn:adsk.objects:os.object:my-persistent-bucket/11--test.dwg' 
     sha1 => '477085439a60779064d91fd1971d53c77c7a163a' 
     size => (int) 188600 
     location => 'https://developer.api.autodesk.com/oss/v2/buckets/my-persistent-bucket/objects/11--test.dwg' 
    } 

使用下面的捲曲功能

$ch = curl_init(); 
    $headers = [ 
     "Authorization: Bearer " . $token->token, 
     "Accept: application/octet-stream" 
    ]; 

    $url = 'https://developer.api.autodesk.com/oss/v2/buckets/'.$this->persistent.'/objects/'.rawurlencode($file->object_key); 

    curl_setopt_array($ch, [ 
     CURLOPT_HTTPHEADER => $headers, 
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_URL => $url 
    ]); 

    $response = curl_exec($ch) ; 
    $http_header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE) ; 
    $http_header = substr($response, 0, $http_header_size) ; 
    $http_body = substr($response, $http_header_size) ; 
    $response_info = curl_getinfo($ch) ; 
    curl_close($ch) ; 

curl_getinfo($ch);看起來一切順利罰款:

'url' => 'https://developer.api.autodesk.com/oss/v2/buckets/my-persistent-bucket/objects/11--test.dwg', 
'content_type' => 'application/x-www-form-urlencoded', 
'http_code' => (int) 200, 
'header_size' => (int) 474, 
'request_size' => (int) 199, 
'filetime' => (int) -1, 
'ssl_verify_result' => (int) 0, 
'redirect_count' => (int) 0, 
'total_time' => (float) 1.261261, 
'namelookup_time' => (float) 0.029048, 
'connect_time' => (float) 0.057444, 
'pretransfer_time' => (float) 0.119675, 
'size_upload' => (float) 0, 
'size_download' => (float) 188600, 
'speed_download' => (float) 149532, 
'speed_upload' => (float) 0, 
'download_content_length' => (float) 188600, 
'upload_content_length' => (float) 0, 
'starttransfer_time' => (float) 0.902231, 
'redirect_time' => (float) 0, 
'redirect_url' => '', 
'primary_ip' => '52.210.137.76', 
'certinfo' => [], 
'primary_port' => (int) 443, 
'local_ip' => '10.0.2.15', 
'local_port' => (int) 50564 

$http_body = '%C8B%BB%8B%A6%12%03Z%7D%29%E7%27%1F%5D%D4%CB%FC%DA%15G%3B%13%0D%89%0A%1C%DB%AE2%2C%9AP%EE%60x6%FD%92I2%F6%DE%7DI%DC%A0O%14%F2%84%9Ed%D0k%C40%B7%3E%3B%A1%22%...

的響應總是看起來像一個URL編碼的字符串,但無論我如何努力對其進行解碼,我不能設法得到一個工作文件,到目前爲止,我已經試過:

curl_unescape() urldecode() rawurldecode()

這些都沒有給我一個可用的文件。值得注意的是,我可以從A360下載文件沒有問題,但我沒有設法從Forge存儲桶中獲得一個文件。

任何關於我做錯的想法都會很棒。

由於

回答

0

的bucketKey應匹配該正則表達式 '^ [-_。一個-Z0-9] {3128} $',並且objectKey應該URL編碼。但是,用於上傳文件/對象的密鑰必須與用於下載的密鑰相匹配。這意味着如果您選擇單向編碼字符,則應在稍後以相同的方式進行編碼。

我也注意到你沒有指定如何編碼下載。如果你沒有指定下載爲八位字節,HTTP將使用默認的UTF8文本字符與二進制。您確實指定了CURLOPT_RETURNTRANSFER,該CURLOPT_RETURNTRANSFER將轉換爲二進制文件,但缺少服務器部分的標題。

下面的代碼適用於我,假設bucketKey和objectKey是使用rawurlencode()編碼的URL;以及何時使用PUT上傳文件/對象。

``` 
$ch =curl_init() ; 
$headers =[ 
    "Authorization: Bearer {$token->token}", 
    "Accept: application/octet-stream", 
    "Content-Type: application/json" 
] ; 
$url ="https://developer.api.autodesk.com/oss/v2/buckets/{$bucket_key}/objects/{$object_key}" ; 
curl_setopt_array($ch, [ 
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => $url 
]) ; 

$response =curl_exec ($ch) ; 
$http_header_size =curl_getinfo ($curl, CURLINFO_HEADER_SIZE) ; 
$http_header =substr ($response, 0, $http_header_size) ; 
$http_body =substr ($response, $http_header_size) ; 
$response_info =curl_getinfo ($ch) ; 
curl_close ($ch) ; 

file_put_contents ('11--test.dwg', $http_body) ; 

```

正如你可以看到我分裂響應分爲兩個部分。標題和八位字節流。文件下載位於第二部分,是HTTP身體響應。響應包含頭文件和正文,因此您需要在結尾處對其進行拆分。

希望有幫助,

+0

感謝您的幫助@cyrille,但我仍然沒有太多的運氣。我注意到你沒有解碼你的'$ http_body'我認爲這是因爲你使用了'octet-stream'。我在頭文件中設置了「Accept:application/octet-stream」,但'curl_getinfo($ ch)'顯示''content_type'=>'application/x-www-form-urlencoded','被髮送。這可能是我無法將響應解碼爲有用的東西的原因嗎?我將編輯我的問題以包含您的建議。謝謝 –