我正在处理一个处理大量图像的网站。用户将能够上传图像。图像将托管在单独的远程 Nginx 服务器上。创建目录并将图像上传到远程服务器的最佳方法是什么? SSH 会是一个可行的选择吗?有什么更好的吗?
Web 应用程序是使用 Spring Boot 创建的
最佳答案
SSH 不会真正帮助您通过网络共享或同步文件。
根据您的标签 (linux),我怀疑您可以在“远程”服务器上安装 nfs-kernel-server
。网络文件系统 (NFS) 允许您通过网络将目录从一个操作系统共享到另一个操作系统。这将允许您与 Spring Boot 服务器共享远程服务器的目录。
可以关注these instructions设置您的 NFS 服务器。
然后您可以使用以下命令将该远程目录挂载到您的 Spring Boot 服务器上:
$ NFS_SERVER="<your remote server>:/<your exported directory>"
$ sudo mount -t nfs "${NFS_SERVER}" /mnt
将文件读写到/mnt 实际上就是将它们读写到远程服务器上的目录中。因此,您需要做的就是让您的 Spring Boot 应用程序读取和写入 /mnt
。
您还可以查看社区项目 Spring Content .该项目是对存储的抽象,并提供了一系列存储类型的实现,包括良好的旧文件系统,因此它非常适合您的用例,并将通过消除您自己编写文件处理 Controller 和服务的代码来简化您的 Spring 应用程序代码。
添加它看起来像这样:
pom.xml (assuming maven).
<!-- Java API -->
<!-- just change this depdendency if you want to store somewhere else -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.8.0</version>
</dependency>
StoreConfig.java
@Configuration
@EnableFilesystemStores
@Import(RestConfiguration.class)
public class StoreConfig {
@Bean
FileSystemResourceLoader fileSystemResourceLoader() throws IOException {
return new FileSystemResourceLoader(new File("/mnt").getAbsolutePath());
}
}
FileStore.java
@StoreRestResource(path="files")
public interface FileStore extends Store<String> {
}
就是这样。 FileStore
本质上是一个通用的 Spring ResourceLoader。 spring-content-fs-boot-starter
依赖项将导致 Spring Content 注入(inject) FileStore
接口(interface)的基于文件系统的实现,因此您无需担心实现它自己。此外,spring-content-rest-boot-starter
依赖项将使 Spring Content 还注入(inject) @Controller
的实现,将 HTTP 请求转发到 文件存储
。
总而言之,您现在将在 /files
处拥有一个功能齐全(POST、PUT、GET、DELETE)的基于 REST 的文件服务,该服务将使用您的 FileStore
来在 /mnt
中检索(和存储)文件;即在您的远程 NFS 服务器上。
所以:
curl -F file=@/path/to/local/an-image.jpg/files/some-directory/an-image.jpg
将上传 an-image.jpg
并将其存储在服务器上的 /mnt/
中。
GET/files/some-directory/an-image.jpg
将再次下载an-image.jpg
。
HTH
如果有用的话,注入(inject)的 Controller 也支持视频流。
如果您希望记录有关用户上传的文件的额外元数据,那么您还可以将内容与 Spring Data 实体相关联(可用于记录此额外元数据)。您可以阅读更多相关信息 here .
HTH
关于linux - 创建目录并将图像上传到远程网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55979442/