Autodesk forge 文件转换为 svf 不起作用

Autodesk forge file translation to svf not working

我目前正在关注 Autodesk Forge 上的为查看器准备文件教程。这是 link: https://forge.autodesk.com/en/docs/model-derivative/v2/tutorials/prep-file4viewer/ 我没有使用 cURL 发出请求,而是使用 jquery ajax 编写了 javascript 代码来发出请求。我已将每个请求设为一个函数,并带有一个按钮来触发每个请求,如下代码所示。目前,除了转换为 svf 请求外,每个请求都可以正常工作。当我尝试发出该请求并检查我的请求状态时,会打印以下消息:

    {
"urn": "dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6cHJvamVjdGZpbGVzMTIzNC9fTUMtMTQwMDYlMjBGT0xERVIlMjBBU1NZJTIwLSUyMEhTQ0YtMTIuaWFtLmR3Zg",
"derivatives": [{
    "hasThumbnail": "false",
    "name": "_MC-14006 FOLDER ASSY - HSCF-12.iam.dwf",
    "progress": "complete",
    "messages": [{
        "type": "error",
        "code": "DWF2D-Not_A_DWF_Error",
        "message": "Unable to open, not a DWF/DWFx file."
    }, {
        "type": "error",
        "message": "Extractor error code -11",
        "code": "TranslationWorker-InternalFailure"
    }],
    "outputType": "svf",
    "status": "failed"
}],
"hasThumbnail": "false",
"progress": "complete",
"type": "manifest",
"region": "US",
"version": "1.0",
"status": "failed"

使用邮递员,我可以发出相同的请求,一切都会正常进行。然后,当我再次尝试翻译该文件时,只要它来自同一个存储桶,它就会起作用。我需要能够立即从代码上传和翻译的文件,而不必先在邮递员中翻译它。我什至试图复制翻译请求的邮递员代码,但是当文件不在我的存储桶中时我得到了同样的错误。下面是我的简单网页代码。

<html>
<title>Viewer</title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.7/jstree.min.js"></script>

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/style.min.css" type="text/css">
    <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.min.js"></script>
    <style>
        body {
            margin: 0;
        }
        #forgeViewer {
            width: 100%;
            height: 100%;
            margin: 40px;
            background-color: #F0F8FF;
        }
    </style>
</head>
<script>
//variable declaration
var access_token='';
const bucketKey = 'projectfiles123874';
const policyKey = 'transient';
var urn1 = '';
var urn2 = '';
var urn3 = '';
//var fileName = '_MC-14006 FOLDER ASSY - HSCF-12.iam.dwf';
var fileName = '';

if(access_token == ''){
getToken();
}

function getToken(){
    $.ajax({
        method: 'POST',
        url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
        },
        data: 'client_id=lAOwCkVLo307t4fxWdEriDrPs1CAhdMl&client_secret=AhWJ5cIrm6Lh65AB&grant_type=client_credentials&scope=data:read data:write bucket:read viewables:read bucket:create data:create',
        success:function(response){
            access_token=response.access_token;
            console.log(response);
            console.log(access_token);
        },
        error:function(error){
            console.log(error);
        }
    }) 
}

function createBucket(){
    $.ajax({
        method: 'POST',
        url: "https://developer.api.autodesk.com/oss/v2/buckets",
        headers:{
            "content-type": "application/json",
           // "x-ads-force":true,
            Authorization: "Bearer " + access_token
        },
        data: JSON.stringify({
            'bucketKey': bucketKey,
            'policyKey': policyKey
        }),

        success:function(response){
            console.log(response);

            //window.location.href = "http://team/www/upload.html";

        },
        error:function(error){
            if (error.response && error.response.status == 409){
                console.log("bucket alrready exists, skipping creation.");
            }
            else{
                console.log("failed to create new bucket.");
            }
        }
    })
}

function uploadFile(){

    $.ajax({
        method: 'PUT',
        url:"https://developer.api.autodesk.com/oss/v2/buckets/"+encodeURIComponent(bucketKey)+"/objects/"+encodeURIComponent(fileName)+"",
        data: 'C:\Users\matthew_vidovic\Desktop\DWGs\_MC-7251 CLOROX CARTON FORMER.iam.dwf',
        headers:{
            Authorization: "Bearer " + access_token
        },
        success:function(response){
            console.log(response);
           urn1 = response.objectId;
           console.log(urn1);
            urn2 = btoa(urn1);
            console.log(urn2);
        },
        error:function(error){
            console.log(error);
        }
    })
}

