首页 > 解决方案 > 得到“NoSuchKey:指定的密钥不存在。” 尝试在我可以证明存在的文件夹上复制对象时

问题描述

归根结底,我正在尝试复制一个文件夹以向该文件夹添加标签,根据文档,显然不可能在已经存在的文件夹和对象上添加(或编辑标签)。在我的应用程序中,我需要在设置记录的特定状态后删除充满附件的文件夹。因此,当我的应用程序中的状态发生变化时,我需要更新 S3 中的标签,以便 S3 管理规则可以在 30 天后启动并删除文件夹。

我在这里阅读了与 javascript 相关的帖子,但我认为我的问题与此无关

我能够使用成功创建一个文件夹s3.putObject

await s3.putObject({
  Bucket: process.env.BUCKET,
  Key: '611d308b6ef5e4066e047824/'
}).promise()

我能够确认该文件夹是使用创建的s3.listObjectsV2

const data = await s3.listObjectsV2({
  Bucket: process.env.BUCKET,
  Delimiter: '/',
  Prefix: '611d308b6ef5e4066e047824' // <-- no slash
}).promise()

console.log(data)
// {
//    IsTruncated: false,
//    Contents: [ [Object] ],
//    Name: '...', // <-- bucket name
//    Prefix: '611d308b6ef5e4066e047824/',
//    Delimiter: '/',
//    MaxKeys: 1000,
//    CommonPrefixes: [],
//    KeyCount: 1
// }

我还能够确认文件已上传(我为此使用 multer/multer-s3 快递),但使用s3.listObjectsV2它们在下可见Contents

const data = await s3.listObjectsV2({
  Bucket: process.env.BUCKET,
  Delimiter: '/',
  Prefix: '611d308b6ef5e4066e047824/' // <-- with a slash
}).promise()

console.log(data)

// {
//   IsTruncated: false,
//   Contents: [
//     {
//       Key: '611d308b6ef5e4066e047824/p19179.jpg',
//       LastModified: '2021-09-02T21:06:09.000Z',
//       ETag: '\"53e6ca3e139f171d486527bd7bcd16d4\"',
//       Size: 10154,
//       StorageClass: 'STANDARD'
//     }
//   ],
//   Name: '...', // <-- bucket name
//   Prefix: '611d308b6ef5e4066e047824/',
//   Delimiter: '/',
//   MaxKeys: 10,
//   CommonPrefixes: [],
//   KeyCount: 1,
//   StartAfter: '611d308b6ef5e4066e047824/0'
// }

当我尝试运行时出现错误,我s3.copyObject在这里添加标签,因为 Multer 和 Multer-S3 不支持添加标签,所以它必须通过s3.copyObject事后完成

const data = await s3.copyObject({
  Key: '611d308b6ef5e4066e047824/',
  // Key: encodeURI('611d308b6ef5e4066e047824/'), <-- i have also tried this unsuccessfully
  // Key: '611d308b6ef5e4066e047824', <-- and this
  Tagging: 'myKey=myVal',
  Metadata: {
    myKey: 'myVal'
  },
  CopySource: `${process.env.BUCKET}/611d308b6ef5e4066e047824/`,
  // CopySource: `${process.env.BUCKET}/611d308b6ef5e4066e047824`, // <-- this does not work either
  Bucket: process.env.BUCKET,
  MetadataDirective: 'REPLACE',
  TaggingDirective: 'REPLACE'
}).promise()

下面是我的应用程序中的错误堆栈

  app:lib:aws:s3:folders:updateOptions FATAL The specified key does not exist. +0ms
  app:lib:aws:s3:folders:updateOptions FATAL NoSuchKey: The specified key does not exist.
  app:lib:aws:s3:folders:updateOptions     at Request.extractError (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/services/s3.js:714:35)
  app:lib:aws:s3:folders:updateOptions     at Request.callListeners (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
  app:lib:aws:s3:folders:updateOptions     at Request.emit (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
  app:lib:aws:s3:folders:updateOptions     at Request.emit (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:688:14)
  app:lib:aws:s3:folders:updateOptions     at Request.transition (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:22:10)
  app:lib:aws:s3:folders:updateOptions     at AcceptorStateMachine.runTo (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/state_machine.js:14:12)
  app:lib:aws:s3:folders:updateOptions     at /Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/state_machine.js:26:10
  app:lib:aws:s3:folders:updateOptions     at Request.<anonymous> (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:38:9)
  app:lib:aws:s3:folders:updateOptions     at Request.<anonymous> (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:690:12)
  app:lib:aws:s3:folders:updateOptions     at Request.callListeners (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
  app:lib:aws:s3:folders:updateOptions     at Request.emit (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
  app:lib:aws:s3:folders:updateOptions     at Request.emit (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:688:14)
  app:lib:aws:s3:folders:updateOptions     at Request.transition (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:22:10)
  app:lib:aws:s3:folders:updateOptions     at AcceptorStateMachine.runTo (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/state_machine.js:14:12)
  app:lib:aws:s3:folders:updateOptions     at /Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/state_machine.js:26:10
  app:lib:aws:s3:folders:updateOptions     at Request.<anonymous> (/Users/aronlilland/Documents/dev/controlair/fileManager/node_modules/aws-sdk/lib/request.js:38:9) +0ms

所以这个错误是不正确的,它确实存在 - 更重要的是,我能够s3.copyObject在其他一些测试记录上成功运行,但上面的信息代表我的文件夹名称与实时数据,我不知道下一步该怎么做‍♂️< /p>

标签: node.jsamazon-web-servicesamazon-s3aws-sdk

解决方案


Amazon S3 folders don't actually exist. When using the Create folder button in the S3 Management Console, it actually creates a zero-length object that 'forces' the folder to appear.

It is not possible to "copy a folder". Well, you can copy the zero-length object, but it doesn't copy anything inside the folder since objects don't actually exist inside a hierarchy.

If your goal is to add or edit tags on an object, there is no need to copy anything. You can add tags with:


推荐阅读