2016-09-26 4 views
0

영구 버킷에 저장된 파일을 다운로드하는 함수를 작성하려고하는데 결과를 해석하는 데 문제가 있습니다. 모든 것이 잘 갔다와 같은버킷에서 객체 다운로드

$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); 보이는 다음 컬 기능을 사용

(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' 
    } 

:

나는 여기에 표시된 개체를 시도하고 다운로드 가이드 here을 다음있어

'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에서 아무런 문제없이 파일을 다운로드 할 수는 있지만, 포지 버킷에서 파일을 가져올 수는 없습니다.

내가 잘못하고있는 것에 대한 아이디어는 훌륭합니다.

감사

답변

0

는 bucketKey는 '^ [-_. A-Z0-9] {3128} $'이 정규 표현식과 일치해야하고, objectKey는 URL 인코딩해야합니다. 그러나 파일/개체를 업로드하는 데 사용되는 키는 다운로드하는 데 사용 된 키와 일치해야합니다. 즉, 문자를 편도로 인코딩하려면 나중에 동일한 방식으로 인코딩해야합니다.

다운로드를 인코딩하는 방법을 지정하지 않은 것으로 나타났습니다. 옥텟으로 다운로드하도록 지정하지 않으면 HTTP는 기본값 인 UTF8 텍스트 문자 대 바이너리를 사용합니다. 바이너리로 변환 할 CURLOPT_RETURNTRANSFER를 지정했지만 서버 부분의 헤더가 누락되었습니다.

bucketKey 및 objectKey가 rawurlencode()로 인코딩 된 경우 아래 코드는 저에게 잘 돌아갑니다. 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이지만 아직 운이 좋지 않습니다. 당신이'octet-stream'을 사용했기 때문에'$ http_body '을 디코딩하지 않았다는 것을 알았습니다. curl_getinfo ($ ch)'는 'content_type'=> 'application/x-www-form-urlencoded'가 보내 졌음을 보여줍니다. 이것이 응답을 무언가로 해독 할 수 없다는 이유 때문일 수 있습니까? 내 질문을 편집하여 제안 사항을 포함 할 예정입니다. 감사 –

관련 문제