function translateToSVF(){

    $.ajax({
        method: 'POST',
        url:"https://developer.api.autodesk.com/modelderivative/v2/designdata/job",
        headers:{
            "content-type": "application/json",
            Authorization: "Bearer " + access_token
        },
        data:JSON.stringify({
            "input":{ "urn":urn2

            },
            "output":{ "destination":{
                "region": "us"
            },
                    "formats": [
                        {
                            "type": "svf",
                            "views": [
                                "2d",
                                "3d"
                            ]
                        }
                    ]
            }
        }),
        success:function(response){
            console.log(response);
            urn3=response.urn;
            console.log(urn3);
           // console.log(access_token);
        },
        error:function(error){
            console.log(error);
        }
    })
/*    POSTMAN REQUEST CODE
    var settings = {
  "url": "https://developer.api.autodesk.com/modelderivative/v2/designdata/job",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Content-Type": "application/json",
    "Cookie": "PF=5aKDLaxVErxdqJY5p40NQy",
    Authorization: "Bearer " + access_token
  },
  "data": JSON.stringify({"input":{"urn":urn2,"rootFilename":fileName,"compressedUrn":false},"output":{"destination":{"region":"us"},"formats":[{"type":"svf","views":["2d","3d"]}]}}),
};

$.ajax(settings).done(function (response) {
  console.log(response);
  urn3 = response.urn;
  console.log(urn3);
});
*/
}



function checkStatus(){
    $.ajax({
        method: 'GET',
        url: "https://developer.api.autodesk.com/modelderivative/v2/designdata/" + encodeURIComponent(urn3) + "/manifest",
        headers:{
            Authorization: "Bearer " + access_token
        },
        success: function(response){
            console.log(response);
        },
        error: function(error){
            console.log(error);
        }
    })
}

    /*
    if(access_token == ''){
        getToken();
    }
    else if(urn1 == ''){
        uploadFile();
    }
    else translateToSVF();
    */
    function displayViewer(){
var viewer;
var options = {
    env: 'AutodeskProduction',
    api: 'derivativeV2',  // for models uploaded to EMEA change this option to 'derivativeV2_EU'

    getAccessToken: function(onTokenReady) {

        var token = access_token;
        var timeInSeconds = 3600; // Use value provided by Forge Authentication (OAuth) API
        onTokenReady(token, timeInSeconds);
    }
};

Autodesk.Viewing.Initializer(options, function() {

    var htmlDiv = document.getElementById('forgeViewer');
    viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv);
    var startedCode = viewer.start();
    if (startedCode > 0) {
        console.error('Failed to create a Viewer: WebGL not supported.');
        return;
    }

    console.log('Initialization complete, loading a model next...');

});

var documentId = 'urn:'+urn3;
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);

function onDocumentLoadSuccess(viewerDocument) {
    var defaultModel = viewerDocument.getRoot().getDefaultGeometry();
    viewer.loadDocumentNode(viewerDocument, defaultModel);
}

function onDocumentLoadFailure() {
    console.error('Failed fetching Forge manifest');
}

}
/*
function check(){
    if (access_token='') getToken
}
*/

function processSelectedFiles(fileInput) {
  var files = fileInput.files;

  for (var i = 0; i < files.length; i++) {
   // alert("Filename " + files[i].name);
    fileName = files[i].name;
    console.log(fileName);
  }
}


</script>
<body><h1>Model Viewer</h1>
<p><button onclick="getToken()">Get Token</button></p>
<p><button onclick="createBucket()">Create Bucket</button></p>
<p><button onclick="uploadFile()">upload file</button></p>
<p><button onclick="translateToSVF()">Translate</button></p>
<p><button onclick="checkStatus()">Check Status</button></p>
<p><button onclick="displayViewer()">display Viewer</button></p>
<p><input type="file" id="input" onchange="processSelectedFiles(this)"></p>

<div id="forgeViewer"></div>

</body>
</html>

如果您能提供任何关于为什么会发生这种情况的帮助,我们将不胜感激。 干杯!

您的 PUT 没有指定文件名,只是一个文件夹,所以上传可能已损坏。此外,您无需提供 content-type

文件必须由用户选择,您的浏览器应用无权访问本地文件夹结构。您可以创建一个 input 文件或 form,然后将其传递给 data 字段

    method: 'PUT',
    url:"https://developer.api.autodesk.com/oss/v2/buckets/"+encodeURIComponent(bucketKey)+"/objects/"+encodeURIComponent(fileName)+"",
    data: theForm.theInput.files[0],
    headers:{
        Authorization: "Bearer " + access_token
    },

您可以通过 SHA-1 签名验证您的下载,或者更简单地比较本地文件大小和实时文件大小(存储桶内)。