-1

I am following the tutorial on the following website: https://aps.autodesk.com/en/docs/data/v2/tutorials/upload-file/,, when i used the signed url to upload file .This step went wrong, I don't know where the problem is.(My app's client script code is php. I use the curl fountion to sent the request.)The upload step has a problem and only returns false. There is no other prompt. Then check the upload returns

enter image description here

This is test data:

      array (
              'jsonapi' => 
              array (
                'version' => '1.0',
              ),
              'data' => 
              array (
                'type' => 'objects',
                'id' => 'urn:adsk.objects:os.object:wip.dm.emea.2/0ae6c75a-0196-4999-921a-80eee9b1356d.png',
                'relationships' => 
                array (
                  'target' => 
                  array (
                    'data' => 
                    array (
                      'type' => 'folders',
                      'id' => 'urn:adsk.wipemea:fs.folder:co.h7aVKQsNQ2-SAal7y_DDoA',
                    ),
                    'links' => 
                    array (
                      'related' => 
                      array (
                        'href' => 'https://developer.api.autodesk.com/data/v1/projects/b.0ba0f51f-b130-427a-b8cb-4a013982f81c/folders/urn:adsk.wipemea:fs.folder:co.h7aVKQsNQ2-SAal7y_DDoA',
                      ),
                    ),
                  ),
                ),
              ),
            )
            2023-06-29 00:22:23
            array (
              0 => 'wip.dm.emea.2',
              1 => '0ae6c75a-0196-4999-921a-80eee9b1356d.png',
            )
            2023-06-29 00:22:23
            array (
              'uploadKey' => 'AQICAHhwdLfJxxjSW2elV4zBsR9a2onNLWfDL3EilPBWyEkdxgGQVHDYw-I8SSvQnwJwBhM5AAABxzCCAcMGCSqGSIb3DQEHBqCCAbQwggGwAgEAMIIBqQYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAwDCjvRjf9BYvdITAACARCAggF6fH5QD_36iPGKJa5JqpxeBc3MXuH5z8sb6OUF-ElLHUqYosXbQk1j83nHRt-bGkofxQl_8pfPPAuqYCrVzNCL4UtY6M5T0DtPrs1KHbd0CAEH1sUcmwpe64u4NjCIV5eQnSabsO5dzzrHDnAJY-U89ehaeEnV0oG1-RoobCPWw6fobjdFMvNxB1FFbvgz7P-wvKqENAnXTHIR8kK3OIV83bleC7aQ7Jet1QSmix499x4YTaPQcqWmALcDIIkxZ_DlnxQ4F2SMs3E1Tk6K_rhXFpb0I5p7uSir6KfvdNGeW2ziAoPnIvQt3aFbCiWjuLzWDVQuZ9nIZE_32Q2zSQs9JVoRVgE-982tguKCJut35vmjA26KMwASXd5NkEpuZwpNJtsX30xUA_Nz28S68lLyNmnPCLrQTFC8bNnUFUGLJ15jwCkTiN0YkssBUHBBYP-ad2XW2DtprqwV4ZOAG-tz3-0boqXbBMctU1g2ol-awREKI_xxwHLNTyJ-',
              'uploadExpiration' => '2023-07-02T00:00:00Z',
              'urlExpiration' => '2023-06-29T00:37:24Z',
              'urls' => 
              array (
                0 => 'https://com-autodesk-oss-prodemea-direct-upload.s3-accelerate.amazonaws.com/21/bd/3a/5d/bucket/wip.dm.emea.2/signed-url-uploads/21bd3a5d-dc5f-4564-8c98-bc80d35d3bca?uploadId=qGGYq4oBKP9NtlH5H8IzjCEK4_u1dn4q6WXc_vD_t1CIF3chDK.0AqiaXrvTeX1Z49UH8e4JuewqcTrbrzKUL6IA0sW5h2aybK.oca_YphxwVZAOVLwreZ4ETh4pi4pPoPqDBxgJx_u8yaWvYMF5tA--&partNumber=1&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEBgaCWV1LXdlc3QtMSJGMEQCIGHqwo1%2F%2Fa5D%2BEOhwoh0N4NHIsW8sWCf1PUg1tsIQNglAiAO8K2ZMs7o7YXQJwt6aPTvLmgRDhsbiNPH8LpFZwEA1Cq5BQiB%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAQaDDExMTgxNzg4NTk2MSIMFENuvBvqaykGZKRUKo0FPdNL6gA8gmQYo38bNNZ5WN29RYbTfxUPRtKDPrpHZREoEZhRxgEQDC7SXZqczlHSaR1lET%2FYa1iolkPr%2BSy4cRGp%2BciCjcCW%2BptgXLlNxh6KLCyQ%2Ftst%2F%2BgOllOI08YtFZkDGkZe%2FTLyvHiz7BK3Ujq7TB95SQD4w2Dl7b7Mnnypsc4Ezq2PT4%2F3ocVVO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credential=ASIARUCG5REEX62EH26M%2F20230629%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Signature=f817e7c3b14b44ca42945b7f40f8775eca31940374843b5db68b5edf4260cad8',
              ),
            )
            2023-06-29 00:22:24
            false
            2023-06-29 00:22:25
            array (
              'uploadKey' => 'AQICAHhwdLfJxxjSW2elV4zBsR9a2onNLWfDL3EilPBWyEkdxgGQVHDYw-I8SSvQnwJwBhM5AAABxzCCAcMGCSqGSIb3DQEHBqCCAbQwggGwAgEAMIIBqQYJKoZIhvcNAQcBMB4GCWCGSAFlAwQBLjARBAwDCjvRjf9BYvdITAACARCAggF6fH5QD_36iPGKJa5JqpxeBc3MXuH5z8sb6OUF-ElLHUqYosXbQk1j83nHRt-bGkofxQl_8pfPPAuqYCrVzNCL4UtY6M5T0DtPrs1KHbd0CAEH1sUcmwpe64u4NjCIV5eQnSabsO5dzzrHDnAJY-U89ehaeEnV0oG1-RoobCPWw6fobjdFMvNxB1FFbvgz7P-wvKqENAnXTHIR8kK3OIV83bleC7aQ7Jet1QSmix499x4YTaPQcqWmALcDIIkxZ_DlnxQ4F2SMs3E1Tk6K_rhXFpb0I5p7uSir6KfvdNGeW2ziAoPnIvQt3aFbCiWjuLzWDVQuZ9nIZE_32Q2zSQs9JVoRVgE-982tguKCJut35vmjA26KMwASXd5NkEpuZwpNJtsX30xUA_Nz28S68lLyNmnPCLrQTFC8bNnUFUGLJ15jwCkTiN0YkssBUHBBYP-ad2XW2DtprqwV4ZOAG-tz3-0boqXbBMctU1g2ol-awREKI_xxwHLNTyJ-',
              'status' => 'error',
              'reason' => 'MissingOrInvalidParts',
              'parts' => 
              array (
                0 => 
                array (
                  'part' => 1,
                  'status' => 'Pending',
                ),
              ),
            )
            2023-06-29 00:22:26

