简体   繁体   English

S3 listObjects 递归节点

[英]S3 listObjects recursively node

I want to recursively get all the files in an S3 bucket folder.我想递归获取 S3 存储桶文件夹中的所有文件。 I want to achieve the same behaviour as the ls -R command in Linux.我想在 Linux 中实现与ls -R命令相同的行为。

Here's an illustration这是一个插图

my_folder
  |_abc
  | |_abc_file.txt
  | |_xyz
  |   |_xyz_file.txt
  |
  |_file.txt

Considering the above directory structure.考虑到上述目录结构。 if I do如果我做

 const data = await s3.listObjectsV2({
    Prefix: 'my_folder/',
    Bucket: bucket,
    Delimiter: `/`,
}).promise();

Currently, I get the following data :目前,我得到以下data

-CommonPrefixes: ["abc"]
-Contents:["file.txt"]

The expected behaviour is:预期的行为是:

- Contents: ["abc/abc_file.txt", "abc/xyz/xyz_file.txt", "file.txt"]

I tried using ES6 generator functions and recursive functions to solve this problem and end up with a lot of messed up code.我尝试使用ES6 生成器函数递归函数来解决这个问题,但最终得到了很多混乱的代码。

I am using S3 node SDK我正在使用 S3 节点 SDK

Amazon S3 is a flat object storage system that does not use directories. Amazon S3 是一种不使用目录的平面对象存储系统。 Instead, the Key (filename) of an object includes the full path of the object.相反,对象的密钥(文件名)包括对象的完整路径。 Directories magically 'appear' based on the paths of existing objects, and can later disappear when there are no objects in that path.目录根据现有对象的路径神奇地“出现”,并且可以在该路径中没有对象时消失。

A CommonPrefix is the Amazon S3-equivalent of showing a sub-directory. CommonPrefix是显示子目录的 Amazon S3 等效项。

When calling ListObjects() with a Delimiter parameter (eg Delimiter='/' ), a list of subdirectories is returned in the CommonPrefixes field.使用Delimiter参数(例如Delimiter='/' )调用ListObjects()时,会在CommonPrefixes字段中返回子目录列表。 This allows recursion through directories much like traditional storage systems.这允许通过目录进行递归,就像传统的存储系统一样。 If you remove this parameter, all objects with the given Prefix will be returned, rather than just the 'current directory'.如果删除此参数,将返回具有给定前缀的所有对象,而不仅仅是“当前目录”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM