简体   繁体   English

使用JavaScript进行服务器端文件系统访问

[英]Server-Side FileSystem Access with JavaScript

I'm hosting a static website on Amazon S3. 我在Amazon S3上托管一个静态网站。 In my Bucket there are also some .txt , .pdf, .svg files stored. 在我的存储桶中,还存储了一些.txt,.pdf和.svg文件。 I want to display a list of these files (only names no content) on my html website with javascript. 我想用javascript在我的html网站上显示这些文件的列表(只命名没有内容)。 As users can upload files to the bucket the names always change. 由于用户可以将文件上传到存储桶,因此名称总是会更改。 That's why I want to list them. 这就是为什么我要列出它们。 And I do not want to list all files instead of showing the html file but show html file with a table that lists files from a specific folder in the bucket. 而且我不想列出所有文件,而不是显示html文件,而是显示带有表的html文件,该表列出了存储桶中特定文件夹中的文件。

I do not use nodejs just js for browsers. 我不只将nodejs用作浏览器的js。 I try not to use any new modules. 我尝试不使用任何新模块。 The Project should remain simple. 该项目应保持简单。

I already read tutorials on using nodejs modules like 'fs' in the browser with help of browserify. 我已经阅读了有关在browserify的帮助下在浏览器中使用诸如'fs'之类的nodejs模块的教程。 I didn't manage that or any other ideas I've found. 我没有解决这个问题,也没有发现其他想法。

I read something about FileSystemDirectoryReader for js but that's not generally supported yet. 我阅读了有关FileSystemDirectoryReader for js的内容,但目前尚不普遍支持。 Would WebKitFileSystem meet my needs? WebKitFileSystem是否可以满足我的需求? I understood that most of the fileSystem APIs work with virtuall directories not the directory i need. 我知道大多数fileSystem API都使用virtuall目录,而不是我需要的目录。

Now I wonder if I actually need anything like npm modules as I do not want to access the Client FileSystem but my own fileSystem on S3. 现在我想知道我是否真的需要npm模块之类的东西,因为我不想访问客户端文件系统,而是要访问S3上的我自己的文件系统。 I already read data from those files with XMLHttpRequest but can I also just list the names? 我已经使用XMLHttpRequest从这些文件中读取了数据,但是我也可以只列出名称吗?

It'd be great if I had some code like: var arrFiles = []; 如果我有这样的代码,那就太好了:var arrFiles = []; arrFiles = readFiles("./files/*"); arrFiles = readFiles(“ ./ files / *”);

Another Approach would be using AWS Lambda to read all file names, save those as a list in a text file and read that file with js to print the names on the website. 另一种方法是使用AWS Lambda读取所有文件名,将它们保存为列表在文本文件中,然后使用js读取该文件以在网站上打印名称。 Seems to be complicated. 似乎很复杂。

If that makes any sense. 如果那有意义的话。 Thanks in advance 提前致谢

With AWS AWS Mobile CLI and the aws-amplify it is possible. 借助AWS, AWS Mobile CLIaws-amplify成为可能。 They is great documentationa and examples on there site 他们是有很大的documentationa和示例网站

Basically you create an aws mobile hub, configure your S3 bucket, cognito, dynamodb and any other aws services you will need. 基本上,您将创建aws移动集线器,配置您的S3 bucket,cognito,dynamodb以及所需的其他任何aws服务。

First install AWS mobile cli with node js 首先使用Node js安装AWS Mobile CLI

npm install -g awsmobile-cli

configure it. 配置它。

awsmobile configure

initialize your project 初始化您的项目

awsmobile init

once than install the aws-amplify node package in your project 在您的项目中安装aws-amplify节点软件包一次

npm i install aws-amplify

Import the necessary modules and configure it. 导入必要的模块并进行配置。

import Amplify, { Storage } from 'aws-amplify';
import aws_exports from './aws-exports';
Amplify.configure(aws_exports);

Finally just call the List function, it will list keys under path specified. 最后只需调用List函数,它将在指定路径下列出键。

Storage.list('photos/')
       .then(result => console.log(result))
       .catch(err => console.log(err));

You can perform a variety of functionality like upload, download and delete. 您可以执行多种功能,例如上载,下载和删除。 They are maintained and supported by an AWS team and are not just limited to S3. 它们由AWS团队维护和支持,不仅限于S3。

They are compatible with both Angular and react js. 它们与Angular和react js都兼容。

I now generated the table content with a lambda function. 现在,我使用lambda函数生成了表内容。

In that lambda function I use the aws.s3 class and listObjectsV2(). 在该lambda函数中,我使用aws.s3类和listObjectsV2()。 It reads all data in a specific folder. 它读取特定文件夹中的所有数据。 I write the names of all found files to a text file that is stored in the bucket. 我将所有找到的文件的名称写到存储在存储桶中的文本文件中。

The js file uses an XMLHttp Request to read that text file everytime the page reloads. 每次页面重新加载时,js文件都使用XMLHttp Request读取该文本文件。 So it gets the content of the bucket folder and can write the names to the table. 这样它就可以获取bucket文件夹的内容,并且可以将名称写入表中。 The lambda funtion is always triggered when I go back to the main page of my website where the table is shown. 当我返回显示该表的网站主页时,总是触发lambda函数。

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

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