Here is an excerpt of my code for the file upload part:

        public static function uploaditem($token, $path_to_upload, $bucket_key, $object_id) 
        {  
            $url = "https://developer.api.autodesk.com/oss/v2/buckets/$bucket_key/objects/$object_id";
    
            $mime = mime_content_type($path_to_upload);
    
            $head = [
                'Authorization: Bearer ' . $token,
                'Content-Type: ' . $mime
            ];
    
            
            if (function_exists('curl_file_create')) { // php 5.5+
              $cFile = curl_file_create($path_to_upload);
            } else { // 
              $cFile = '@' . realpath($path_to_upload);
            }
            $post = array('extra_info' => '123456','file_contents'=> $cFile);
    
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url); 
            curl_setopt($curl, CURLOPT_HEADER, true); // 启用输出头部信息
            curl_setopt($curl, CURLOPT_VERBOSE, true); // 启用详细输出        
            if(!empty($head))curl_setopt($curl, CURLOPT_HTTPHEADER, $head);  
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
            curl_setopt($curl, CURLOPT_PUT, true);
    
            $response = curl_exec($curl);
            curl_close($curl);
    
            return $response;
        }

Here is an sample of my code for the file upload part:

//The function uploaditem($token, $path_to_upload, $bucket_key, $object_id) takes in parameters that are passed in from the previous step. As you can see from the code returned above, there should be no errors.

  public static function uploaditem($token, $path_to_upload, $bucket_key, $object_id) 
{  
    $url = "https://developer.api.autodesk.com/oss/v2/buckets/$bucket_key/objects/$object_id";

    $mime = mime_content_type($path_to_upload);

    $head = [
        'Authorization: Bearer ' . $token,
        'Content-Type: ' . $mime
    ];

    
    if (function_exists('curl_file_create')) { // php 5.5+
      $cFile = curl_file_create($path_to_upload);
    } else { // 
      $cFile = '@' . realpath($path_to_upload);
    }
    $post = array('extra_info' => '123456','file_contents'=> $cFile);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HEADER, true); // 启用输出头部信息
    curl_setopt($curl, CURLOPT_VERBOSE, true); // 启用详细输出        
    if(!empty($head))curl_setopt($curl, CURLOPT_HTTPHEADER, $head);  
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_PUT, true);

    $response = curl_exec($curl);
    curl_close($curl);

    return $response;
}

