2017-08-23 21 views
1

我正在嘗試在Nodejs中使用CosmosDB。我有一個集合中創建一些文件,並增加了幾個附件的文件,像這樣的一個:如何使用Nodejs SDK從CosmosDB獲取附件?

let dbds = new azure(dbEndpoint, {"masterKey":dbKey}) 
fs.open("c:/temp/capture.png", "r", (err, fd) => { 
    if(err) console.log(err); 
    else{ 
     dbds.createAttachmentAndUploadMedia(documentLink, fs, {contentType:"image/png", partitionKey: partitionKey}, (err, result) =>{ 
     fs.close(fd); 
     if(err) console.log(err); 
     else console.log(result); 

     } 
    } 
} 

現在,當我讀到附件爲該文件,我得到2個附件:

dbds.readAttachments(documentLink, {"partitionKey":partitionKey}).toArray((err, result) => { 
    if(err) console.log(err); 
    else { 
    console.log(result.length); //2 
    result.forEach(i => console.log(i.id);) //id of each attachment 
    } 
} 

現在我需要能夠讀取文檔並將其存儲在本地。似乎沒有任何文檔可以在Nodejs中執行此操作。我試過以下內容:

let attachment = result[0]; //from result of toArray() above -- first attachment from list 
let mediaURI = `${attachment._self}${attachment.media}` 
dbds.readMedia(mediaURI, (err, result) => { 
    if(err) console.log(JSON.parse(err.body).message); //Request url is invalid 
    else { 
     //try to write result to file 
    } 
} 

如何爲媒體創建一個有效的URI來下載它?

編輯

基於下面的評論,我更新了我的代碼如下:

let attachment = result[0] 
let mediaURI = attachment.media //here is the change 
dbds.readMedia(mediaURI, (err, result) => { 
    if(err) console.log(JSON.parse(err.body).message); 
    else { 
     //try to store the data 
    } 
}) 

我不再得到一個錯誤,而是得到一個JSON對象:

"{"constants":{"O_RDONLY":0,"O_WRONLY":1,"O_RDWR":2,"S_IFMT"‌​:61440,"S_IFREG":327‌​68,"S_IFDIR":16384,"‌​S_IFCHR":8192,"S_IFL‌​NK":40960,"O_CREAT":‌​256,"O_EXCL":1024,"O‌​_TRUNC":512,"O_APPEN‌​D":8,"F_OK":0,"R_OK"‌​:4,"W_OK":2,"X_OK":1‌​},"F_OK":0,"R_OK":4,‌​"W_OK":2,"X_OK":1}" 
+0

應該有一個鏈接/ URL它在返回所以附件'的console.log(我)',而不是'的console.log( i.id)' –

+0

我在媒體上看到的唯一鏈接就是上面代碼示例中顯示的內容。 'i._self'是附件的鏈接。 'i.media'似乎是媒體數據的一個使用者 - 除非我使用它,否則會給我一個錯誤 - 再次看到我的最後一個代碼示例。 – RHarris

回答

0

我對這個有幫助的一些人在微軟。

主要問題是我沒有正確寫入文件。

而是這個

let dbds = new azure(dbEndpoint, {"masterKey":dbKey}) 
fs.open("c:/temp/capture.png", "r", (err, fd) => { 
    if(err) console.log(err); 
    else{ 
     dbds.createAttachmentAndUploadMedia(documentLink, fs, {contentType:"image/png", partitionKey: partitionKey}, (err, result) =>{ 
     fs.close(fd); 
     if(err) console.log(err); 
     else console.log(result); 

     } 
    } 
} 

隨着fs.open FS是不是FILESTREAM - 這正是createAttachmentAndUploadMedia所期待的。我錯誤地認爲fs是fs.open創建的文件流。

我傷口什麼事做了以下內容:

fs.readFile("c:/temp/myimage.jpg",(err, data) => { 
    if(err) reject(err); 
    else dbds.svc.createAttachmentAndUploadMedia(docPath,data,{contentType: "image/jpeg" 
     , partitionKey: "Key"}, (err, result) =>{..} 
}) 
0

在你的例子中,mediaURI應該是$ {attachment.media}不與$ {attachment._self}連接。

這裏是一個片段:

return new Promise<any>((resolve, reject) => { 
    this.documentDbClient.readAttachment(docLink, options, (err, result) => { 
     if (err) 
      return reject(err); 

     resolve(result.media); 
    }); 
}).then((medialink) => { 
    return new Promise<any>((resolve, reject) => { 
     this.documentDbClient.readMedia(medialink, (err, result) => { 
      if (err) 
       return reject(err); 

      // buffered data  
      const data = []; 
      result.on('data', (chunk) => data.push(chunk)); 

      result.on('end',() => { 
       resolve(data.join('')); 
      }); 

      // if not buffered 
      // resolve(result.toString()); 
     }); 
    }); 
}); 
+0

這有幫助 - 我不再收到一個錯誤,但現在我得到一個JSON字符串'「{」constants「:{」O_RDONLY「:0,」O_WRONLY「:1,」O_RDWR「:2,」S_IFMT「 :61440 「S_IFREG」:32768 「S_IFDIR」:16384 「S_IFCHR」:8192, 「S_IFLNK」:40960 「O_CREAT」:256, 「O_EXCL」:1024, 「O_TRUNC」:512, 「O_APPEND」:8 「F_OK」:0 「R_OK」:4 「W_OK」:2 「X_OK」:1}, 「F_OK」:0 「R_OK」:4 「W_OK」:2 「X_OK」:1} 「'而不是我的二進制數據。 – RHarris

+0

我的connectionPolicy被設置爲MediaReadMode Buffered,所以我期待的數據數組 – RHarris

+0

對不起,我認爲該文件只是一個文本字符串。如果結果被緩衝,我相信你只需要添加訂閱者到事件。我用未經測試的代碼編輯了我的回覆。 –

相關問題