我正在尝试使用Autodesk的Model Derivative API将.zip
文件翻译成.svf
清单。我能够成功创建一个存储桶,使用分块上传将zip放入存储桶,并使用相应的端点开始转换作业(所有端点返回200)。当我来检查作业的翻译进度时,它会停止在“0%完成”,并最终返回TranslationWorker-InternalFailure
的失败消息。
这一切都是在Node.js&堆栈上以编程方式完成的。 superagent发出HTTP请求。我可以通过使用Paw手动翻译来确认文件本身是否有效;并且还使用我们正在替换的旧工具进行翻译。
请参阅下面的代码的特定部分。
translate: async function(accessToken, obj) {
return await request.post('https://developer.api.autodesk.com/modelderivative/v2/designdata/job')
.set("Authorization", "Bearer " + accessToken)
.set("Content-Type", "application/json")
.set('x-ads-force', true)
.send({
input: {
urn: obj.base64Urn,
compressedUrn: true,
rootFilename: obj.parentFile
},
output: {
formats: [
{
type: "thumbnail"
},
{
type: "svf",
views: ["2d", "3d"]
}
]
}
});
}
在上面的代码中,变量具有以下值:
obj.base64Urn
由Autodesk通过以下函数提供的URN动态转换:
base64Urn: function() {
// https://tools.ietf.org/html/rfc7515#appendix-C
return this.getDataValue("urn") != null ? (new Buffer(this.getDataValue("urn"))).toString('base64').replace(/=+$/g, "").replace(/\+/g, "-").replace(/[\/]/g, "_") : null;
}
obj.parentFile
采用"160728 Small Test Project.rvt"
getTranslationProgressAndDerivatives: function(accessToken, obj) {
return request.get('https://developer.api.autodesk.com/modelderivative/v2/designdata/' + obj.base64Urn + '/manifest')
.set('Authorization', "Bearer " + accessToken);
}
当然,必须在某些时候将obj放在Autodesk的服务器上。这是通过以下代码完成的:
class HttpAutodeskPutObjectWriteStream extends stream.Writable {
/**
* Constructor sets all the properties for the instance.
*
* @param {string} accessToken - The OAuth2 access token needed for server authentication to engage with Autodesk's API.
* @param {Obj} obj
*/
constructor(accessToken, bucket) {
super();
this.accessToken = accessToken;
this.obj = obj;
this._bytesTransferred = 0;
this._putHttpUrl = 'https://developer.api.autodesk.com/oss/v2/buckets/' + this.obj.name + '/objects/' + this.obj.name + '/resumable';
}
/**
* Return a bytes transferred statement.
*
* @private
*
* @param chunk - The chunk currently being transferred.
*
* @returns {string}
*/
_bytesTransferredStatement(chunk) {
return `bytes ${this._bytesTransferred}-${this._bytesTransferred+chunk.byteLength-1}/${this.obj.zipFileSize}`;
};
/**
* Writes data to the stream. Note the use of the serialize method in the request.
*
* @private
*
* @param chunk - The chunk currently being transferred.
* @param encoding - The encoding of the chunk data.
* @param callback - The function to be called on chunk completion (success or otherwise).
*
* @returns {Promise.<void>}
*/
async _write(chunk, encoding, callback) {
try {
let stmt = this._bytesTransferredStatement(chunk);
this._bytesTransferred += chunk.byteLength;
let response = await request.put(this._putHttpUrl)
.set("Authorization", "Bearer " + this.accessToken)
.set("Session-Id", this.bucket.key)
.set("Content-Length", chunk.byteLength)
.set("Content-Range", stmt)
.serialize(function(d) { return d; })
.send(chunk.toString());
if (response.status === 200) {
this.urn = response.body.objectId;
}
callback(null);
} catch (e) {
callback(e);
}
};
}
然后在这里调用:
put: function(accessToken, bucketEntity) {
return new Promise(async (resolve, reject) => {
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).
const objStorageLocation = (await config.get())[process.env.NODE_ENV].objStorageLocation;
const pathToFile = path.join(__dirname, "../../", objStorageLocation, obj.name + ".zip");
let readFileStream = fs.createReadStream(pathToFile, { highWaterMark: maximalChunkedTransferSize });
let ws = new HttpAutodeskPutObjectWriteStream(accessToken, obj);
readFileStream.pipe(ws);
ws.on("finish", () => resolve(ws.urn));
ws.on("error", () => reject());
});
}
一旦失败,这是我从翻译进度端点收到的响应:
{
"type": "manifest",
"hasThumbnail": "false",
"status": "failed",
"progress": "complete",
"region": "US",
"urn": "<redacted>",
"version": "1.0",
"derivatives": [
{
"name": "LMV Bubble",
"hasThumbnail": "false",
"status": "failed",
"progress": "complete",
"messages": [
{
"type": "error",
"message": "Translation failure",
"code": "TranslationWorker-InternalFailure"
}
],
"outputType": "svf"
}
]
}
我在这里做了什么明显的错误,在尝试翻译文件时会导致100%的失败率?
答案 0 :(得分:1)
当然,在StackOverflow上发帖会让您从不同的角度审视您的代码。问题在于我上传文件,特别是最后一个链式方法:
let response = await request.put(this._putHttpUrl)
.set("Authorization", "Bearer " + this.accessToken)
.set("Session-Id", this.bucket.key)
.set("Content-Length", chunk.byteLength)
.set("Content-Range", stmt)
.serialize(function(d) { return d; })
.send(chunk.toString()); // HERE
在块上调用.toString()
是不正确的。