MSGraph-使用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)

解决方法

我很想念var filmCharacters = [ ['Vito','Michael','Sonny','Freddo'],['Mia','Vincent','Jules','Butch'],['Bella','Edward','Jacob','Carlisle'],['James','M','Moneypenny','Felix'] ]; for (var i = 0; i < filmCharacters.length; i++) { for (var j = 0; j < filmCharacters[i].length; j++) { if (filmCharacters[i][j].startsWith('M') == true) { console.log(filmCharacters[i][j]); } } }的拼写 我像那样固定

NextExpectedRanges