我正在嘗試在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":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}"
應該有一個鏈接/ URL它在返回所以附件'的console.log(我)',而不是'的console.log( i.id)' –
我在媒體上看到的唯一鏈接就是上面代碼示例中顯示的內容。 'i._self'是附件的鏈接。 'i.media'似乎是媒體數據的一個使用者 - 除非我使用它,否則會給我一個錯誤 - 再次看到我的最後一個代碼示例。 – RHarris