I want to use the Data Management API to Upload a File

Eason Kang
  • 6,155
  • 1
  • 7
  • 24

1 Answers1

0

It looks like you're using the deprecated old OSS API for uploading, not https://aps.autodesk.com/en/docs/data/v2/tutorials/upload-file/ is using. If you're still using the old one, then now please use the direct-S3 now instead.

Here is the working sample using OSS DirectS3 API for your reference:

<?php
// Function for basic field validation (present and neither empty nor only white space
// ref: https://stackoverflow.com/a/381275
function isNullOrEmptyString($str)
{
    return ($str === null || trim($str) === '');
}

function getUploadUrls($accessToken, $bucketKey, $objectKey, $minutesExpiration = null, $parts = 1, $firstPart = 1, $uploadKey = null)
{
    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $accessToken,
    );

    $queries = array(
        'parts' => $parts,
        'firstPar' => $firstPart
    );

    if (!isNullOrEmptyString($uploadKey))
        $queries['uploadKey'] = $uploadKey;

    if (!is_null($minutesExpiration))
        $queries['minutesExpiration'] = $minutesExpiration;

    $url = 'https://developer.api.autodesk.com/oss/v2/buckets/' . $bucketKey . '/objects/' . $objectKey . '/signeds3upload';
    $url .= '?' . http_build_query($queries);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

    // print_r($http_status);
    // print_r($result);

    $header = substr($response, 0, $header_size);
    $body   = substr($response, $header_size);

    $data['status'] = $http_status;
    $data['header'] = $header;
    $data['body']   = $body;

    curl_close($ch);

    return $data;
}

function completeUpload($accessToken, $bucketKey, $objectKey, $uploadKey)
{
    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $accessToken,
    );

    $body = array(
        'uploadKey' => $uploadKey
    );

    $url = 'https://developer.api.autodesk.com/oss/v2/buckets/' . $bucketKey . '/objects/' . $objectKey . '/signeds3upload';

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    $response = curl_exec($ch);

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

    $header = substr($response, 0, $header_size);
    $body   = substr($response, $header_size);

    $data['status'] = $http_status;
    $data['header'] = $header;
    $data['body']   = $body;

    curl_close($ch);

    return $data;
}

