How to upload file to storage location in Autodesk

2019-09-16 07:44发布

问题:

I have genereted accessToken by 3-legged Token, bucketKey and objectName by Create a Storage Location API as shown in this tutorial(step 4).

I want to upload file to storage location.My request returns 'OK 200' status, but it not upload file to A360, as I need. Where is the mistake? How to correctly upload file to user's storage location using 3-legged Token?

    [HttpGet]
    public JObject UploadFile(string accessToken,string bucketKey,string objectName,string filePath)
    {                      
        var url = String.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, objectName);

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = client.PutAsync(url, new StreamContent(System.IO.File.OpenRead(filePath))).Result.EnsureSuccessStatusCode();
            System.Diagnostics.Debug.WriteLine(response);
        }

    }

Request must returns JSON with parameters of created file,but returns:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Headers: Authorization, Accept-Encoding, Range, Content-Type
  Access-Control-Allow-Methods: GET
  Access-Control-Allow-Origin: *
  Connection: keep-alive
  Date: Wed, 11 Jan 2017 15:36:21 GMT
  Server: Apigee
  Server: Router
  Content-Length: 440
  Content-Type: application/json; charset=utf-8
}

回答1:

I have upload it files before to my a360 hub with no problem following the steps in that tutorial, Have you tried doing it by only Rest calls or cURL ones to make sure the process is right? I can tell you my steps using cURL so you can try them and let me know.

NOTE: When using cURL I use the JQ processor to visualize the response in my terminal in a more organized way. https://stedolan.github.io/jq/

3 legged Authentication

Step 0.1: Direct the User to the Authorization Web Flow, set the right scopes

https://developer.api.autodesk.com/authentication/v1/authorize?response_type=code&client_id=xxxxxxxxxx&redirect_uri=http%3A%2F%2Fxxxxx.com%2Foauth%2Fcallback&scope=data:read data:write bucket:create bucket:read data:create

Step 0.2: Implement Code that Extracts the Authorization Code

code = xxxxxxxxxxxxx-xxxxx-xxxxxxxxx

Step 0.3: Exchange the Authorization Code for an Access Token

curl -v 'https://developer.api.autodesk.com/authentication/v1/gettoken' -X 'POST' -H 'Content-Type: application/x-www-form-urlencoded' -d 'client_id=xxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx&grant_type=authorization_code&code=xxxxxxxxx-xxxxxxx&redirect_uri=http://xxxxxx.com/oauth/callback' | jq '.'

Response

{ "access_token": "OC5yVHwVyjZRfxQhXJxxxxxxxxx", "refresh_token": "o3pKZaUvEsIozWwdyldoHGmLjxxxxxxxxxxx", "token_type": "Bearer", "expires_in": 86399 }

Now that we have our 3 legged token with the lets move to the start of the steps to upload a file to A360 Hub

Step 1: Find the hub that has your resource

curl -X GET -H "Authorization: Bearer xxxxxxxxxxxxxx" "https://developer.api.autodesk.com/project/v1/hubs" | jq '.'

The response will be:

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs" } }, "data": [ { "type": "hubs", "id": "a.cGVyc29uYWw6dWxxxxxxxxxxx", // MAKE NOTE OF THIS ID "attributes": { "name": "autodesk", "extension": { "type": "hubs:autodesk.core:Hub", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/hubs%3Aautodesk.core%3AHub-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.YnVzaW5lc3M6YXV0xxxxxxxxxx" } }, "relationships": { "projects": { "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.YnVzaW5lc3M6YXV0b2Rlc2sxNDU/projects" } } } } }

Step 2: Find the project that has your resource

curl -X GET -H "Authorization: Bearer OC5yVHwVyxxxxxxxxxxxx" "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects" | jq '.'

Response

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects" } }, "data": [ { "type": "projects", "id": "a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ", "attributes": { "name": "Design Files to Share", "extension": { "type": "projects:autodesk.core:Project", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/projects%3Aautodesk.core%3AProject-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ" } }, "relationships": { "hub": { "data": { "type": "hubs", "id": "a.cGVyc29uYWw6dWxxxxxxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx" } } }, "rootFolder": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/folders/urn%3Aadsk.wipprod%3Afs.folder%3Aco.BKklEuaHRsiii-mKNxxxxxx" } } }, "topFolders": { "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/topFolders" } } } } }

//Step 3: Create a storage location

