环回组件存储创建 API 以显示文件夹
Loopback component storage creating APIs to displaying folders
我已经能够将环回框架连接到 Amazon S3 帐户,从中创建 REST API。但是,它显示的信息仅在整个帐户的容器和文件级别。
我的目标是创建允许用户编写不同级别的文件夹名称然后显示该路径的内容的 API。
例如:
parent
child1
child1-child
toys.txt
child2
notes.txt
child2-child
toys.txt
所以如果用户输入 parent/child2 其余的 api 应该从那里开始显示内容,即 {notes.txt, child2-child/ } 深度为 1
我已经能够在 storage-service.js
的 getFiles
函数中硬连线一个桶:
StorageService.prototype.getFiles = function (container, options, cb) {
...
return this.client.getFiles('hardwiredbucketname', options, function (err, files) {
...
};
现在想创建允许我指定文件夹名称并将其所有内容显示到一个深度的 API。
这里首先要注意的是S3中没有文件夹。 S3 具有扁平结构。你应该先阅读Working with S3 folders。
从节点使用 S3 时,最好的办法是使用 AWS SDK. In order get folder contents you can create remote method that will accept path argument on your storage model and then you can use 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' 属性 以获取您的文件和文件夹。
我已经能够将环回框架连接到 Amazon S3 帐户,从中创建 REST API。但是,它显示的信息仅在整个帐户的容器和文件级别。
我的目标是创建允许用户编写不同级别的文件夹名称然后显示该路径的内容的 API。
例如: parent child1 child1-child toys.txt child2 notes.txt child2-child toys.txt
所以如果用户输入 parent/child2 其余的 api 应该从那里开始显示内容,即 {notes.txt, child2-child/ } 深度为 1
我已经能够在 storage-service.js
的 getFiles
函数中硬连线一个桶:
StorageService.prototype.getFiles = function (container, options, cb) {
...
return this.client.getFiles('hardwiredbucketname', options, function (err, files) {
...
};
现在想创建允许我指定文件夹名称并将其所有内容显示到一个深度的 API。
这里首先要注意的是S3中没有文件夹。 S3 具有扁平结构。你应该先阅读Working with S3 folders。
从节点使用 S3 时,最好的办法是使用 AWS SDK. In order get folder contents you can create remote method that will accept path argument on your storage model and then you can use 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' 属性 以获取您的文件和文件夹。