function uploadStream($url, $stream)
{
    $headers = array(
        'Content-Type: application/octet-stream',
        //'Authorization: Bearer ' . $accessToken,
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stream);
    $response = curl_exec($ch);

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);

    $header = substr($response, 0, $header_size);
    $body   = substr($response, $header_size);

    $data['status'] = $http_status;
    // $data['header'] = $header;
    // $data['body']   = $body;
    $data['data'] = $response;

    curl_close($ch);

    return $data;
}

function uploadToBucket($accessToken, $bucketKey, $objectKey, $filePath)
{
    $fileSize = filesize($filePath);
    $maxBatches = 25;
    $chunkSize = 5 * 1024 * 1024;
    $nbChunks = (int)round((double)$fileSize / (double)$chunkSize) + 1;
    $partsUploaded = 0;
    $start = 0;
    $uploadUrls = [];
    $uploadKey = null;

    $handle = fopen($filePath, 'r');

    if (count($uploadUrls) <= 0) {
        $uploadParamsRes = getUploadUrls($accessToken, $bucketKey, $objectKey, null, min($chunkSize - $partsUploaded, $maxBatches), $partsUploaded + 1, $uploadKey);
        $uploadParams = json_decode($uploadParamsRes['body']);

        //var_dump($uploadParams);

        $uploadKey = $uploadParams->uploadKey;
        $uploadUrls = $uploadParams->urls;
    }

    while ($partsUploaded < $nbChunks) {
        echo 'Uploading part: ' . $partsUploaded + 1 . "\n";

        $end = min(($partsUploaded + 1) * $chunkSize, $fileSize);
        $numberOfBytes = $end - $start;

        $fileBytes = fread($handle, $numberOfBytes);

        try {
            $url = $uploadUrls[$partsUploaded];
            $uploadRes = uploadStream($url, $fileBytes);
        } catch (Exception $e) {
            fclose($handle);
            echo 'Exception when uploading object: ', $e->getMessage(), PHP_EOL;
        }

        $partsUploaded++;
        $start = $end;
    }

    fclose($handle);

    $completedRes = completeUpload($accessToken, $bucketKey, $objectKey, $uploadKey);
    var_dump($completedRes);
}


$accessToken = 'eyJh...';
$bucketKey = 'wip.dm.emea.2';
$objectKey = '0ae6c75a-0196-4999-921a-80eee9b1356d.png';

uploadToBucket($accessToken, $bucketKey, $objectKey, './test.png');

?>
Eason Kang
  • 6,155
  • 1
  • 7
  • 24
  • Thank you for your answer. It was very useful to me. I have also tested the latest version of the interface, but it is not as complete as what you wrote. – jian zhang Jul 06 '23 at 02:05
  • I will continue to use your interface in the viewer api to view files. When used 'https://developer.api.autodesk.com/modelderivative/v2/regions/eu/designdata/job' to do translation job, it returns false. I have sent you a email for the details. Please take a look, thank you! I don't know how to pursue this here – jian zhang Jul 06 '23 at 07:22
  • Additionally, if this issue can be resolved, the above issues can be ignored. https://stackoverflow.com/questions/76626903/if-the-file-is-in-a-bim-360-or-acc-workspace-how-to-view-it-in-the-browser – jian zhang Jul 06 '23 at 08:30
  • The translation is another thing. It's irrelevant to the upload. Please share your code snippet for sending your translation request using Model Derivative API. BTW, API response doesn't return false, so it would be something you made in the PHP codes. – Eason Kang Jul 07 '23 at 08:10
  • My code snippet for sending your translation request using Model Derivative API. is here:https://stackoverflow.com/questions/76626903/if-the-file-is-in-a-bim-360-or-acc-workspace-how-to-view-it-in-the-browser thank you. – jian zhang Jul 08 '23 at 00:29