curl -X POST -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -H "Authorization: Bearer OC5yVHwVyjZRfxxxxxxxxx" -d '{
      "jsonapi": { "version": "1.0" },
      "data": {
        "type": "objects",
        "attributes": {
          "name": "lego-racecar.3dm"
        },
        "relationships": {
          "target": {
            "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }
          }
        }
      }
}' "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/storage" | jq '.'

The response of the call

{ "jsonapi": { "version": "1.0" }, "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "relationships": { "target": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/folders/urn%3Aadsk.wipprod%3Afs.folder%3Aco.BKklEuaHRsiii-mKNxxxxxx" } } } } } }

Step 4: Upload a file to the storage location

curl -X PUT -H "Authorization: Bearer OC5yVHwVyjZRfxQhXJjxxxxxxx" --data-binary @lego-racecar.3dm "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm" | jq '.'

Response

{ "bucketKey": "wip.dm.prod", "objectId": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "objectKey": "6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "sha1": "9c8e51403adc6b3bb6f586433bca66689xxxxxx", "size": 107743049,
"contentType": "application/x-www-form-urlencoded", "location": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm" }

And the final would be Step 5: Create the first version of the uploaded file

curl -X POST -H "Authorization: Bearer OC5yVHwVyjZRfxQhXJjxxxxxxxx" -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -d '{
    "jsonapi": { "version": "1.0" },
    "data": {
      "type": "items",
      "attributes": {
        "displayName": "lego-racecar.3dm",
        "extension": {
          "type": "items:autodesk.core:File",
          "version": "1.0"
        }
      },
      "relationships": {
        "tip": {
          "data": {
            "type": "versions", "id": "1"
          }
        },
        "parent": {
          "data": {
            "type": "folders",
            "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx"
          }
        }
      }
    },
    "included": [
      {
        "type": "versions",
        "id": "1",
        "attributes": {
          "name": "lego-racecar.3dm",
          "extension": {
            "type": "versions:autodesk.core:File",
            "version": "1.0"
          }
        },
        "relationships": {
          "storage": {
            "data": {
              "type": "objects",
              "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm"
            }
          }
        }
      }
    ]
  }' "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/items" | jq '.'

Response

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg" } }, "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:BlMbtGTyQ2eMLvZ05rVvAg", "attributes": { "displayName": "lego-racecar.3dm", "createTime": "2016-11-02T18:49:51+00:00", "createUserId": "TNSUM9VPW79S", "lastModifiedTime": "2016-11-02T18:49:51+00:00", "lastModifiedUserId": "TNSUM9VPW79S", "extension": { "type": "items:autodesk.core:File", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/items%3Aautodesk.core%3AFile-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg" } }, "relationships": { "tip": { "data": { "type": "versions", "id": "urn:adsk.wipprod:fs.file:vf.BlMbtGTyQ2eMLvZ05rVvAg?version=1" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/tip" } } }, "versions": { "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/versions" } } }, "parent": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.YgCsk6CGSUSTFeexhncYNw" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/parent" } } }, "refs": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/relationships/refs" }, "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/refs" } } }, "links": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/relationships/links" } } } } }, "included": [ { "type": "versions", "id": "urn:adsk.wipprod:fs.file:vf.BlMbtGTyQ2eMLvZ0xxxxxx?version=1", "attributes": { "name": "lego-racecar.3dm", "displayName": "lego-racecar.3dm", "createTime": "2016-11-02T18:49:51+00:00", "createUserId": "TNSUM9VPxxxxxx", "lastModifiedTime": "2016-11-02T18:49:51+00:00", "lastModifiedUserId": "TNSUM9VPxxxxxx", "versionNumber": 1, "mimeType": "application/vnd.autodesk.rhinoceros", "storageSize": 107743049, "fileType": "3dm", "extension": { "type": "versions:autodesk.core:File", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/versions%3Aautodesk.core%3AFile-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1" } }, "relationships": { "item": { "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:BlMbtGTyQ2eMLvZ0xxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/item" } } }, "links": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/relationships/links" } } }, "refs": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/relationships/refs" }, "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/refs" } } }, "downloadFormats": { "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/downloadFormats" } } }, "derivatives": { "data": { "type": "derivatives", "id": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxx/manifest" } } }, "thumbnails": { "data": { "type": "thumbnails", "id": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVyc2lxxxxx/thumbnail" } } }, "storage": { "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b086943xxxxx.3dm" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b086943xxxxx.3dm" } } } } } ] }

It can be a bit much of information, but this is the process I follow and has worked before. You could try it like I said before, with Postman doing the set of REST calls and later on build your ajax calls.

Let me know how it goes. Cheers,