Microsoft graph api Msgraph-使用typeScript上载大型文件
我有一种上传大文件的方法,叫做Microsoft graph api Msgraph-使用typeScript上载大型文件,microsoft-graph-api,email-attachments,Microsoft Graph Api,Email Attachments,我有一种上传大文件的方法,叫做uploadLargeAttachment,由于某些原因,它一直失败,错误代码为503 这是我的方法 public static maxAttachmentBytes = Math.pow(1024, 2) * 3; private async getAuthHeaders() { const token = await this.getAccessToken(); return { authorization: `Bearer ${t
uploadLargeAttachment
,由于某些原因,它一直失败,错误代码为503
这是我的方法
public static maxAttachmentBytes = Math.pow(1024, 2) * 3;
private async getAuthHeaders() {
const token = await this.getAccessToken();
return {
authorization: `Bearer ${token}`,
};
}
public async uploadLargeAttachment(
inbox: string,
messageId: string,
attachment: MSGraph.UploadAttachmentData
) {
let content = Buffer.from(attachment.contentBytes, "base64");
const session = await this.axios.post(
`users/${inbox}/messages/${messageId}/attachments/createUploadSession`,
{
AttachmentItem: {
attachmentType: "file",
name: attachment.name,
size: attachment.size,
},
},
{
headers: await this.getAuthHeaders(),
}
);
let start = 0;
try {
while (content.byteLength > 0) {
const bytes = Math.min(
EmailRepository.maxAttachmentBytes,
content.byteLength
);
const result = await this.axios.put(
session.data.uploadUrl,
content.slice(0, bytes),
{
// maxContentLength: Infinity,
headers: {
"Content-Range": `bytes ${start}-${start + bytes - 1}/${
attachment.size
}`,
"Content-Type": "application/octet-stream",
"Content-Length": bytes,
"Retry-After": 5, --> this second try
},
}
);
if (result.data.NextExpectedRanges) {
start = parseInt(result.data.NextExpectedRanges[0]);
}
content = content.slice(bytes);
}
} catch (error) {
await this.axios.delete(session.data.uploadUrl);
throw error;
}
}
我的朋友试图解决这个问题
1-减少maxAttachmentBytes
2-我添加了“重试后”:5,
但还是失败了
我错过了什么
错误日志
Error: Request failed with status code 503
at createError (/Users/minafawzy/Documents/TechModgroup/BDS-API/node_modules/axios/lib/core/createError.js:16:15)
at settle (/Users/minafawzy/Documents/TechModgroup/BDS-API/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/Users/minafawzy/Documents/TechModgroup/BDS-API/node_modules/axios/lib/adapters/http.js:236:11)
at IncomingMessage.emit (events.js:327:22)
at IncomingMessage.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
我错过了拼写
NextExpectedRanges
我就这样修好了
if (result.data.nextExpectedRanges) {
start = parseInt(result.data.nextExpectedRanges[0]);
}
HTTP错误503表示服务不可用。我会尝试与MS Graph Explorer或POSTMAN对同一租户进行相同的呼叫,看看它是否有效;你也可以尝试上传小附件,但那是因为NextExpectedRanges写错了