Msgraph - 使用 typeScript 上传大文件
Msgraph - upload large files using typeScript
我有一个名为 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- 我添加了 "Retry-After": 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]);
}
我有一个名为 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- 我添加了 "Retry-After": 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]);
}