创建目录并将图像上传到远程网络服务器

creating directory and uploading images to remote web server

我正在处理一个处理大量图像的网站。用户将能够上传图像。图像将托管在单独的远程 Nginx 服务器上。创建目录并将图像上传到远程服务器的最佳方法是什么? SSH 会是一个可行的选择吗?有什么更好的吗?

Web 应用程序是使用 Spring Boot

创建的

SSH 不会真正帮助您通过网络共享或同步文件。

根据您的标签 (linux),我怀疑您可以在 "remote" 服务器上安装 nfs-kernel-server。网络文件系统 (NFS) 允许您通过网络将目录从一个 OS 共享到另一个。这将允许您与 Spring 引导服务器共享远程服务器的目录。

您可以按照 these instructions 设置您的 NFS 服务器。

然后您可以将该远程目录挂载到您的 Spring 引导服务器上:

$ NFS_SERVER="<your remote server>:/<your exported directory>"
$ sudo mount -t nfs "${NFS_SERVER}" /mnt

将文件读写到/mnt 实际上就是将文件读写到远程服务器上的目录中。因此,您需要做的就是让您的 Spring 启动应用程序读取和写入 /mnt.

您还可以查看社区项目 Spring Content。该项目是对存储的抽象,并提供了一系列存储类型的实现,包括良好的旧文件系统,因此它非常适合您的用例,并将通过消除对文件处理控制器和自己服务。

添加它看起来像这样:

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 内容注入 FileStore 接口的基于文件系统的实现,因此您无需担心自己实现它。此外,spring-content-rest-boot-starter 依赖项将使 Spring 内容也注入 @Controller 的实现,将 HTTP 请求转发到 FileStore 的方法。

总之,您现在将在 /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

注入的控制器也支持视频流,如果有用的话。

如果您希望记录有关用户上传的文件的额外元数据,那么您还可以将内容与 Spring 数据实体相关联(可用于记录此额外元数据)。您可以阅读更多相关信息 here

HTH