文件上载到azure文件共享无法处理更大的文件

mrfwxfqh  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(300)

我正在尝试使用此@azure/存储文件共享节点js客户端库将大文件(0到100mb)上载到azure文件共享。
小于4MB的小文件可以正常工作,但大于4MB则会出错(contentlength必须大于0且<=4194304字节)
我的有效负载在可读流中,我在库中使用方法uploadrange
下面是代码

{
        method: "POST",
        path: `${apiBase}/mount`,
        config: {
          description: "uploadFile",
          tags: ["api", "file"],
          notes: ["upload a file"],
          auth: {
            strategy: "jwt",
            mode: options.session.auth,
            access: {
              scope: options.session.scope,
            },
          },
          payload: {
            maxBytes: 1024 * 1024 * 200,
            multipart: true,
            parse: true,
            output: "stream",
            allow: "multipart/form-data",
          },
          timeout: {
            server: 10 * 60 * 1000,
            socket: 12 * 60 * 1000,
          },

          response: {
            status: {
              200: Joi.object({
                status: Joi.string(),
                fileUrl: Joi.string(),
                date: Joi.number(),
              }),
              422: Joi.object({
                statusCode: Joi.number().integer(),
                error: Joi.string(),
                message: Joi.string(),
              }),
              503: Joi.object({
                statusCode: Joi.number().integer(),
                error: Joi.string(),
                message: Joi.string(),
              }),
            },
          },
          handler: async (request, h) => {
            if (!request.auth.credentials) {
              throw Boom.unauthorized("unexpected unauthorized error");
            }
            try {
              const azureDirectory = azure.sanitizeContainerName(
                request.auth.credentials.userId
              );
              const azureFileStream = request.payload.file;
              const r = await azure.uploadFileToVolume({
                azureFileServiceClient: options.azureFileServiceClient,
                azureFileVolumeMount: options.azureFileVolumeMount,
                azureFileStream,
                azureDirectory,
              });
              if (r.errorCode) {
                throw Boom.badData(
                  `file upload error due to azure error ${r.errorCode}` +
                    `\n${JSON.stringify(r)}`
                );
              }

              return h.response({
                status: "ok",
                fileUrl:
                  options.azureFileServiceClient.url +
                  azureDirectory +
                  // encode file name
                  `/${encodeURIComponent(azureFileStream.hapi.filename)}`,
                date: new Date(r.lastModified).getTime(),
              });
            } catch (e) {
              throw Boom.serverUnavailable(`file upload error ${e}`);
            }
          },
        },
      }

exports.uploadFileToVolume = async ({
  azureFileServiceClient,
  azureFileVolumeMount,
  azureFileStream,
  azureDirectory,
}) => {
  const directoryClient = await azureFileServiceClient
    .getShareClient(azureFileVolumeMount)
    .getDirectoryClient(azureDirectory);

  if (!(await directoryClient.exists())) {
    // create azure directory by userid if not exists
    await azureFileServiceClient
      .getShareClient(azureFileVolumeMount)
      .createDirectory(azureDirectory);
  }

  const content = azureFileStream._data;
  const fileName = encodeURIComponent(azureFileStream.hapi.filename);
  const fileClient = directoryClient.getFileClient(fileName);
  await fileClient.create(content.length);

  return await fileClient.uploadRange(content, 0, content.length);

};

有谁能帮我在库中找到正确的方法来发送文件吗?我试图使用uploadstream,但它不起作用。

uqxowvwt

uqxowvwt1#

您出现此错误的原因是因为 uploadRange 是4mb。 uploadRange 操作Map到 Put Range rest api操作,限制来自rest api方面(有关详细信息,请参阅说明 Range or x-ms-range 在请求头部分)。
你要做的就是阅读你的内容,然后打电话给我 uploadRange 方法重复使用这些块。读取块时,必须确保所读取块的最大大小不超过4mb。

相关问题