ASW-SDK S3上传中载中止

发布于 2025-01-19 18:45:23 字数 753 浏览 1 评论 0原文

我要中传中载,但是呼叫upload.abort()提出错误上传中载已上传。。如何无声地中载上载(没有错误)?

import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3';
import { Progress, Upload } from "@aws-sdk/lib-storage";

const uploadParams: PutObjectCommandInput = {
  Bucket: 'my-bucket',
  Key: 'my-file',
  Body: file,
};

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

// start upload
upload.done();

我还试图抓住诺言

upload.abort().then().catch(() => {
  // ...
})

,也试图尝试抓住一切

try {
  upload.abort().then().catch(() => {
    // ...
  })
} catch () {
  // ...
}

I want abort an upload, but calling upload.abort() raise an error Upload aborted.. How abort the upload silently (without error)?

import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3';
import { Progress, Upload } from "@aws-sdk/lib-storage";

const uploadParams: PutObjectCommandInput = {
  Bucket: 'my-bucket',
  Key: 'my-file',
  Body: file,
};

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

// start upload
upload.done();

I have also tried to catch the promise

upload.abort().then().catch(() => {
  // ...
})

And also tried to try catch all

try {
  upload.abort().then().catch(() => {
    // ...
  })
} catch () {
  // ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-01-26 18:45:23

该错误实际上是从 upload.done Promise 抛出的。

您可以检查代码 这里此处< /a>

对于您的情况,您需要类似以下内容:

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

try {
  // start upload
  upload.done();
} catch (err) {
    if (err.name === 'AbortError') {
      // Handle gracefully
    }
}

That error is actually being thrown from the upload.done Promise.

You can check the code here and here

For your case, you would want something like the following:

const upload: Upload = new Upload({
  client: s3Client,
  params: uploadParams,
});

// abort after 3 seconds
setTimeout(() => upload.abort(), 3000);

try {
  // start upload
  upload.done();
} catch (err) {
    if (err.name === 'AbortError') {
      // Handle gracefully
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文