环回组件存储创建api来显示文件夹

Loopback component storage creating APIs to displaying folders

本文关键字:显示 显示文件 文件夹 api 创建 组件 存储      更新时间:2023-09-26

我已经能够将环回框架连接到Amazon S3帐户,并从中创建REST api。但是,它只显示整个帐户的容器和文件级别的信息。

我的目标是创建api,允许用户编写不同级别的文件夹名称,然后显示该路径的内容。

例如

:家长child1child1-childtoys.txtchild2notes.txtchild2-childtoys.txt

所以如果用户输入parent/child2,剩下的API应该显示从那里开始的内容例如{notes.txt, child2-child/},深度为1

我已经能够在storage-service.jsgetFiles函数中硬连接单个桶:

StorageService.prototype.getFiles = function (container, options, cb) {
  ...
  return this.client.getFiles('hardwiredbucketname', options, function (err, files) {
...
};

,现在想要创建api,允许我指定文件夹名称并以单一深度显示其所有内容。

首先要注意的是,S3中没有文件夹。S3具有扁平结构。您应该先阅读使用S3文件夹。

当从节点使用S3时,最好的方法是使用AWS SDK。为了获取文件夹内容,你可以创建远程方法,它将接受存储模型上的路径参数,然后你可以使用listObjecstV2方法来获取对象列表。

var AWS = require('aws-sdk');
var s3 = new AWS.S3()
var params = {
  Bucket: 'hardwiredbucketname',
  Prefix: 'parent/',
  Delimiter: '/'
};
s3.listObjectsV2(params, function(err, data){
  if (err) console.log(err, err.stack);
  console.log(data);
});

从回调中获取'data'对象,然后解析'Contents'和'CommonPrefixes'属性来获取你的文件和文件夹。