创建目录并将图像上传到远程Web服务器 [英] creating directory and uploading images to remote web server

查看:174
本文介绍了创建目录并将图像上传到远程Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

该Web应用程序是使用Spring Boot创建的

解决方案

SSH并不会真正帮助您在网络上共享或同步文件.

基于您的标签(linux),我怀疑您可以在您的远程"服务器上安装nfs-kernel-server.网络文件系统(NFS)允许您通过网络将目录从一个操作系统共享到另一个操作系统.这将允许您与Spring Boot服务器共享来自远程服务器的目录.

您可以按照这些说明进行操作设置您的NFS服务器.

然后您可以使用以下方法在Spring Boot服务器上挂载该远程目录:

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

读写文件到/mnt实际上是在将它们读写到远程服务器上的目录中.因此,您要做的就是让您的Spring Boot应用读写/mnt.

您还可以查看社区项目 Spring Content .该项目是对Storage的抽象,并为包括良好的旧Filesystem在内的一系列Storage类型提供了实现,因此它非常适合您的用例,并且通过消除您自己编写文件处理控制器和服务的需求,将简化您的Spring应用程序代码. /p>

添加它看起来像这样:

pom.xml(假设为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注入FileStore接口的基于文件系统的实现,因此您不必担心自己实现它.而且,spring-content-rest-boot-starter依赖项将使Spring Content也注入@Controller的实现,该实现将HTTP请求转发到FileStore的方法上.

全部,您现在在/files处将具有基于REST的全功能文件服务(POST,PUT,GET,DELETE),该服务将使用您的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 Data实体(可用于记录此其他元数据)相关联.您可以在此处中了解更多信息.

>

HTH

I'm working on a website that deals with a lot of images. Users will have the ability to upload images. Images would be hosted on a separate remote Nginx server. What is the best way to create directories and upload images to the remote server? Would SSH be a viable option? anything better?

The web application is created with Spring Boot

解决方案

SSH won't really do anything to help you share or sync files across a network.

Based on your tag (linux) I suspect you could install the nfs-kernel-server on your "remote" server. Network FileSystem (NFS) allows you to share directories from one OS to another across a network. This will allow you to share a directory from your remote server with your Spring Boot server.

You can follow these instructions to set up your NFS server.

You can then mount that remote directory on your Spring Boot server with:

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

Reading and writing files to /mnt will actually be reading and writing them to the directory on your remote server. So, all you need to do then is make your Spring Boot app read and write to /mnt.

You could also look at the community project Spring Content. This project is an abstraction over Storage and provides implementations for a range of Storage types including the good old Filesystem so it is ideal for your use case and will simplify your Spring application code by removing the need to code file handling controllers and services yourself.

Adding it would look something like this:

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> {
  }

And that's it. The FileStore is essentially a generic Spring ResourceLoader. The spring-content-fs-boot-starter dependency will cause Spring Content to inject a filesystem-based implementation of the FileStore interface so you don't need to worry about implementing it yourself. Moreover, the spring-content-rest-boot-starter dependency will make Spring Content also inject an implementation of an @Controller that forwards HTTP requests onto the methods of the FileStore.

All up, you will now have a fully functional (POST, PUT, GET, DELETE) REST-based file service at /files that will use your FileStore to retrieve (and store) files in /mnt; i.e. on your remote NFS server.

So:

curl -F file=@/path/to/local/an-image.jpg /files/some-directory/an-image.jpg

will upload an-image.jpg and store it in /mnt/ on your server.

GET /files/some-directory/an-image.jpg

will download an-image.jpg again.

HTH

The injected controller also supports video streaming too, in case that is useful.

If you wish to record additional metadata about the files your users upload then you can also associate content with Spring Data entities (that can be used to record this additional metadata). You can read more about that here.

HTH

这篇关于创建目录并将图像上传到远程Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