最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何使用 aws sdk v3 跟踪 S3 中 Node.js 的上传进度

网站源码admin40浏览0评论

如何使用 aws sdk v3 跟踪 S3 中 Node.js 的上传进度

如何使用 aws sdk v3 跟踪 S3 中 Node.js 的上传进度

是否可以通过

PutObjectCommand
@aws-sdk/client-s3
跟踪S3的上传进度?

response.on('end', async () => {
  try {
    console.log('File downloaded successfully!');
    console.log('File deleted from Seedr!');

    socket.emit('message', 'Successfully downloaded "' + downloadPath + '" to API server, File deleted from Seedr');
    await seedr.deleteFolder(selectedVideo.fid);

    // Upload the file to S3
    const fileContent = fs.createReadStream(downloadPath);
    uploadParams.Body = fileContent;

    socket.emit('message', 'Uploading "' + s3key + '" to S3.');

    const uploadCommand = new PutObjectCommand(uploadParams);
    await s3.send(uploadCommand);

    console.log(`File uploaded to S3 bucket: ${S3_BUCKET}`);

    // Remove the downloaded file from the server
    fs.unlinkSync(downloadPath);

    // Notify the client that the operation is complete
    socket.emit('message', `Successfully uploaded "${s3key}" to S3 and file deleted from local server`);

    activeDownloads.delete(s3key);
  } catch (error) {
    console.error(`Error after file download: ${error}`);
    socket.emit('error', {
      s3key,
      message: `Error after file download: ${error.message}`,
    });
    activeDownloads.delete(s3key);
  }
});

当我使用 sdk v2 时很容易,但在 v3 中我无法获得任何解决方案。

回答如下:

是的,可以通过

@aws-sdk/client-s3
的PutObjectCommand来跟踪S3的上传进度。为了跟踪上传进度,您可以使用 AWS SDK for Node.js v3 提供的 ManagedUpload 类。以下是如何使用 ManagedUpload 跟踪 S3 上传进度的示例:

const upload = new ManagedUpload({
  client: s3,
  params: uploadParams,
  partSize: 5 * 1024 * 1024, // 5 MB chunks
  queueSize: 4, // Upload 4 parts in parallel
});

upload.on('httpUploadProgress', (progress) => {
  const percentCompleted = Math.round((progress.loaded / progress.total) * 100);
  console.log(`Upload progress: ${percentCompleted}%`);
});

try {
  console.log(`Uploading "${s3key}" to S3...`);
  const result = await upload.promise();
  console.log(`File uploaded to S3 bucket: ${S3_BUCKET}`);
} catch (error) {
  console.error(`Error uploading file to S3: ${error}`);
}

来自https://chat.openai/

发布评论

评论列表(0)

  1. 暂无评论