簡體   English   中英

生成 s3 aws 存儲桶的正確架構

[英]generate the right schema of an s3 aws bucket

我有這個代表 aws s3 存儲桶內容的路徑或位置列表:

const keysS3 = [
                'platform-tests/',
                'platform-tests/datasets/',
                'platform-tests/datasets/random_csv_datasets/',
                'platform-tests/datasets/random_csv_datasets/1/',
                'platform-tests/datasets/random_csv_datasets/1/raw/',
                'platform-tests/datasets/random_csv_datasets/1/raw/change_name_dtype.json',
                'platform-tests/datasets/random_csv_datasets/1/raw/change_name_dtype_2.csv',
                'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack.json',
                'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_0.csv',
                'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_1.csv',
                'platform-tests/datasets/random_csv_datasets/1/raw/random_data_stack_2.csv',
                'platform-tests/test-folder/',
                'README.md',
                'preprocessed_data.csv',
                'preprocessed_data2 copy.csv',
                'reg1.csv',
                'tested_demo.csv'
            ];

我在 Typescrtip 中編寫此代碼:

let output = {};
let current: any;
for (const path of keysS3) {
                current = output;
                const segment = path.split('/');
                segment.forEach((value, index) => {
                    if (value !== '' && index + 1 < segment.length) {
                        if (!(value in current)) {
                            current[value] = {};
                        }
                        current = current[value];
                    } else {
                        current[value] = null;
                    }
                });
            }
            console.log(JSON.stringify(output));

要將其轉換為該模式,輸出如下:

{
   "platform-tests":{
      "datasets":{
         "random_csv_datasets":{
            "1":{
               "raw":{
                  "change_name_dtype.json":null,
                  "change_name_dtype_2.csv":null,
                  "random_data_stack.json":null,
                  "random_data_stack_0.csv":null,
                  "random_data_stack_1.csv":null,
                  "random_data_stack_2.csv":null
               }
            }
         }
      },
      "test-folder":{

      }
   },
   "README.md":null,
   "preprocessed_data.csv":null,
   "preprocessed_data2 copy.csv":null,
   "reg1.csv":null,
   "tested_demo.csv":null
}

但是,問題是我得到了這個架構,它在每個子文件夾之后添加了這個額外的字段

““:空值

{
   "platform-tests":{
      "":null,
      "datasets":{
         "":null,
         "random_csv_datasets":{
            "1":{
               "":null,
               "raw":{
                  "":null,
                  "change_name_dtype.json":null,
                  "change_name_dtype_2.csv":null,
                  "random_data_stack.json":null,
                  "random_data_stack_0.csv":null,
                  "random_data_stack_1.csv":null,
                  "random_data_stack_2.csv":null
               }
            },
            "":null
         }
      },
      "test-folder":{
         "":null
      }
   },
   "README.md":null,
   "preprocessed_data.csv":null,
   "preprocessed_data2 copy.csv":null,
   "reg1.csv":null,
   "tested_demo.csv":null
}

過濾掉.split上的空元素,因為您以/結尾,因此您將始終在數組末尾得到一個空數組元素。

const segment = path.split('/').filter(Boolean);

我只是通過添加這行代碼來解決它:

 if (!(value in current)) {
     current[value] = value.includes('.') ? null : {};
}

驗證值是路徑還是文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM